Seite 1 von 1
matplotlib legend
Verfasst: Mittwoch 15. September 2010, 14:38
von maria_dolores
Hallo zusammen,
kann man die Legende in einem barplot irgendwie außerhalb des Plots darstellen?
Mein jetziges Beispiel sieht so aus:
Code: Alles auswählen
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
width = 0.15
x = (1,2,3,4,5,6,7,8,9,10)
y = (1,2,3,4,5,6,7,8,9,10)
z = (2,3,5,7,9,10,12,13,15,17)
N = len(x)
ind = np.arange(N)
plot1 = ax.bar( ind,y, width, color = 'black')
plot2 = ax.bar( ind+width,z, width, color = 'orange')
plt.xticks(ind, x, ha = 'right')
fig.legend((plot1[0], plot2[0]), ('das erste', 'das zweite'), 'center right')
plt.show()
Momentan ist die Legende teilweise noch im Plot. Mit anderen Argumenten wie 'upper right' ändert sich das auch nicht. Ich habe öfter Plots, die mehr als nur zwei Legendeneinträge haben. Wenn das der Fall ist, verdecken sie Teile des Plots.
Hat jemand eine Idee, wie man die Legende komplett neben den Plot kriegt?
Bin für jeden Hinweis dankbar.
Gruß Isa
Re: matplotlib legend
Verfasst: Mittwoch 15. September 2010, 16:56
von DaMutz
Mach mal ein Beispiel, wo es ohne Beeinflussung nicht funktioniert. Ich denke er macht es "immer" korrekt. Sonst versuche es mit dem auskommentierten Teil dieser Zeile:
Code: Alles auswählen
fig.legend((plot1[0], plot2[0]), ('das erste', 'das zweite'))#, loc="center left", bbox_to_anchor=[.9, 0.5])
Diese Information findest du auch unter:
http://matplotlib.sourceforge.net/examp ... demo3.html
Re: matplotlib legend
Verfasst: Donnerstag 16. September 2010, 01:11
von gkuhl
@maria_dolores: Schau doch mal in die Dokumentation von ``plt.legend`` an. Da werden sehr genau die Möglichkeiten beschrieben, die Legende zu positionieren.
Grüße
Gerrit
Re: matplotlib legend
Verfasst: Donnerstag 16. September 2010, 08:15
von maria_dolores
@DaMutz: Danke, ich habe Deinen auskommentierten Teil genommen. Funktioniert wunderbar.
@gkuhl: Danke, ich habe mir die Doku noch mal angeschaut.
Allerdings habe ich keine Möglichkeit gesehen, die Legendenbeschriftung komplett ausserhalb zu setzen, ausser mit der 'bbox_to_anchor' - Anweisung.
Gruß
Isa
Re: matplotlib legend
Verfasst: Donnerstag 16. September 2010, 08:41
von gkuhl
Steht aber drin:
help(legend) hat geschrieben:[...]
Users can specify any arbitrary location for the legend using the *bbox_to_anchor* keyword argument. bbox_to_anchor can be an instance of BboxBase(or its derivatives) or a tuple of 2 or 4 floats. For example,
loc = 'upper right', bbox_to_anchor = (0.5, 0.5)
will place the legend so that the upper right corner of the legend at the center of the axes.
The legend location can be specified in other coordinate, by using the *bbox_transform* keyword.
The loc itslef can be a 2-tuple giving x,y of the lower-left corner of the legend in axes coords (*bbox_to_anchor* is ignored).
[...]
Re: matplotlib legend
Verfasst: Donnerstag 16. September 2010, 14:41
von maria_dolores
Ja genau. Ich wusste nur nicht, ob Du eine andere Anweisung meintest als die bbox-Anweisung.
Danke.
Gibt es eigentlich auch eine Möglichkeit die x-Achsenbeschriftung kleiner zu machen. Im Sinne von fontsize.
Ich habe es so probiert, was aber nicht geklappt hat:
Code: Alles auswählen
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties as font
fig = plt.figure()
fig.clear()
width = 0.3
ax = fig.add_axes([0.1,0.17,0.6,0.8])
x = ('heute', 'morgen', 'uebermorgen', 'naechste woche', 'gar nicht')
#for entry in x:
# font.set_size('small')
y = range(15,20,1)
z = range(20,25,1)
N = len(x)
ind = np.arange( N )
plot1 = ax.bar( ind, y, width, color = 'black' )
plot2 = ax.bar( ind+width, (z), width, color = 'orange' )
plt.xticks(ind, x, ha = 'right', rotation = 54)
leg = fig.legend( (plot1[0], plot2[0]), ('x_achse', 'y-achse'), 'center right' )
plt.show()
Gefunden hab ich die Doku unter
http://matplotlib.sourceforge.net/api/f ... Properties. Allerdings hab ich es nicht ganz hinbekommen es auszuführen. Siehe Code.
Hat jemand eine Idee, wie man das machen kann?
--------------------------------------------------------------------------------------------------------
Also da hab ich mir aber schwer getan... .
Einfach
Gruß Isa