3

In one of my graphs, I used a secondary axis. My code create two different legend and show the legends in my graph. This is my code:

fig3 = plt.figure()
ax3 = fig3.add_subplot(111)
ax4 = fig3.add_subplot(111)

ax4 = ax3.twinx()
line6 = ax3.plot(threshold, different_costs, '-r', label = 'Costs   differences', linewidth = 2.0)
line7 = ax4.plot(threshold, costs1, '-b', label = 'Costs of Model 1 (OFF)',    linewidth = 2.0)
line9 = ax4.plot(threshold, costs2, '-y', label = 'Costs of Model 2 (STANDBY)', linewidth = 2.0)

ax3.set_xlabel("Threshold")
ax3.set_ylabel("Costs savings")
ax4.set_ylabel("Total costs")

plt.suptitle("Costs savings of using MODEL 1")
plt.legend()

plt.show()

How can I create one legend with three labels? And how can I show this legend outside of my graph?

Frank Martin
  • 2,584
  • 2
  • 22
  • 25
Kuijpers
  • 35
  • 1
  • 1
  • 3
  • Please see my answer to your question. Let me know if it works :) – Chuck May 07 '17 at 14:13
  • It works! But now I cannot read the last label (Costs of Model 2( STANDBY))... Do you know how I can solve this? – Kuijpers May 07 '17 at 15:41
  • You may have to experiment with different numbers within `bbox` to fit in all text. If the answer solved your problem, don't forget to upvote and accept. – Chuck May 07 '17 at 15:50
  • How can I upvote and accept the answer? – Kuijpers May 07 '17 at 15:53
  • If the answer solved your issue, you can upvote and accept via the following http://stackoverflow.com/help/someone-answers and https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Chuck May 07 '17 at 15:56
  • Thanks for accepting :) – Chuck May 07 '17 at 19:23

1 Answers1

0

Under this part of your code:

line6 = ax3.plot(threshold, different_costs, '-r', label = 'Costs   differences', linewidth = 2.0)
line7 = ax4.plot(threshold, costs1, '-b', label = 'Costs of Model 1 (OFF)',    linewidth = 2.0)
line9 = ax4.plot(threshold, costs2, '-y', label = 'Costs of Model 2 (STANDBY)', linewidth = 2.0)

To get all your lines onto the same legend, write:

lns = line6 + line7 + line9
labels = [l.get_label() for l in lns]
plt.legend(lns, labels)

To get your legend outside of the plot, refer to this answer How to put the legend out of the plot, and you can write:

plt.legend(lns, labels, loc='upper right', bbox_to_anchor=(0.5, -0.05))

For some sample data:

fig3 = plt.figure()
ax3 = fig3.add_subplot(111)
ax4 = fig3.add_subplot(111)

ax4 = ax3.twinx()
line6 = ax3.plot(range(0,10), range(0,10), '-r', label = 'Costs   differences', linewidth = 2.0)
line7 = ax4.plot(range(10,15), range(10,15), '-b', label = 'Costs of Model 1 (OFF)',    linewidth = 2.0)
line9 = ax4.plot(range(0,5), range(0,5), '-y', label = 'Costs of Model 2 (STANDBY)', linewidth = 2.0)

ax3.set_xlabel("Threshold")
ax3.set_ylabel("Costs savings")
ax4.set_ylabel("Total costs")

plt.suptitle("Costs savings of using MODEL 1")

lns = line6 + line7 + line9
labels = [l.get_label() for l in lns]
plt.legend(lns, labels, loc='upper right', bbox_to_anchor=(0.5, -0.05))

plt.show()

Lines on legend and Legend outside plot

Community
  • 1
  • 1
Chuck
  • 3,664
  • 7
  • 42
  • 76