簡體   English   中英

在girdspec中的多列范圍子圖中左右對齊餅圖

[英]Align pie chart left or right in multicolumn span subplot in girdspec

我有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_to_anchor完成。 文檔可以在這里找到。

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()

其中生成以下圖表:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM