Python怎么用matplotlib画图,想要显示百分比和数值怎么做?请在以下代码中补充
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams["font.family"] = ["simhei"]
data = {"1星": 895, "2星": 187, "3星": 920, "4星": 986, "5星": 100495}
data = pd.DataFrame([data])
# 绘制圆环图,并返回饼块对象
wedges, texts = plt.pie(data.iloc[0], wedgeprops={"width": 0.5})
# 构造annotate函数的**kwargs参数,设置引导线线型
kw = dict(arrowprops=dict(arrowstyle="-"), zorder=0, va="center")
# 遍历饼块绘制注释标签和引导线
for i, p in enumerate(wedges):
# 根据matplotlib.patches.Wedge对象的theta1和theta2参数计算饼块均分点的角度
ang = (p.theta2 - p.theta1) / 2.0 + p.theta1
# 根据角度的弧度计算 饼块均分点的坐标(引导线的起点)
y = np.sin(np.deg2rad(ang))
x = np.cos(np.deg2rad(ang))
# print(p.theta1, p.theta2, ang, np.deg2rad(ang), x, y)
# 演示引导线起点位置
# plt.plot(x, y, "or")
# 根据x的值即角度所在象限确定引导线的对齐方式
horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
# 设置引导线的连接方式
connectionstyle = "angle,angleA=0,angleB={}".format(ang)
kw["arrowprops"].update({"connectionstyle": connectionstyle})
# 绘制注释标签和引导线
plt.annotate(
data.columns[i],
xy=(x, y),
xytext=(1.35 * np.sign(x), 1.4 * y),
horizontalalignment=horizontalalignment,
**kw
)
plt.title("用户评分等级占比")
plt.show()
修改成功啦,但是数据重叠怎么办?