- 南丁格尔玫瑰图
from pyecharts.charts import Pie
from pyecharts import options as opts
import random
# 随机颜色生成
# def randomcolor(kind):
# colors = []
# for i in range(kind):
# colArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
# color = ""
# for i in range(6):
# color += colArr[random.randint(0, 14)]
# colors.append("#" + color)
# return colors
# 数据
#provinces = ['西藏', '青海', '贵州', '江苏']
#num = [51, 44, 33, 31]
#color_series = randomcolor(len(provinces))
levels = ['数据层 3049', '应用层 846', '传输层 642', '感知层 209']
num = [64.7, 17.9, 13.5, 4.4]
color_series = ['FF8785', '6CDADA', 'FF99FF', '7EAEF1']
#color_series = ['FF99FF', '7EAEF1', 'FF8785', '6CDADA']
# 创建饼图
fig = Pie(init_opts=opts.InitOpts(width='800px', height='800px'))
# 添加数据
fig.add("", [list(z) for z in zip(levels, num)],
radius=['35%', '80%'],
center=['45%', '55%'], # radius和center需要根据len(levels)调节
rosetype='area')
# 设置全局配置
fig.set_global_opts(title_opts=opts.TitleOpts(title=''),
legend_opts=opts.LegendOpts(is_show=False)
)
# 设置系列配置和颜色
fig.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position='inside', font_size=23,
formatter='{b}\n{c}%', font_style='normal', font_weight='bold',
font_family='Microsoft YaHei')) # b:province;c:num
fig.set_colors(color_series)
# 在网页生成照片
fig.render('chart1.html')
效果:
- 水平堆叠条形图
# 大类与层次
# 柱状堆叠图
import pyecharts.options as opts
from pyecharts.charts import Bar
import random
goods = ['物联网服务业', '物联网制造业']
chuan = [600, 46]
shu = [3007, 46]
ying = [846]
gan = ["", 209]
bar = (
Bar()
.add_xaxis(goods)
.add_yaxis('传输层', chuan, stack='stack1', color='FF8785')
.add_yaxis('数据层', shu, stack='stack1', color='6CDADA')
.add_yaxis('感知层', ying, stack='stack1', color='FF99FF')
.add_yaxis('应用层', gan, stack='stack1', color='7EAEF1')
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position="insideRight", font_size=12,
font_style='normal', font_weight='bold',
font_family='Microsoft YaHei'))
.set_global_opts(title_opts=opts.TitleOpts(title=''),
xaxis_opts=opts.AxisOpts(name='企业数量'),
yaxis_opts=opts.AxisOpts(name='物联网大类'))
)
bar.render('chart2.html')
效果: