ragesz
ragesz

Reputation: 9527

Python pandas plot more columns but shows only one legend

I want to plot two columns on the same x-axis & y-axis. But pandas-plot only displays the second column's legend (so no dots for the first column). Of course both labels (name of columns) are displayed in the legend box.

My DataFrame is:

df = pd.DataFrame({'datetime':[dt.datetime(2016,1,1,0,0,0), dt.datetime(2016,1,4,0,0,0),
    dt.datetime(2016,1,9,0,0,0)], 'value':[10,7,8], 'value2':[12,4,9]})

And my plot is:

ax = df.plot(x='datetime', y='value', marker='o', linewidth=0)
df.plot(ax=ax, x='datetime', y='value2', marker='o', linewidth=0)

If I plot the lines as well than the "legend" of the first column is displayed, but it is only a blue line without dots:

ax = df.plot(x='datetime', y='value', marker='o')
df.plot(ax=ax, x='datetime', y='value2', marker='o')

Is it possible to show only the dots in the legend box (and on the plot) for both columns?

Thank you!

Upvotes: 0

Views: 601

Answers (1)

nico
nico

Reputation: 386

You should try recalling the legend after you call the second plot:

ax.legend()

this is what I get:

enter image description here

Upvotes: 1

Related Questions