import matplotlib.pyplot as plt
import pandas as pd
import matplotlib
data = pd.Series([95,261,105,30,9],index =["五星","四星","三星","二星","一星"])
data
# 设置图片大小和分辨率
fig=plt.figure(figsize=(5,5), dpi=100)
# 设置饼图标签
labels = data.index
# 设置饼图颜色
colors = ['#9ACD32','#EEAD0E','#EE7942','#483D8B','#EEB422']
# 偏移位置or偏离圆心的距离(这里使“四星”数据偏移)
explode = (0, 0.1, 0, 0, 0)
# 绘制饼图:autopct显示百分比,这里保留一位小数;shadow控制是否显示阴影
data.plot(kind="pie", explode=explode, labels=labels, autopct='%0.1f%%', colors=colors, shadow=True)
# 解决中文字体乱码的问题
matplotlib.rcParams['font.sans-serif']=['simsun']
#使饼图长宽相等,确保饼图被画成圆形。
plt.axis('equal')
# 保存图片
plt.savefig("./饼图.jpg")
fig=plt.figure(figsize=(5,5),dpi=100)
#数据集,x1, x2分别对应内部、外部百分比例
x1=[7,22,14,5,14,6,32]
x2=[43,25,32]
#设置饼状图各个区块的颜色
inner_color=['#ffce2b','#00c5d2', '#ff812c']
outer_color=['#9ACD32','#836FFF','#1E90FF']
labels = ['标签1','标签2','标签3']
# 绘制内部饼图:autopct控制显示百分比,radius控制半径,pctdistance控制数据标签到圆心位置的距离
plt.pie(x1, autopct='%0.1f%%', radius=0.7, pctdistance=0.7,colors=inner_color,wedgeprops=dict(linewidth=3,width=0.4,edgecolor='w'))
# 绘制外部饼图:wedgeprops以字典形式传参,linewidth控制间隔线条的宽度,width控制饼图宽度(不超过半径),edgecolor控制间隔线条的颜色(此处为白色)
plt.pie(x2, autopct='%0.1f%%', radius=1, pctdistance=0.8, colors=outer_color, wedgeprops=dict(linewidth=3,width=0.4,edgecolor='w'), labels=labels)
# 图例文字
legend_text=['子标签1','子标签2','子标签3']
# 设置图例标题、位置,frameon控制是否显示图例边框,bbox_to_anchor控制图例显示在饼图的外面
plt.legend(legend_text,title='主题占比', loc='upper right', frameon=False, bbox_to_anchor=(0.75, 0, 0.4, 1))
#使饼图长宽相等,确保饼图被画成圆形。
plt.axis('equal')
# 保存图片
plt.savefig("./嵌套饼图.png")
# 导入相应模块
from pyecharts import options as opts
from pyecharts.charts import Pie
from snapshot_selenium import snapshot as driver
from pyecharts.render import make_snapshot
# 设置数据
num = [95, 261, 105, 30, 9]
rank = ["五星", "四星", "三星", "二星", "一星"]
# 绘制饼图
pie = Pie(init_opts=opts.InitOpts())
# 添加数据
pie.add("", [list(z) for z in zip(rank, num)])
# 设置颜色
pie.set_colors(['#9ACD32', '#EEAD0E', '#EE7942', '#483D8B', '#EEB422'])
# 设置标题及标题位置
pie.set_global_opts(title_opts=opts.TitleOpts(title="简洁饼图", pos_left="10%", pos_right="90%"))
# 控制图例显示为垂直格式,也可在set_global_opts里面添加:legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical"
# 设置标签显示格式
pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
import pyecharts.options as opts
from pyecharts.charts import Pie
# 设置内层数据
inner_x_data = ["标签1", "标签2", "标签3"]
inner_y_data = [335, 679, 1548]
inner_data_pair = [list(z) for z in zip(inner_x_data, inner_y_data)]
# 设置外层数据
outer_x_data = ["子标签1", "子标签2", "子标签3", "子标签4", "子标签5", "子标签6", "子标签7", "子标签8"]
outer_y_data = [335, 310, 234, 135, 1048, 251, 147, 102]
outer_data_pair = [list(z) for z in zip(outer_x_data, outer_y_data)]
# 绘制饼图
pie = Pie(init_opts=opts.InitOpts(width="1600px", height="800px"))
# 设置颜色
pie.set_colors(['#FF00FF','#EEAD0E','#EE7942','#483D8B','#EEB422','#C0FF3E','#FFC1C1','#FF6A6A','#98FB98','#87CEFA','#8470FF'])
pie.add(series_name="内层饼图",
# 添加内层数据
data_pair=inner_data_pair,
# 内层饼图半径
radius=[0, "30%"],
# 控制标签显示在饼图内部
label_opts=opts.LabelOpts(position="inner"))
pie.add(series_name="外层饼图",
# 外层饼图半径
radius=["40%", "55%"],
# 添加外层数据
data_pair=outer_data_pair,
label_opts=opts.LabelOpts(
# 控制标签显示在饼图外部
position="outside",
# 设置标签显示格式
formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{d}%} ",
# 设置背景颜色,此处为白色
background_color="#FFFFFF",
# 设置边框颜色,此处为蓝色
border_color="#0000FF",
# 设置边框宽度
border_width=1,
border_radius=4,
rich={
# 控制series_name文字的颜色(此处为红色)、行间距、对其方式
"a": {"color": "#FF0000", "lineHeight": 22, "align": "center"},
# 控制series_name文字部分的填充色(此处为黄色)、宽度、对其方式、填充高度、绘图边角的弧度(这里左上角和右上角的弧度为4)
"abg": {
"backgroundColor": "#FFFF00",
"width": "100%",
"align": "right",
"height": 22,
"borderRadius": [4, 4, 0, 0],
},
# 控制边框分割线的颜色(此处为红色)、绘制范围、边框宽度、边框高度
"hr": {
"borderColor": "#ff1a00",
"width": "100%",
"borderWidth": 0.5,
"height": 0,
},
# 控制子标签文字的大小和行间距
"b": {"fontSize": 16, "lineHeight": 33},
# 控制百分比的颜色(此处为绿色)、背景填充色(此处为深蓝色)、填充范围、边角弧度
"per": {
"color": "#00FF00",
"backgroundColor": "#191970",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
# 设置标签显示在左侧,且垂直分布
pie.set_global_opts(legend_opts=opts.LegendOpts(pos_left="20%", orient="vertical"))
# 设置标签显示格式
pie.set_series_opts(tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a}
{b}: {c} ({d}%)"))
# 保存图片
make_snapshot(driver, pie.render(),"Pyecharts嵌套饼图.png")
# 或者保存为HTML格式
pie.render("Pyecharts嵌套饼图.html")
输出结果:
希望大家可以通过 点赞 的方式表达对本文的支持!后续会有更多精美干货送上!
感谢大家!!!