matplotlib饼图用主图的实际标题替换最后一个饼图子图标题[重复]
Posted
技术标签:
【中文标题】matplotlib饼图用主图的实际标题替换最后一个饼图子图标题[重复]【英文标题】:matplotlib pie chart replacing last pie subplot title with actual title of the main plot [duplicate] 【发布时间】:2021-11-16 09:27:22 【问题描述】:我遇到了一个问题,matplotlib pie 正在用实际的情节标题替换我最后一个子情节文本的值。谁能告诉我为什么我会遇到这个问题以及如何克服它?
keep_list_tt = [6.012, 2.734, 2.76, 4.585, 4.19]
eppm_plot = [0, 771, 830, 919, 1097]
all_test_tt = [31.344, 31.344, 31.344, 31.344, 31.344]
always_failing_list_tt = [0.112, 0.112, 0.112, 0.112, 0.112]
always_failing_list_tt = [23.203, 23.203, 23.203, 23.203, 23.203]
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1,len(keep_list_tt), figsize=(30,8))
labels = ["always_failing_list_tt", "always_passing_list_tt", "keep_list_tt","remove_list_tt"]
explode = (0.2, 0,0.1,0.1)
for i in range(len(keep_list_tt)):
eppm_pie_plot = [always_failing_list_tt[i],always_passing_list_tt[i],keep_list_tt[i],round(np.subtract(all_test_tt[i],np.add(keep_list_tt[i],np.add(always_failing_list_tt[i],always_passing_list_tt[i]))),3)]
pie = axs[i].pie(eppm_pie_plot, explode=explode,autopct="")
axs[i].set_title("EPPM is - ".format(eppm_plot[i]))
for j, a in enumerate(pie[2]):
a.set_text("".format(eppm_pie_plot[j]))
plt.legend(labels,bbox_to_anchor=(1.1,0.5) )
plt.axis('equal')
plt.title ("Temp")
plt.show()
【问题讨论】:
【参考方案1】:这个问题似乎是由于plt.axis('equal')
仅应用于循环过程之外的最后一个图。请将该代码移到循环内。当你在做的时候,优化图表的循环方式。
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1,len(keep_list_tt), figsize=(30,8))
labels = ["always_failing_list_tt", "always_passing_list_tt", "keep_list_tt","remove_list_tt"]
explode = (0.2, 0,0.1,0.1)
for i, ax in enumerate(fig.axes):
eppm_pie_plot = [always_failing_list_tt[i],always_passing_list_tt[i],keep_list_tt[i],round(np.subtract(all_test_tt[i],np.add(keep_list_tt[i],np.add(always_failing_list_tt[i],always_passing_list_tt[i]))),3)]
pie = ax.pie(eppm_pie_plot, explode=explode, autopct="")
ax.set_title("EPPM is - ".format(eppm_plot[i]))
ax.axis('equal')
for j, a in enumerate(pie[2]):
a.set_text("".format(eppm_pie_plot[j]))
plt.legend(labels, bbox_to_anchor=(1.1,0.5))
#plt.axis('equal')
fig.suptitle("Temp")
plt.show()
【讨论】:
感谢@r-beginners 的帮助。将优化我的循环 这是一个重复的问题,但可以接受,如果对您有帮助,请接受我的回答。 谢谢,我接受了答案,因为它解决了我应该在循环之外的 axis=equal 的特殊问题以上是关于matplotlib饼图用主图的实际标题替换最后一个饼图子图标题[重复]的主要内容,如果未能解决你的问题,请参考以下文章
Python Matplotlib饼图将两个标题相同的切片合并在一起