简体   繁体   English

Python熊猫绘制更多列,但仅显示一个图例

[英]Python pandas plot more columns but shows only one legend

I want to plot two columns on the same x-axis & y-axis. 我想在同一x轴和y轴上绘制两列。 But pandas-plot only displays the second column's legend (so no dots for the first column). 但是pandas-plot仅显示第二列的图例(因此第一列没有点)。 Of course both labels (name of columns) are displayed in the legend box. 当然,两个标签(列名)都显示在图例框中。

My DataFrame is: 我的DataFrame是:

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! 谢谢!

You should try recalling the legend after you call the second plot: 调用第二个图后,您应该尝试重新调用图例:

ax.legend()

this is what I get: 这就是我得到的:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM