在Pyplot饼图的子图之间的ConnectionPatch()或pyplot.arrow

时间:2017-05-19 01:28:28

标签: python matplotlib pie-chart

我想用箭头连接这些饼图。我了解了ConnectionPatch()函数,但它需要坐标轴和饼图没有轴,是吗?

import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec


labels = 'Good', 'Poor', 'Mediocre'
sizes = [124, 205, 133]

gs = gridspec.GridSpec(2,5)

def make_autopct(sizes):
    def my_autopct(pct):
        total = sum(sizes)
        val = int(round(pct*total/100.0))
        return '{p:.1f}%({v:d})'.format(p=pct,v=val)
    return my_autopct

plt.figure(figsize=(15,8))
plt.subplot(gs[:,2])
plt.axis('equal')
piechart = plt.pie(sizes, radius=3, labels=labels, labeldistance=1.1, textprops={'backgroundcolor':'w', 'fontsize':12}, autopct=make_autopct(sizes), 
pctdistance=0.5, shadow=False, colors='w')[0]
piechart[0].set_hatch('\\\\\\\\\\')
piechart[1].set_hatch('xxxx')
piechart[2].set_hatch('+++')
mpl.rcParams['font.size'] = 20


poor = [201, 25]
mediocre = [97, 32]
good = [83, 30]
label_small = "Private","Government"

plt.subplot(gs[0,0])
plt.axis('equal')
pie_poor = plt.pie(poor, labels=label_small, autopct='%1.1f%%', 
pctdistance=1.5, textprops={'fontsize':8},  shadow=False, colors='w', startangle=0)[0]
pie_poor[0].set_hatch('//////')
pie_poor[1].set_hatch('....')

plt.subplot(gs[0,4])
plt.axis('equal')
pie_good = plt.pie(good, labels=label_small, autopct='%1.1f%%', pctdistance=1.4, textprops={'fontsize':8},  shadow=False, colors='w', startangle=270)[0]
pie_good[0].set_hatch('//////')
pie_good[1].set_hatch('....')

plt.subplot(gs[1,4])
plt.axis('equal')
pie_mediocre = plt.pie(mediocre, labels=label_small, autopct='%1.1f%%', pctdistance=1.3, textprops={'fontsize':8},  shadow=False, colors='w', startangle=180)[0]
pie_mediocre[0].set_hatch('//////')
pie_mediocre[1].set_hatch('....')

plt.draw()

[Output]

我想连接标有"差"的大楔子?到了左边较小的馅饼,"好"到了右上方较小的馅饼,以及“平庸的”#34;带箭头的右下方较小的馅饼。

1 个答案:

答案 0 :(得分:1)

matplotlib中的每个情节都生活在一个轴上。要获得轴的控制,请使用 ax = plt.subplot(..)。然后,您可以使用ConnectionPatch连接轴。

enter image description here

完整代码:

import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np


labels = 'Good', 'Poor', 'Mediocre'
sizes = [124, 205, 133]

gs = gridspec.GridSpec(2,5)

def make_autopct(sizes):
    def my_autopct(pct):
        total = sum(sizes)
        val = int(round(pct*total/100.0))
        return '{p:.1f}%({v:d})'.format(p=pct,v=val)
    return my_autopct

plt.figure(figsize=(15,8))
ax = plt.subplot(gs[:,2])
plt.axis('equal')
piechart = plt.pie(sizes, radius=3, labels=labels, labeldistance=1.1, textprops={'backgroundcolor':'w', 'fontsize':12}, autopct=make_autopct(sizes), 
pctdistance=0.5, shadow=False, colors='w')[0]
piechart[0].set_hatch('\\\\\\\\\\')
piechart[1].set_hatch('xxxx')
piechart[2].set_hatch('+++')
mpl.rcParams['font.size'] = 20


poor = [201, 25]
mediocre = [97, 32]
good = [83, 30]
label_small = "Private","Government"

ax1 = plt.subplot(gs[0,0])
plt.axis('equal')
pie_poor = plt.pie(poor, labels=label_small, autopct='%1.1f%%', 
pctdistance=1.5, textprops={'fontsize':8},  shadow=False, colors='w', startangle=0)[0]
pie_poor[0].set_hatch('//////')
pie_poor[1].set_hatch('....')

ax2 = plt.subplot(gs[0,4])
plt.axis('equal')
pie_good = plt.pie(good, labels=label_small, autopct='%1.1f%%', pctdistance=1.4, textprops={'fontsize':8},  shadow=False, colors='w', startangle=270)[0]
pie_good[0].set_hatch('//////')
pie_good[1].set_hatch('....')

ax3 = plt.subplot(gs[1,4])
plt.axis('equal')
pie_mediocre = plt.pie(mediocre, labels=label_small, autopct='%1.1f%%', pctdistance=1.3, textprops={'fontsize':8},  shadow=False, colors='w', startangle=180)[0]
pie_mediocre[0].set_hatch('//////')
pie_mediocre[1].set_hatch('....')

from matplotlib.patches import ConnectionPatch
xy = (1, 1)
con = ConnectionPatch(xyA=(0,0), xyB=xy, coordsA="data", coordsB="data", 
                      axesA=ax2, axesB=ax, color="crimson", lw=3)
ax2.add_artist(con)
xy = (-1, 0)
con = ConnectionPatch(xyA=(0,0), xyB=xy, coordsA="data", coordsB="data", 
                      axesA=ax1, axesB=ax, color="darkblue", lw=3)
ax1.add_artist(con)
xy = (1, -1)
con = ConnectionPatch(xyA=(0,0), xyB=xy, coordsA="data", coordsB="data", 
                      axesA=ax3, axesB=ax, color="gold", lw=3)
ax3.add_artist(con)

plt.show()