1

I have 2x2 gird. I want pie chart at position (0,0) (0, 1), (1, 1) and legend at (1, 0) I am trying to do that by draw a pie chart in (0,0) (0, 1), and a pie chart that span 2 column at second row. I can left-align the legend. However, I don't know how to right align the pie chart.

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

plot

I want somehow move the third pie chart (ax3) toward right a bit.

Community
  • 1
  • 1
Haha TTpro
  • 5,137
  • 6
  • 45
  • 71
  • 1
    You're saying "I want pie chart at position (0,0) (0, 1), (1, 1)"; however you do not place the third bar chart at position (1,1). Is there any reason for that? I would also suggest reading [Legend overlaps with the pie chart](https://stackoverflow.com/questions/43272206/python-legend-overlaps-with-the-pie-chart). – ImportanceOfBeingErnest Jul 10 '17 at 15:50
  • hi @ImportanceOfBeingErnest, I want legend on the left. Thus, I think let the pie span 2 column, then align legend on the left and pie on the right might work somehow. By the ways, DavidG answer work for me. – Haha TTpro Jul 10 '17 at 16:04
  • I see, aligning the pie chart inside the axes would actually be possible, but I would not recommend it, because it's much more work than just placing the chart at position (1,1) and the legend at position (1,0), just as in the answer below. – ImportanceOfBeingErnest Jul 10 '17 at 18:56

1 Answers1

3

You can achieve this by plotting your third subplot in the position (1,1) and then move the legend outside of this subplot so that it, in effect, occupies the position (1,0). This can be done using bbox_to_anchor. The documentation can be found here.

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

Which produces the following graph:

enter image description here

DavidG
  • 24,279
  • 14
  • 89
  • 82