甜甜圈图并排

时间:2019-01-16 04:25:35

标签: python matplotlib charts data-visualization

我想并排制作两个甜甜圈图, 目前,我并排有一个饼图和一个甜甜圈图。

甜甜圈图

这是我的代码,我需要循环吗?

recipe = "flour","sugar","egg","butter","milk","yeast"
fracs = [15, 30, 45, 10]
data = [225, 90, 50, 60, 100, 5]

fig = plt.figure()

ax1 = fig.add_axes([0, 0, .5, .5], aspect=1)
ax1.pie(data, labels=recipe, radius = 1.2)
ax2 = fig.add_axes([.5, .0, .5, .5], aspect=1)
ax2.pie(data, labels=recipe, radius = 1.2)
circle = plt.Circle((0,0), 0.7, color='white')
p=plt.gcf()
p.gca().add_artist(circle)
ax1.set_title('A donut', y=1.1)
ax2.set_title('A donut',y=1.1)
plt.savefig('donut1.png')
plt.show()

2 个答案:

答案 0 :(得分:0)

您可以使用wedgeprops中的pie参数来定义甜甜圈。

inner_radius = 0.7
chart_radius = 1.2
wedge_props = dict(width=chart_radius-inner_radius)

ax1.pie(data, labels=recipe, radius=chart_radius, wedgeprops=wedge_props)
ax2.pie(data, labels=recipe, radius=chart_radius, wedgeprops=wedge_props)

答案 1 :(得分:0)

是的,您可以像CristianoKöhler在另一个答案中建议的那样,使用wedgeprops使用循环+来做到这一点:

import matplotlib.pyplot as plt

recipe = "flour", "sugar", "egg", "butter", "milk", "yeast"
fracs = [15, 30, 45, 10]
data = [225, 90, 50, 60, 100, 5]

fig = plt.figure()

for i in range(2):
    ax = fig.add_axes([i/2, .1, .5, .5], aspect=1)
    ax.pie(data, labels=recipe, radius=1.2, wedgeprops={'width': 0.5})
    ax.set_title('A donut {}'.format(i+1), y=1.1)
    plt.text(0, -1.6, 'Description {}'.format(i+1), ha='center')

plt.savefig('donut1.png')
plt.show()

enter image description here