Seite 1 von 1

pyplot und labels

Verfasst: Donnerstag 11. Juli 2013, 17:10
von Tyrax
Hallo,

ich arbeite seit ein paar Wochen mit matplotlib, doch wie mir scheint, sind mir ein paar grundlegende Dinge noch nicht klar geworden. Ich habe so viele verschiedene Stile und Beispiele gesehen, dass ich an folgender Stelle verzweifle:

Code: Alles auswählen

import matplotlib.pyplot as plt
import numpy as np

xData = 3 + np.random.randn(1000)
yData = np.power(-5 + 2*np.random.randn(1000), 1) + 10

fig  = plt.figure()

ax1 = fig.add_subplot(121)
ax1.plot(xData,   yData, '-k', lw=2, label='test1')
ax1.legend(loc='upper left', fontsize=24)

ax2 = fig.add_subplot(122)
ax2.legend(loc = 'upper left', fontsize=24)
ax2.plot(xData,   yData, '-b', lw=2, label='test2')

plt.show()
Im zweiten subplot, werden die Daten nicht gelabelt und ich bekomme die Warnung:

Code: Alles auswählen

/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.8-intel.egg/matplotlib/axes.py:4747: UserWarning: No labeled objects found. Use label='...' kwarg on individual plots.
Was ist hier los? Warum bekomme ich in einem subplot mein label und im anderen nicht? Ich stehe komplett auf dem Schlauch.

Danke für Eure Hilfe. Tyrax

edit: Scheinbar ist die Sache nicht so trivial, wie sie ausgesehen hat. Vielleicht ist auch schon mein Ansatz Mist. Also vielleicht besser ohne meinen Beispiel-Code:
Wie kann ich mit pyplot eine figure mit zwei subplots erstellen und darin unabhängig Daten labeln?

Re: pyplot und labels

Verfasst: Samstag 13. Juli 2013, 16:11
von schaeffkoch
so....?!

Code: Alles auswählen

import pylab as pl
import scipy as sp

t=sp.linspace(0, 2*sp.pi,100)
y1=sp.sin(t)
y2=sp.sin(t**2)

pl.figure()
ax1=pl.subplot(2,1,2)
pl.plot(t,y1, lw=2,label="y1")
ax1.legend()
ax2=pl.subplot(2,1,1)
pl.plot(t,y2,label="y2")
ax2.legend()
pl.show()

Re: pyplot und labels

Verfasst: Samstag 13. Juli 2013, 16:20
von schaeffkoch
legend muss nach plot kommen.

Re: pyplot und labels

Verfasst: Samstag 13. Juli 2013, 16:30
von Tyrax
Oh Mann, es war am Ende mal wieder so einfach! Besten Dank schaeffkoch, da wäre ich wohl nicht drauf gekommen.

Grüße, Tyrax