Pyecharts-饼图

饼图

饼图

导入包

1
2
3
4
5
6
import os
from pyecharts.globals import ThemeType
from pyecharts.charts import Pie
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

加载数据

1
2
3
4
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
v1 = [11, 12, 13, 10, 10, 10]

print(list(zip(attr, v1)))

这里zip()将对象打包成一个元组,list()转为列表

另外一种处理数据的方法:

1
2
3
4
5
6
7
data_pair = []
for x, y, c in zip(attr, v1, ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc']):
data_pair.append(opts.PieItem(
name=x,
value=y,
itemstyle_opts=opts.ItemStyleOpts(color=c)
))

开始做图

1
2
3
4
5
6
7
8
9
pie = (Pie(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))
.add("服装", list(zip(attr, v1)), # 用第二种方法时,数据位置改为data_pair
radius=[40, 200],
rosetype="radius",
is_clockwise=True)
)

pie.render("饼图.html")
os.system("饼图.html")

添加标题

1
.set_global_opts(title_opts=opts.TitleOpts("服装销量", pos_left="10%"))

图例位置

1
.set_global_opts(legend_opts=opts.LegendOpts(pos_right="10%", orient="vertical"))

设置颜色

1
.set_colors(["#5470c6", "#91cc75"," #fac858", "#ee6666", "#73c0de", "#3ba272", "#fc8452", "#9a60b4"])

使用第二种方法时,不需要设置颜色,因为数据中包含了颜色

渲染图片

1
make_snapshot(snapshot, pie.render(), "饼图.png")

大功告成!

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2022-2023 发现美的眼睛
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信