大家好,我是PYTHON爱好者二狗.今天向各位领导汇报的题目是:使用pyecharts绘制简单的数据动态展示图.
1.文中电脑操作系统:win10
2.文中所使用的python模块:
(1).pandas (第三方模块,需要pip安装)
安装命令:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pandas
官方文档:https://pandas.pydata.org/pandas-docs/stable/
(2).pyecharts (第三方模块,需要pip安装)
安装命令:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyecharts
官方文档:https://pyecharts.org/#/zh-cn/intro
3.文中数据源文件:二手车数据.csv(如果需要,请后台私信)
4.pyecharts版本:1.9.1
5.文中所使用的IDE:VSCODE(安装Jupyter插件)
文中绘制的动态图表分为:①时间线轮播图,②柱状图,③饼图,④词云图,⑤树图
接下来,二狗向各位领导汇报绘制过程及注意事项.
import pandas as pd # 导入pandas模块
df = pd.read_csv('二手车数据.csv',index_col=0) # 读取数据表格
df['car_year'] = df.car_year.agg(lambda x : x.split('-')[0])
# 切割表格中的car_year列的所有值 取出年份
df = df[(df.car_city=='昆明')
| (df.car_city=='贵阳')
|(df.car_city=='南宁')
| (df.car_city=='成都')
| (df.car_city=='重庆')
|(df.car_city=='拉萨')]
# 筛选出表格中car_city列中的昆明,贵阳,南宁,成都,重庆,拉萨的数据
tb = df.pivot_table(values=['car_price(万元)',],index=['car_year','car_city'])
# 把筛选出的城市数据聚合分类计算出每年每个城市的二手车均值
注:pivot_table()方法,不指定aggfunc参数时,默认aggfunc="mean"计算均值.
year_list = [i[0] for i in tb.index] # 生成一个装有年份的列表
city_list = [i[1] for i in tb.index] # 生成一个装有城市名称的列表
price_list = [round(i,ndigits=2) for i in tb['car_price(万元)']]
# 生成一个装有每个城市均值的列表,对列表里的每个元素四舍五入,保留2位小数
time_data = {} #创建一个空字典
for k,v1,v2 in zip(year_list,city_list,price_list):
#遍历 year_list city_list price_list 组装一个数据格式为{年份:{城市:价格...}}的字典
if k not in time_data:
time_data[k] = {v1:v2}
else:
time_data[k].update({v1:v2})
import pyecharts.options as opt
from pyecharts.commons.utils import JsCode
from pyecharts.charts import Pie, Bar,Timeline,Grid,TreeMap,WordCloud
from pyecharts.globals import ThemeType
#导入绘图模块
tl = Timeline(
init_opts=opt.InitOpts(
theme=ThemeType.DARK,
animation_opts=opt.AnimationOpts(
animation_duration=3000,
animation_delay_update = 10,
)
))# 绘制时间线轮播图
for year,city_price_dict in time_data.items():
#分别遍历字典的键和值
values_list = list(sorted(city_price_dict.items(), key=lambda value : value[1], reverse=True))
# 把字典的值 转换为降序排列(从大到小)的列表 [(城市1,价格1),(城市2,价格2)...]
values_data = (sorted(city_price_dict.items(), key=lambda value : value[1], reverse=False))
# 把字典的值 转换为升序排列(从小到大)的列表 [(城市1,价格1),(城市2,价格2)...]
import pyecharts.options as opt
from pyecharts.commons.utils import JsCode
from pyecharts.charts import Pie, Bar,Timeline,Grid,TreeMap,WordCloud
from pyecharts.globals import ThemeType
#导入绘图模块
tl = Timeline(
init_opts=opt.InitOpts(
theme=ThemeType.DARK,
animation_opts=opt.AnimationOpts(
animation_duration=3000,
animation_delay_update = 10,
)
))# 绘制时间线轮播图
for year,city_price_dict in time_data.items():
#分别遍历字典的键和值
values_list = list(sorted(city_price_dict.items(), key=lambda value : value[1], reverse=True))
# 把字典的值 转换为降序排列(从大到小)的列表 [(城市1,价格1),(城市2,价格2)...]
values_data = (sorted(city_price_dict.items(), key=lambda value : value[1], reverse=False))
# 把字典的值 转换为升序排列(从小到大)的列表 [(城市1,价格1),(城市2,价格2)...]
pie = (#绘制饼图
Pie()
.add(
series_name=' 单位:万元',
data_pair = values_data,
radius= ['50%','60%'],
center = ['70%','50%'],
rosetype= 'radius'
)
.set_global_opts(
title_opts=opt.TitleOpts(
title= f'{year}年西南地区二手车均价'
)
)
)
charts = WordCloud()
charts.add( #绘制词云图
series_name = '均价(万元)',
data_pair= values_data,
textstyle_opts=opt.TextStyleOpts(font_family="serif"),
shape = "diamond",
word_size_range= [10, 50],
pos_left = '35%',
pos_top = '12%',
)
charts.set_global_opts(
title_opts = opt.TitleOpts(
),
)
bar = (#绘制柱状图
Bar()
.add_xaxis([i[0] for i in values_data])
.add_yaxis(
"单位万元",
[i[1] for i in values_data],
bar_width=30,
category_gap=0
)
.reversal_axis()
.set_global_opts(
legend_opts=opt.LegendOpts(is_show=False),
yaxis_opts=opt.AxisOpts(is_show=True,),
xaxis_opts=opt.AxisOpts(is_show=True),
)
.set_series_opts(
label_opts=opt.LabelOpts(position="right"),
itemstyle_opts={
"normal": {
"color": JsCode(
"""new echarts.graphic.LinearGradient(0, 0, 0, 1, [{offset: 0,color: 'rgba(0, 244, 255, 1)'}, {offset: 1,color: 'rgba(0, 77, 167, 1)'}], false)"""
),
"barBorderRadius": [30, 30, 30, 30],"shadowColor": "rgb(0, 160, 221)",
}
}
)
)
tree = ( #绘制树图
TreeMap()
.add(
series_name=year,
data=[{'value':i[1],'name':i[0]} for i in values_data],
visual_min=30,
leaf_depth=1,
# 标签居中为 position = "inside"
width= '12%',
height= '53%',
pos_left = '88%',
pos_top = '47%',
label_opts=opt.LabelOpts(position="inside"),
breadcrumb_opts=opt.TreeMapBreadcrumbOpts(
is_show=False,
pos_left= 'left',
height=50,
),
roam=False,
)
.set_global_opts(
legend_opts=opt.LegendOpts(is_show=False),
title_opts=opt.TitleOpts(
),
)
)
grid_chart =( #把绘制好的图形组合在一起
Grid()
.add(
bar,
grid_opts=opt.GridOpts(
width= 300,
height= 320
),
)
.add(
tree,
grid_opts=opt.GridOpts(
),
)
.add(
charts,
grid_opts=opt.GridOpts(
),
)
.add(
pie,
grid_opts=opt.GridOpts(
),
)
)
tl.add( #把组合好的图形,添加进时间线轮播图里
chart=grid_chart,
time_point= f'{year}年',
)
tl.add_schema(
is_auto_play= True,
is_timeline_show=True,
orient="horizontal",
pos_left='left',
symbol_size=[8,5]
)
tl.render('时间线图.html')#生成绘制好的图形html文件
tl.render_notebook() # 在notebook里渲染出来绘制好的图形
PS:绘图过程就是调整各图形的对应的参数,各位领导请详阅官方文档:https://pyecharts.org/#/zh-cn/intro
import pandas as pd
import pyecharts.options as opt
from pyecharts.commons.utils import JsCode
from pyecharts.charts import Pie, Bar,Timeline,Grid,TreeMap,WordCloud
from pyecharts.globals import ThemeType
time_data = {}
tl = Timeline(
init_opts=opt.InitOpts(
theme=ThemeType.DARK,
animation_opts=opt.AnimationOpts(
animation_duration=3000,
animation_delay_update = 10,
)
))
df = pd.read_csv('二手车数据.csv',index_col=0)
df['car_year'] = df.car_year.agg(lambda x : x.split('-')[0])
df = df[(df.car_city=='昆明') | (df.car_city=='贵阳') |(df.car_city=='南宁') | (df.car_city=='成都') | (df.car_city=='重庆')| (df.car_city=='拉萨')]
tb = df.pivot_table(values=['car_price(万元)',],index=['car_year','car_city'])
year_list = [i[0] for i in tb.index]
city_list = [i[1] for i in tb.index]
price_list = [round(i,ndigits=2) for i in tb['car_price(万元)']]
for k,v1,v2 in zip(year_list,city_list,price_list):
if k not in time_data:
time_data[k] = {v1:v2}
else:
time_data[k].update({v1:v2})
for year,city_price_dict in time_data.items():
values_list = list(sorted(city_price_dict.items(), key=lambda value : value[1], reverse=True))
values_data = (sorted(city_price_dict.items(), key=lambda value : value[1], reverse=False))
pie = (
Pie()
.add(
series_name=' 单位:万元',
data_pair = values_data,
radius= ['50%','60%'],
center = ['70%','50%'],
rosetype= 'radius'
)
.set_global_opts(
title_opts=opt.TitleOpts(
title= f'{year}年西南地区二手车均价'
)
)
)
charts = WordCloud()
charts.add(
series_name = '均价(万元)',
data_pair= values_data,
textstyle_opts=opt.TextStyleOpts(font_family="serif"),
shape = "diamond",
word_size_range= [10, 50],
pos_left = '35%',
pos_top = '12%',
)
charts.set_global_opts(
title_opts = opt.TitleOpts(
),
)
bar = (
Bar()
.add_xaxis([i[0] for i in values_data])
.add_yaxis(
"单位万元",
[i[1] for i in values_data],
bar_width=30,
category_gap=0
)
.reversal_axis()
.set_global_opts(
legend_opts=opt.LegendOpts(is_show=False),
yaxis_opts=opt.AxisOpts(is_show=True,),
xaxis_opts=opt.AxisOpts(is_show=True),
)
.set_series_opts(
label_opts=opt.LabelOpts(position="right"),
itemstyle_opts={
"normal": {
"color": JsCode(
"""new echarts.graphic.LinearGradient(0, 0, 0, 1, [{offset: 0,color: 'rgba(0, 244, 255, 1)'}, {offset: 1,color: 'rgba(0, 77, 167, 1)'}], false)"""
),
"barBorderRadius": [30, 30, 30, 30],"shadowColor": "rgb(0, 160, 221)",
}
}
)
)
tree = (
TreeMap()
.add(
series_name=year,
data=[{'value':i[1],'name':i[0]} for i in values_data],
visual_min=30,
leaf_depth=1,
# 标签居中为 position = "inside"
width= '12%',
height= '53%',
pos_left = '88%',
pos_top = '47%',
label_opts=opt.LabelOpts(position="inside"),
breadcrumb_opts=opt.TreeMapBreadcrumbOpts(
is_show=False,
pos_left= 'left',
height=50,
),
roam=False,
)
.set_global_opts(
legend_opts=opt.LegendOpts(is_show=False),
title_opts=opt.TitleOpts(
),
)
)
grid_chart =(
Grid()
.add(
bar,
grid_opts=opt.GridOpts(
width= 300,
height= 320
),
)
.add(
tree,
grid_opts=opt.GridOpts(
),
)
.add(
charts,
grid_opts=opt.GridOpts(
),
)
.add(
pie,
grid_opts=opt.GridOpts(
),
)
)
tl.add(
chart=grid_chart,
time_point= f'{year}年',
)
tl.add_schema(
is_auto_play= True,
is_timeline_show=True,
orient="horizontal",
pos_left='left',
symbol_size=[8,5]
)
tl.render('时间线图.html')
tl.render_notebook()
以上汇报完毕,下篇文章再见.(如需文中数据资料请后台私信)
页面更新:2024-04-14
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号