近来,“南丁格尔玫瑰图”因人民日报的疫情播报而大火全网。看惯了饼图,乍一接触这款图表,难免会被它的视觉冲击性所惊叹。因为这两年我也在学习Python,所以现在准备用Python来制作南丁格尔玫瑰图来试一下自己的水平。
先上CSDN查阅资料,借以参考。
案例代码如下:
import pandas as pd
from pyecharts.charts import Pie
from pyecharts import options as opts
# 准备数据
provinces = ['北京','上海','黑龙江','吉林','辽宁','内蒙古','新疆','西藏','青海','四川','云南','陕西','重庆',
'贵州','广西','海南','澳门','湖南','江西','福建','安徽','浙江','江苏','宁夏','山西','河北','天津']
num = [1,1,1,17,9,22,23,42,35,7,20,21,16,24,16,21,37,12,13,14,13,7,22,8,16,13,13]
color_series = ['#FAE927','#E9E416','#C9DA36','#9ECB3C','#6DBC49',
'#37B44E','#3DBA78','#14ADCF','#209AC9','#1E91CA',
'#2C6BA0','#2B55A1','#2D3D8E','#44388E','#6A368B'
'#7D3990','#A63F98','#C31C88','#D52178','#D5225B',
'#D02C2A','#D44C2D','#F57A34','#FA8F2F','#D99D21',
'#CF7B25','#CF7B25','#CF7B25']
# 创建数据框
df = pd.DataFrame({'provinces': provinces, 'num': num})
# 降序排序
df.sort_values(by='num', ascending=False, inplace=True)
# 提取数据
v = df['provinces'].values.tolist()
d = df['num'].values.tolist()
# 实例化Pie类
pie1 = Pie(init_opts=opts.InitOpts(width='1350px', height='750px'))
# 设置颜色
pie1.set_colors(color_series)
# 添加数据,设置饼图的半径,是否展示成南丁格尔图
pie1.add("", [list(z) for z in zip(v, d)],
radius=["30%", "135%"],
center=["50%", "65%"],
rosetype="area"
)
# 设置全局配置项
pie1.set_global_opts(title_opts=opts.TitleOpts(title='玫瑰图示例'),
legend_opts=opts.LegendOpts(is_show=False),
toolbox_opts=opts.ToolboxOpts())
# 设置系列配置项
pie1.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position="inside", font_size=12,
formatter="{b}:{c}天", font_style="italic",
font_weight="bold", font_family="Microsoft YaHei"
),
)
# 生成html文档
# pie1.render('南丁格尔玫瑰图.html')
# 用魔术命令,让玫瑰图在notebook中显示
pie1.render_notebook()
运行结果:
微调案例代码:
import pandas as pd
from pyecharts.charts import Pie
from pyecharts import options as opts
provinces = ['西藏','青海','贵州','江苏','新疆','内蒙古','海南',
'吉林','重庆','山西','安徽','江西','河北','湖南','宁夏','河南','云南','湖北']
num = [51,44,33,31,31,31,30,26,25,25,23,22,22,22,17,9,4,3]
color_series = ['#C9DA36','#9ECB3C','#6DBC49',
'#3DBA78','#14ADCF','#209AC9',
'#2C6BA0','#2D3D8E','#6A368B'
'#7D3990','#A63F98','#C31C88',
'#D5225B','#D02C2A','#D44C2D',
'#D99D21','#CF7B25''#CF7B25']
df = pd.DataFrame({'provinces': provinces, 'num': num})
df.sort_values(by='num', ascending=False, inplace=True)
a = df['provinces'].values.tolist()
b = df['num'].values.tolist()
pie1 = Pie(init_opts=opts.InitOpts(width='1350px', height='750px'))
pie1.set_colors(color_series)
# 添加数据,设置饼图的半径,是否展示成南丁格尔图
pie1.add("", [list(z) for z in zip(a, b)],
radius=["30%", "115%"],
center=["30%", "60%"],
rosetype="area"
)
# 设置全局配置项
pie1.set_global_opts(title_opts=opts.TitleOpts(title='多省区市确诊病例连续零新增'),
legend_opts=opts.LegendOpts(is_show=False),
toolbox_opts=opts.ToolboxOpts())
# 设置系列配置项
pie1.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position="inside", font_size=12,
formatter="{b}:{c}天", font_style="italic",
font_weight="bold", font_family="Microsoft YaHei"
),
)
# pie1.render('a.html')
pie1.render_notebook()
运行结果:
把标签调到外面:
import pandas as pd
from pyecharts.charts import Pie
from pyecharts import options as opts
provinces = ['西藏','青海','贵州','江苏','新疆','内蒙古','海南',
'吉林','重庆','山西','安徽','江西','河北','湖南','宁夏','河南','云南','湖北']
num = [51,44,33,31,31,31,30,26,25,25,23,22,22,22,17,9,4,3]
color_series = ['#C9DA36','#9ECB3C','#6DBC49',
'#3DBA78','#14ADCF','#209AC9',
'#2C6BA0','#2D3D8E','#6A368B'
'#7D3990','#A63F98','#C31C88',
'#D5225B','#D02C2A','#D44C2D',
'#D99D21','#CF7B25''#CF7B25']
df = pd.DataFrame({'provinces': provinces, 'num': num})
df.sort_values(by='num', ascending=False, inplace=True)
a = df['provinces'].values.tolist()
b = df['num'].values.tolist()
pie1 = Pie(init_opts=opts.InitOpts(width='1350px', height='750px'))
pie1.set_colors(color_series)
# 添加数据,设置饼图的半径,是否展示成南丁格尔图
pie1.add("", [list(z) for z in zip(a, b)],
radius=["15%", "85%"],
center=["30%", "60%"],
rosetype="area"
)
# 设置全局配置项
pie1.set_global_opts(title_opts=opts.TitleOpts(title='多省区市确诊病例连续零新增'),
legend_opts=opts.LegendOpts(is_show=False),
toolbox_opts=opts.ToolboxOpts())
# 设置系列配置项,改成标签在外
pie1.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position="outside", font_size=14,
formatter="{b}:{c}天", font_style="italic",
font_weight="bold", font_family="Microsoft YaHei"
),
)
# pie1.render('a.html')
pie1.render_notebook()
运行结果:
标签外置:
import pandas as pd
from pyecharts.charts import Pie
from pyecharts import options as opts
provinces = ['西藏','青海','贵州','江苏','新疆','内蒙古','海南',
'吉林','重庆','山西','安徽','江西','河北','湖南','宁夏','河南','云南','湖北']
num = [51,44,33,31,31,31,30,26,25,25,23,22,22,22,17,9,4,3]
color_series = ['#C9DA36','#9ECB3C','#6DBC49',
'#3DBA78','#14ADCF','#209AC9',
'#2C6BA0','#2D3D8E','#6A368B'
'#7D3990','#A63F98','#C31C88',
'#D5225B','#D02C2A','#D44C2D',
'#D99D21','#CF7B25''#CF7B25']
df = pd.DataFrame({'provinces': provinces, 'num': num})
df.sort_values(by='num', ascending=False, inplace=True)
a = df['provinces'].values.tolist()
b = df['num'].values.tolist()
pie1 = Pie(init_opts=opts.InitOpts(width='1350px', height='750px'))
pie1.set_colors(color_series)
# 添加数据,设置饼图的半径,是否展示成南丁格尔图
pie1.add("", [list(z) for z in zip(a, b)],
radius=["10%", "100%"],
center=["35%", "60%"],
rosetype="area"
)
# 设置全局配置项
pie1.set_global_opts(title_opts=opts.TitleOpts(title='多省区市确诊病例连续零新增'),
legend_opts=opts.LegendOpts(is_show=False),
toolbox_opts=opts.ToolboxOpts())
# 设置系列配置项
pie1.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position="outside", font_size=14,
formatter="{b}:{c}天", font_style="italic",
font_weight="bold", font_family="Microsoft YaHei"
),
)
# pie1.render('a.html')
pie1.render_notebook()
把参数替换掉,应用到自己的实践工作中:
代码修改后如下:
注:numpy里面的内容,可以打开Excel的剪切板……
import pandas as pd
from pyecharts.charts import Pie
from pyecharts import options as opts
provinces = ["长鲜鱿(盒)","海瓜子","甜玉米","茼蒿苗","紫茄","凤梨","进口香蕉","花甲肉","南鲳鱼(盒)","辣椒苗",
"吊筒(盒)","耙耙柑","白鲳(盒)","冬瓜","水库鳙鱼片","鲍鱼"]
num = [1,1,1,1,1,1,1,1,1,1,1,1,2,2,3,4]
color_series = ['#C9DA36','#9ECB3C','#6DBC49',
'#3DBA78','#14ADCF','#209AC9',
'#2C6BA0','#2D3D8E','#6A368B'
'#7D3990','#A63F98','#C31C88',
'#D5225B','#D02C2A','#D44C2D',
'#D99D21','#CF7B25''#CF7B25']
df = pd.DataFrame({'provinces': provinces, 'num': num})
df.sort_values(by='num', ascending=False, inplace=True)
a = df['provinces'].values.tolist()
b = df['num'].values.tolist()
pie1 = Pie(init_opts=opts.InitOpts(width='1350px', height='750px'))
pie1.set_colors(color_series)
# 添加数据,设置饼图的半径,是否展示成南丁格尔图
pie1.add("", [list(z) for z in zip(a, b)],
radius=["20%", "140%"],
center=["30%", "70%"],
rosetype="area"
)
# 设置全局配置项
'''
pie1.set_global_opts(title_opts=opts.TitleOpts(title="5月份第2周打折验收单品次数"),
legend_opts=opts.LegendOpts(is_show=False),
toolbox_opts=opts.ToolboxOpts())
'''
pie1.set_global_opts(title_opts=opts.TitleOpts(title="5月份第2周打折验收单品次数"),
legend_opts=opts.LegendOpts(is_show=False))
# 设置系列配置项
pie1.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position="inside", font_size=12,
formatter="{b}:{c}次", font_style="italic",
font_weight="bold", font_family="Microsoft YaHei"
),
)
# pie1.render('a.html')
pie1.render_notebook()
标签向外:
import pandas as pd
from pyecharts.charts import Pie
from pyecharts import options as opts
provinces = ["长鲜鱿(盒)","海瓜子","甜玉米","茼蒿苗","紫茄","凤梨","进口香蕉","花甲肉","南鲳鱼(盒)","辣椒苗",
"吊筒(盒)","耙耙柑","白鲳(盒)","冬瓜","水库鳙鱼片","鲍鱼"]
num = [1,1,1,1,1,1,1,1,1,1,1,1,2,2,3,4]
color_series = ['#C9DA36','#9ECB3C','#6DBC49',
'#3DBA78','#14ADCF','#209AC9',
'#2C6BA0','#2D3D8E','#6A368B'
'#7D3990','#A63F98','#C31C88',
'#D5225B','#D02C2A','#D44C2D',
'#D99D21','#CF7B25''#CF7B25']
df = pd.DataFrame({'provinces': provinces, 'num': num})
df.sort_values(by='num', ascending=False, inplace=True)
a = df['provinces'].values.tolist()
b = df['num'].values.tolist()
pie1 = Pie(init_opts=opts.InitOpts(width='1350px', height='750px'))
pie1.set_colors(color_series)
# 添加数据,设置饼图的半径,是否展示成南丁格尔图
pie1.add("", [list(z) for z in zip(a, b)],
radius=["20%", "105%"],
center=["40%", "60%"],
rosetype="area"
)
# 设置全局配置项
'''
pie1.set_global_opts(title_opts=opts.TitleOpts(title="5月份第2周打折验收单品次数"),
legend_opts=opts.LegendOpts(is_show=False),
toolbox_opts=opts.ToolboxOpts())
'''
pie1.set_global_opts(title_opts=opts.TitleOpts(title="5月份第2周打折验收单品次数"),
legend_opts=opts.LegendOpts(is_show=False))
# 设置系列配置项
pie1.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position="outside", font_size=12,
formatter="{b}:{c}次", font_style="italic",
font_weight="bold", font_family="Microsoft YaHei"
),
)
# pie1.render('a.html')
pie1.render_notebook()
小结:玩了半个月的pyecharts,越改越顺手,越玩越爽,都有一种不用学Power bi的错觉了!
来源:oschina
链接:https://my.oschina.net/u/3750423/blog/4276553