Python 在girdspec中的多列跨距子批次中向左或向右对齐饼图

Python 在girdspec中的多列跨距子批次中向左或向右对齐饼图,python,matplotlib,charts,alignment,Python,Matplotlib,Charts,Alignment,我有2x2的网格。我希望饼图位于(0,0)(0,1)、(1,1)位置,图例位于(1,0) 我试图通过在(0,0)(0,1)中绘制一个饼图,并在第二行绘制一个跨越2列的饼图来实现这一点。我可以左对齐图例。但是,我不知道如何右对齐饼图 labels = ["Very negative", "Negative", "Neutral", "Positive", "Very positive"] dev_s

我有2x2的网格。我希望饼图位于(0,0)(0,1)、(1,1)位置,图例位于(1,0) 我试图通过在(0,0)(0,1)中绘制一个饼图,并在第二行绘制一个跨越2列的饼图来实现这一点。我可以左对齐图例。但是,我不知道如何右对齐饼图

labels = ["Very negative", "Negative", "Neutral", "Positive", "Very positive"]
dev_sentences = [139, 289, 229, 279, 165]
test_sentences = [279, 633, 389, 510, 399]
train_sentences = [1092, 2218, 1624, 2322, 1288]

plt.clf()
plt.cla()
plt.close()
gs = gridspec.GridSpec(2, 2)
ax1= plt.subplot(gs[0, 0])
ax1.pie(dev_sentences,  autopct='%1.1f%%',
        shadow=True, startangle=90)
ax1.axis('equal')

ax2= plt.subplot(gs[0, 1])
ax2.pie(test_sentences, autopct='%1.1f%%',
        shadow=True, startangle=90)
ax2.axis('equal')

ax3 = plt.subplot(gs[1, :])
ax3.pie(train_sentences, autopct='%1.1f%%',
        shadow=True, startangle=90)
ax3.axis('equal')
ax3.legend(labels=labels, loc="upper left")


我希望以某种方式将第三个饼图(ax3)向右移动一点。

您可以通过在位置(1,1)中绘制第三个子图来实现这一点,然后将图例移动到此子图之外,使其实际上占据位置(1,0)。这可以使用
bbox\u to\u锚定
来完成。可以找到文档

这将生成以下图表:


您的意思是“我想要(0,0)(0,1)、(1,1)位置的饼图”;但是,您不会将第三个条形图放置在位置(1,1)处。有什么原因吗?我还建议大家阅读。嗨@ImportanceOfBeingErnest,我希望左边有图例。因此,我认为让饼图横跨2列,然后将左侧的图例与右侧的饼图对齐可能会起到某种作用。顺便说一句,DavidG answer对我来说很有用。我知道,在轴内对齐饼图实际上是可能的,但我不推荐这样做,因为这比仅仅将图表放置在位置(1,1)和图例放置在位置(1,0)要复杂得多,正如下面的答案所示。
labels = ["Very negative", "Negative", "Neutral", "Positive", "Very positive"]
dev_sentences = [139, 289, 229, 279, 165]
test_sentences = [279, 633, 389, 510, 399]
train_sentences = [1092, 2218, 1624, 2322, 1288]

plt.clf()
plt.cla()
plt.close()
gs = gridspec.GridSpec(2, 2)
ax1= plt.subplot(gs[0, 0])
ax1.pie(dev_sentences,  autopct='%1.1f%%',
        shadow=True, startangle=90)
ax1.axis('equal')

ax2= plt.subplot(gs[0, 1])
ax2.pie(test_sentences, autopct='%1.1f%%',
        shadow=True, startangle=90)
ax2.axis('equal')

ax3 = plt.subplot(gs[1, 1])
ax3.pie(train_sentences, autopct='%1.1f%%',
        shadow=True, startangle=90)
ax3.axis('equal')

# use bbox_to_anchor to move your legend to wherever you like
ax3.legend(labels=labels, bbox_to_anchor=(-1,1), loc="upper left")

plt.show()