1 回答
TA贡献1862条经验 获得超6个赞
注意:虽然它不是您目标的完美再现,但我认为它已经足够接近您可以微调到您想要的结果。
我曾经gridspec
创建两个单独的子图(pie_chart
和title
),添加了自定义注释行(改编自文档),并将子图格式化title
为黑色,没有任何可见的刻度/刺。
绘图结果:
使用的完整代码:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import gridspec
custpref=pd.DataFrame({'tov_type':['Inpatient','Office Visit','Appointment Schedule','Allergy Sheet'],'count':[7,6,1,1]})
fig=plt.figure(figsize=(6,4))
gs1 = gridspec.GridSpec(1,1,
left=0.1,right=0.7,
bottom=0.1,top=0.7,
)
gs2 = gridspec.GridSpec(1,1,
left=0.05,right=0.95,
bottom=0.9,top=1.0,
)
pie_ax=fig.add_subplot(gs1[0])
title_ax=fig.add_subplot(gs2[0])
# Create a list of colors (from iWantHue)
colors = ["#6287da","#72ac5c","#8653aa","#bb7438","#b94b75"]
# Create a pie chart
wedges, texts = pie_ax.pie(
# using data total)arrests
custpref['count'],
# with no shadows
shadow=False,
# with colors
colors=colors,
# with the start angle at 90%
startangle=90,
)
bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
kw = dict(xycoords='data', textcoords='data', arrowprops=dict(arrowstyle="-"), zorder=0, va="center")
for i, p in enumerate(wedges):
ang = (p.theta2 - p.theta1)/2. + p.theta1
y = np.sin(np.deg2rad(ang))
x = np.cos(np.deg2rad(ang))
horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
connectionstyle = "angle,angleA=0,angleB={}".format(ang)
kw["arrowprops"].update({"connectionstyle": connectionstyle,"color":colors[i]})
pie_ax.annotate(custpref['tov_type'][i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
horizontalalignment=horizontalalignment, **kw)
# View the plot drop above
pie_ax.axis('equal')
title_ax.set_facecolor('k')
title_ax.text(0.5,0.5,"Top 5 Visit Types Total = 15 Visits",
ha="center",va="center",transform=title_ax.transAxes,color="w")
for side in ['top', 'bottom', 'left', 'right']:
title_ax.spines[side].set_visible(False)
title_ax.axes.get_xaxis().set_visible(False)
title_ax.axes.get_yaxis().set_visible(False)
plt.savefig(r"PieChart.png",bbox_inches="tight")
plt.show()
添加回答
举报