Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas not changing legend label

Tags:

python

pandas

I'm trying to plot my data, however pandas uses the column label which I need to override in the legend. I thought the label keyword would change this, but it seems to have no effect. Googling shows that this was a bug in the 0.15 version, but I have 0.25 now. Does anyone know of a way around this?

Sample code:

x = pd.DataFrame(list(range(2,513, 2)), columns=['x'])
y = pd.DataFrame(np.random.rand(256), columns=['y'])
df = pd.concat([x, y], axis=1)
df = df.set_index(['x'])
df.plot(label='Random')

I would expect the legend to then list 'Random' as label for the curve.

like image 971
Luke Avatar asked Aug 31 '25 05:08

Luke


1 Answers

It seems like the labels are created automatically from the series names. You can rename your y series before plotting as a workaround, ie.:

df.y.rename('Random').plot(legend=True)
like image 171
mac13k Avatar answered Sep 02 '25 18:09

mac13k