在特定坐标处将多个图放置到一个大轴上

时间:2018-11-19 21:29:50

标签: python matplotlib subplot

我正在尝试将多个matplotlib子图放入一个大轴中,其中大轴上的刻度标签对应于一些参数值,已为其获取每个子图中的数据。这是一个例子,

import matplotlib.pyplot as plt
data = {}
data[(10, 10)] = [0.45, 0.30, 0.25]
data[(10, 20)] = [0.2, 0.5, 0.3]
data[(20, 10)] = [0.1, 0.3, 0.6]
data[(20, 20)] = [0.6, 0.15, 0.25]
data[(30, 10)] = [0.4, 0.35, 0.25]
data[(30, 20)] = [0.5, 0.1, 0.4]

# x and y coordinates for the big plot
x_coords = list(set([k[0] for k in data.keys()]))
y_coords = list(set([k[1] for k in data.keys()]))

labels = ['Frogs', 'Hogs', 'Dogs']
explode = (0.05, 0.05, 0.05)  #
colors = ['gold', 'beige', 'lightcoral']

fig, axes = plt.subplots(len(y_coords), len(x_coords))

for row_topToDown in range(len(y_coords)):
    row = (len(y_coords)-1) - row_topToDown
    for col in range(len(x_coords)):
        axes[row][col].pie(data[(x_coords[col], y_coords[row_topToDown])], explode=explode, colors = colors, \
        autopct=None, pctdistance = 1.4, \
        shadow=True, startangle=90, radius=0.7, \
        wedgeprops = {'linewidth':1, 'edgecolor':'Black'}
                                     )
        axes[row][col].axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
        axes[row][col].set_title('(' + str(x_coords[col]) + ', ' + str(y_coords[row_topToDown]) + ')')

fig.tight_layout()        
plt.show()

,这就是我希望输出看起来像的样子: enter image description here

1 个答案:

答案 0 :(得分:2)

我看到两个选择:

A。使用单轴

您可以将所有饼图绘制到相同的轴上。使用centerradius参数在数据坐标中缩放饼图。可能如下所示。

import matplotlib.pyplot as plt
data = {}
data[(10, 10)] = [0.45, 0.30, 0.25]
data[(10, 20)] = [0.2, 0.5, 0.3]
data[(20, 10)] = [0.1, 0.3, 0.6]
data[(20, 20)] = [0.6, 0.15, 0.25]
data[(30, 10)] = [0.4, 0.35, 0.25]
data[(30, 20)] = [0.5, 0.1, 0.4]

labels = ['Frogs', 'Hogs', 'Dogs']
explode = [.2]*3
colors = ['gold', 'beige', 'lightcoral']
radius = 4
margin = 2

fig, ax = plt.subplots()

for x,y in data.keys():
    d = data[(x,y)]
    ax.pie(d, explode=explode, colors = colors, center=(x,y), 
            shadow=True, startangle=90, radius=radius, 
            wedgeprops = {'linewidth':1, 'edgecolor':'Black'})

    ax.annotate("({},{})".format(x,y), xy = (x, y+radius), 
                xytext = (0,5), textcoords="offset points", ha="center")

ax.set_frame_on(True)
xaxis = list(set([x for x,y in data.keys()]))
yaxis = list(set([y for x,y in data.keys()]))
ax.set(aspect="equal", 
       xlim=(min(xaxis)-radius-margin,max(xaxis)+radius+margin), 
       ylim=(min(yaxis)-radius-margin,max(yaxis)+radius+margin), 
       xticks=xaxis, yticks=yaxis)
fig.tight_layout()        
plt.show()

enter image description here

B。使用插入轴

您可以将每个饼图放置在其各自的轴中,并将轴放置在数据坐标中。使用mpl_toolkits.axes_grid1.inset_locator.inset_axes可简化此操作。与上述内容的主要区别在于,您可以使用父轴的不相等方面,并且无法使用tight_layout

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

data = {}
data[(10, 10)] = [0.45, 0.30, 0.25]
data[(10, 20)] = [0.2, 0.5, 0.3]
data[(20, 10)] = [0.1, 0.3, 0.6]
data[(20, 20)] = [0.6, 0.15, 0.25]
data[(30, 10)] = [0.4, 0.35, 0.25]
data[(30, 20)] = [0.5, 0.1, 0.4]


labels = ['Frogs', 'Hogs', 'Dogs']
explode = [.05]*3
colors = ['gold', 'beige', 'lightcoral']
radius = 4
margin = 2

fig, axes = plt.subplots()

for x,y in data.keys():
    d = data[(x,y)]
    ax = inset_axes(axes, "100%", "100%", 
                    bbox_to_anchor=(x-radius, y-radius, radius*2, radius*2),
                    bbox_transform=axes.transData, loc="center")
    ax.pie(d, explode=explode, colors = colors,
            shadow=True, startangle=90,
            wedgeprops = {'linewidth':1, 'edgecolor':'Black'})

    ax.set_title("({},{})".format(x,y))


xaxis = list(set([x for x,y in data.keys()]))
yaxis = list(set([y for x,y in data.keys()]))
axes.set(aspect="equal", 
       xlim=(min(xaxis)-radius-margin,max(xaxis)+radius+margin), 
       ylim=(min(yaxis)-radius-margin,max(yaxis)+radius+margin), 
       xticks=xaxis, yticks=yaxis)

plt.show()

enter image description here


关于如何在图外放置图例,我将带您参考How to put the legend out of the plot。以及如何创建饼图到How to add a legend to matplotlib pie chart?的图例
此外,Python - Legend overlaps with the pie chart可能也很有趣。

相关问题