赞
踩
- import numpy as np
- import pandas as pd
- import datetime
- from pyecharts.charts import Line
- from pyecharts.charts import Boxplot
- from pyecharts.charts import Pie,Grid
- from pyecharts import options as opts
- from pyecharts.charts import Calendar
代码如下:
- # 读取数据
- df = pd.read_csv('beijing_AQI_2018.csv')
- attr = df['Date'].tolist() # 将 Date 转换为列表
- v1 = df['AQI'].tolist() # 将 AQI 转换为列表
-
- # 创建折线图
- line = (
- Line()
- .add_xaxis(attr)
- .add_yaxis("AQI值:", v1, is_smooth=True,
- areastyle_opts=opts.AreaStyleOpts(opacity=0.3, color="#000"),
- markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]),
- markpoint_opts=opts.MarkPointOpts(data=[
- opts.MarkPointItem(type_="max", name="最大值"),
- opts.MarkPointItem(type_="min", name="最小值")
- ]))
- .set_global_opts(title_opts=opts.TitleOpts(title="2018年北京AQI全年走势图",
- pos_top='5%',
- pos_left='center'),
- xaxis_opts=opts.AxisOpts(type_="category"),
- yaxis_opts=opts.AxisOpts(type_="value"))
- )
-
- # 将图表渲染到 HTML 文件
- line.render("./2018年北京AQI全年走势图.html")
结果如下:
代码如下:
- # 读取数据
- df = pd.read_csv('beijing_AQI_2018.csv')
- attr = df['Date'].tolist() # 将 Date 转换为列表
- v1 = df['PM'].tolist() # 将 PM 转换为列表
-
- # 创建折线图
- line = (
- Line()
- .add_xaxis(attr)
- .add_yaxis("PM2.5值:", v1, is_smooth=True,
- areastyle_opts=opts.AreaStyleOpts(opacity=0.3, color="#000"),
- markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]),
- markpoint_opts=opts.MarkPointOpts(data=[
- opts.MarkPointItem(type_="max", name="最大值"),
- opts.MarkPointItem(type_="min", name="最小值")
- ]))
- .set_global_opts(title_opts=opts.TitleOpts(title="2018年北京PM2.5全年走势图",
- pos_top='5%',
- pos_left='center'),
- xaxis_opts=opts.AxisOpts(type_="category"),
- yaxis_opts=opts.AxisOpts(type_="value"))
- )
-
- # 将图表渲染到 HTML 文件
- line.render("./2018年北京PM2.5全年走势图.html")
结果如下:
代码如下:
- # 读取数据
- df = pd.read_csv('beijing_AQI_2018.csv')
-
- # 提取 Date 和 AQI 的值
- dom = df[['Date', 'AQI']]
-
- # 提取月份
- df['month'] = df['Date'].apply(lambda x: x.split('/')[1])
-
- # 根据月份分组并计算每月的 AQI 平均值
- month_com = df.groupby('month')['AQI'].mean().reset_index()
-
- # 排序月份
- month_com = month_com.sort_values(by='month')
-
- # 构造月份标签
- attr = ["{}月".format(i) for i in range(1, 13)]
-
- # 获取每月的 AQI 平均值
- v1 = month_com['AQI'].tolist()
- v1 = ["{}".format(int(i)) for i in v1]
-
- # 创建折线图
- line = (
- Line()
- .add_xaxis(attr)
- .add_yaxis("AQI月均值", v1,
- markpoint_opts=opts.MarkPointOpts(data=[
- opts.MarkPointItem(type_="max", name="最大值"),
- opts.MarkPointItem(type_="min", name="最小值")
- ]))
- .set_global_opts(title_opts=opts.TitleOpts(title="2018年北京月均AQI走势图",
- pos_top='5%',
- pos_left='center'),
- xaxis_opts=opts.AxisOpts(type_="category"),
- yaxis_opts=opts.AxisOpts(type_="value"))
- )
-
- # 渲染图表到 HTML 文件
- line.render("./2018年北京月均AQI走势图.html")
结果如下:
代码如下:
- # 读取数据
- df = pd.read_csv('beijing_AQI_2018.csv')
-
- # 提取 Date 和 PM 的值
- dom = df[['Date', 'PM']]
-
- # 提取月份
- df['month'] = df['Date'].apply(lambda x: x.split('/')[1])
-
- # 根据月份分组并计算每月的 PM 平均值
- month_com = df.groupby('month')['PM'].mean().reset_index()
-
- # 排序月份
- month_com = month_com.sort_values(by='month')
-
- # 构造月份标签
- attr = ["{}月".format(i) for i in range(1, 13)]
-
- # 获取每月的 PM 平均值
- v1 = month_com['PM'].tolist()
- v1 = ["{}".format(int(i)) for i in v1]
-
- # 创建折线图
- line = (
- Line()
- .add_xaxis(attr)
- .add_yaxis("PM2.5月均值", v1,
- markpoint_opts=opts.MarkPointOpts(data=[
- opts.MarkPointItem(type_="max", name="最大值"),
- opts.MarkPointItem(type_="min", name="最小值")
- ]))
- .set_global_opts(title_opts=opts.TitleOpts(title="2018年北京月均PM2.5走势图",
- pos_top='5%',
- pos_left='center'),
- xaxis_opts=opts.AxisOpts(type_="category"),
- yaxis_opts=opts.AxisOpts(type_="value"))
- )
-
- # 渲染图表到 HTML 文件
- line.render("./2018年北京月均PM2.5走势图.html")
结果如下:
代码如下:
- # 读取数据
- df = pd.read_csv('beijing_AQI_2018.csv')
-
- # 提取 Date 和 AQI 的值
- dom = df[['Date', 'AQI']]
- data = [[], [], [], []]
- dom1, dom2, dom3, dom4 = data
-
- # 根据季度提取 AQI 值
- for date, aqi in zip(dom['Date'], dom['AQI']):
- month = int(date.split('/')[1])
- if month in [1, 2, 3]:
- dom1.append(aqi)
- elif month in [4, 5, 6]:
- dom2.append(aqi)
- elif month in [7, 8, 9]:
- dom3.append(aqi)
- else:
- dom4.append(aqi)
-
- # 创建箱形图
- boxplot = Boxplot()
-
- # 设置 x 轴和 y 轴数据
- x_axis = ['第一季度', '第二季度', '第三季度', '第四季度']
- y_axis = [dom1, dom2, dom3, dom4]
- y_axis = boxplot.prepare_data(y_axis)
-
- # 添加数据到箱形图
- boxplot.add_xaxis(x_axis)
- boxplot.add_yaxis("AQI", y_axis)
-
- # 设置图表全局选项
- boxplot.set_global_opts(title_opts=opts.TitleOpts(title="2018年北京季度AQI箱形图", pos_left='center', pos_top='6%'))
-
- # 渲染图表到 HTML 文件
- boxplot.render("./2018年北京季度AQI箱形图.html")
结果如下:
代码如下:
- # 读取数据
- df = pd.read_csv('beijing_AQI_2018.csv')
-
- # 提取 Date 和 PM 的值
- dom = df[['Date', 'PM']]
- data = [[], [], [], []]
- dom1, dom2, dom3, dom4 = data
-
- # 根据季度提取 PM 值
- for date, pm in zip(dom['Date'], dom['PM']):
- month = int(date.split('/')[1])
- if month in [1, 2, 3]:
- dom1.append(pm)
- elif month in [4, 5, 6]:
- dom2.append(pm)
- elif month in [7, 8, 9]:
- dom3.append(pm)
- else:
- dom4.append(pm)
-
- # 创建箱形图
- boxplot = Boxplot()
-
- # 设置 x 轴和 y 轴数据
- x_axis = ['第一季度', '第二季度', '第三季度', '第四季度']
- y_axis = [dom1, dom2, dom3, dom4]
- y_axis = boxplot.prepare_data(y_axis)
-
- # 添加数据到箱形图
- boxplot.add_xaxis(x_axis)
- boxplot.add_yaxis("PM2.5", y_axis)
-
- # 设置图表全局选项
- boxplot.set_global_opts(title_opts=opts.TitleOpts(title="2018年北京季度PM2.5箱形图", pos_left='center', pos_top='6%'))
-
- # 渲染图表到 HTML 文件
- boxplot.render("./2018年北京季度PM2.5箱形图.html")
结果如下:
代码如下:
- # 读取数据
- df = pd.read_csv('beijing_AQI_2018.csv')
-
- # 根据 Quality_grade 分组
- rank_message = df.groupby(['Quality_grade'])
-
- # 计算每个 Quality_grade 的频数
- rank_com = rank_message['Quality_grade'].agg(['count'])
- rank_com.reset_index(inplace=True) # 重置索引
- rank_com_last = rank_com.sort_values('count', ascending=False) # 从大到小排序
-
- # 提取 Quality_grade 和对应的频数
- attr = rank_com_last['Quality_grade'].tolist()
- v1 = rank_com_last['count'].tolist()
-
- # 创建饼图
- pie = (
- Pie()
- .add("", [list(z) for z in zip(attr, v1)], radius=["40%", "75%"],
- label_opts=opts.LabelOpts(is_show=True, formatter="{b}: {d}%"))
- .set_global_opts(title_opts=opts.TitleOpts(title="2018年北京全年空气质量情况", pos_left='center', pos_top='0'),
- legend_opts=opts.LegendOpts(is_show=True, orient="vertical", pos_left="left", pos_top="10%"))
- )
-
- # 渲染图表到 HTML 文件
- pie.render('./2018年北京全年空气质量情况.html')
结果如下:
代码如下:
- # Read data from 'beijing_AQI_2018.csv'
- df = pd.read_csv('beijing_AQI_2018.csv')
-
- # Extract 'Date' and 'PM' columns
- dom = df[['Date', 'PM']]
-
- list1 = []
-
- # Compress the date and PM values into a list of lists
- for i, j in zip(dom['Date'], dom['PM']):
- time_list = i.split('/')
- time = datetime.date(int(time_list[0]), int(time_list[1]), int(time_list[2]))
- PM = int(j)
- list1.append([str(time), PM])
-
- # Create a calendar chart
- calendar = (
- Calendar(init_opts=opts.InitOpts(width="800px", height="400px"))
- .add(
- series_name="2018年北京PM2.5指数日历图",
- yaxis_data=list1,
- calendar_opts=opts.CalendarOpts(range_="2018"),
- )
- .set_global_opts(
- title_opts=opts.TitleOpts(title="2018年北京PM2.5指数日历图", pos_left="center",pos_top='50%'),
- visualmap_opts=opts.VisualMapOpts(
- max_=300, min_=0, orient="horizontal", is_piecewise=True,
- pos_top='60%', # 将视觉映射放置在图表的顶部
- pos_left='center' # 将视觉映射放置在图表的水平居中位置
- ),
- )
- )
-
- # Render the chart to an HTML file
- calendar.render('./2018年北京PM2.5指数日历图.html')
结果如下:
代码如下:
-
- citys = ['beijing', 'shanghai', 'guangzhou', 'shenzhen']
- cityes_AQI = []
-
- # 遍历四个城市的数据
- for city in citys:
- filename = city + '_AQI' + '_2018.csv'
- aqi_data = pd.read_csv(filename)
-
- # 提取日期和AQI指数两列内容
- get_data = aqi_data[['Date', 'AQI']]
-
- # 获取每行数据的月份
- month_for_data = [j.split('/')[1] for j in get_data['Date']]
- aqi_data['Month'] = month_for_data
-
- # 求每个月AQI平均值
- month_AQI = aqi_data.groupby(['Month'])['AQI'].mean().reset_index()
-
- # 获取每个城市月均AQI的数据,转化为int数据类型
- city_AQI_data_int = month_AQI['AQI'].astype(int).tolist()
- cityes_AQI.append(city_AQI_data_int)
-
- months = [str(i) + '月' for i in range(1, 13)]
-
- line = (
- Line(init_opts=opts.InitOpts(width="800px", height="400px"))
- .add_xaxis(months)
- .add_yaxis("北京", cityes_AQI[0], color='red', label_opts=opts.LabelOpts(is_show=False))
- .add_yaxis("上海", cityes_AQI[1], color='purple', label_opts=opts.LabelOpts(is_show=False))
- .add_yaxis("广州", cityes_AQI[2], color='blue', label_opts=opts.LabelOpts(is_show=False))
- .add_yaxis("深圳", cityes_AQI[3], color='orange', label_opts=opts.LabelOpts(is_show=False))
- .set_global_opts(
- title_opts=opts.TitleOpts(title="2018年北上广深AQI全年走势图", pos_left='center', pos_top='0'),
- legend_opts=opts.LegendOpts(orient="horizontal", pos_top="8%"),
- )
- )
-
- line.render('./2018年北上广深AQI全年走势图.html') # 生成HTML文件,保存在当前目录下
结果如下:
代码如下:
- import numpy as np
- import pandas as pd
- from pyecharts.charts import Line
- from pyecharts import options as opts
-
- citys = ['beijing', 'shanghai', 'guangzhou', 'shenzhen']
- cityes_AQI = []
-
- # 遍历四个城市的数据
- for city in citys:
- filename = city + '_AQI' + '_2018.csv'
- aqi_data = pd.read_csv(filename)
-
- # 提取日期和PM2.5指数两列内容
- get_data = aqi_data[['Date', 'PM']]
-
- # 获取每行数据的月份
- month_for_data = [j.split('/')[1] for j in get_data['Date']]
- aqi_data['Month'] = month_for_data
-
- # 求每个月PM2.5平均值
- month_AQI = aqi_data.groupby(['Month'])['PM'].mean().reset_index()
-
- # 获取每个城市月均PM2.5的数据,转化为int数据类型
- city_AQI_data_int = month_AQI['PM'].astype(int).tolist()
- cityes_AQI.append(city_AQI_data_int)
-
- months = [str(i) + '月' for i in range(1, 13)]
-
- line = (
- Line(init_opts=opts.InitOpts(width="800px", height="400px"))
- .add_xaxis(months)
- .add_yaxis("北京", cityes_AQI[0], color='red', label_opts=opts.LabelOpts(is_show=False))
- .add_yaxis("上海", cityes_AQI[1], color='purple', label_opts=opts.LabelOpts(is_show=False))
- .add_yaxis("广州", cityes_AQI[2], color='blue', label_opts=opts.LabelOpts(is_show=False))
- .add_yaxis("深圳", cityes_AQI[3], color='orange', label_opts=opts.LabelOpts(is_show=False))
- .set_global_opts(
- title_opts=opts.TitleOpts(title="2018年北上广深PM2.5全年走势图", pos_left='center', pos_top='0'),
- legend_opts=opts.LegendOpts(orient="horizontal", pos_top="8%"),
- )
- )
-
- line.render('./2018年北上广深PM2.5全年走势图.html') # 生成HTML文件,保存在当前目录下
结果如下:
代码如下:
- citys = ['beijing', 'shanghai', 'guangzhou', 'shenzhen']
- v = []
- attrs = []
- for i in range(4):
- filename = citys[i] + '_AQI' + '_2018.csv'
- df = pd.read_csv(filename)
-
- # 根据 Quality_grade 分组
- Quality_grade_message = df.groupby(['Quality_grade'])
- # 每组的频数
- Quality_grade_com = Quality_grade_message['Quality_grade'].agg(['count'])
- Quality_grade_com.reset_index(inplace=True)
- Quality_grade_com_last = Quality_grade_com.sort_values('count', ascending=False)
- # 取 Quality_grade 的值
- Quality_grade_array = Quality_grade_com_last['Quality_grade'].values.tolist()
- attrs.append(Quality_grade_array)
- Quality_grade_count = Quality_grade_com_last['count'].values.tolist()
- v.append(Quality_grade_count)
-
-
- # 创建饼图并设置属性
- pie1 = (
- Pie()
- .add("北京", [list(z) for z in zip(attrs[0], v[0])], radius=["20%", "40%"], center=["30%", "27%"],)
- .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {d}%"))
- .set_global_opts(title_opts=opts.TitleOpts(title="北京", pos_left='27%', pos_top='25%'),
- legend_opts=opts.LegendOpts(orient="vertical", pos_right="5%", pos_top="35%"))
- )
-
- pie2 = (
- Pie()
- .add("上海", [list(z) for z in zip(attrs[1], v[1])], radius=["20%", "40%"], center=["70%", "27%"],)
- .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {d}%"))
- .set_global_opts(title_opts=opts.TitleOpts(title="上海", pos_left='67%', pos_top='25%'),
- legend_opts=opts.LegendOpts(is_show=False))
- )
-
- pie3 = (
- Pie()
- .add("广州", [list(z) for z in zip(attrs[2], v[2])], radius=["20%", "40%"], center=["30%", "77%"],)
- .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {d}%"))
- .set_global_opts(title_opts=opts.TitleOpts(title="广州", pos_left='27%', pos_top='75%'),
- legend_opts=opts.LegendOpts(is_show=False))
- )
-
- pie4 = (
- Pie()
- .add("深圳", [list(z) for z in zip(attrs[3], v[3])], radius=["20%", "40%"], center=["70%", "77%"],)
- .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {d}%"))
- .set_global_opts(title_opts=opts.TitleOpts(title="深圳", pos_left='67%', pos_top='75%'),
- legend_opts=opts.LegendOpts(is_show=False))
- )
-
-
-
- # 创建 Grid 并添加饼图
- grid = (
- Grid()
- .add(pie1, grid_opts=opts.GridOpts(pos_left="5%", pos_right="50%", pos_top="10%"))
- .add(pie2, grid_opts=opts.GridOpts(pos_left="55%", pos_right="5%", pos_top="10%"))
- .add(pie3, grid_opts=opts.GridOpts(pos_left="5%", pos_right="50%", pos_top="50%"))
- .add(pie4, grid_opts=opts.GridOpts(pos_left="55%", pos_right="5%", pos_top="50%"))
- )
-
- grid.render('./2018年北上广深全年空气质量情况.html')
结果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。