使用IPython和ggplot在多个timeseries图上获取图例需要做什么?

2024-10-06 08:10:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试学习如何使用python中的ggplot库。在 在熟悉了一些例子后,我注意到 时间序列图的几个系列,似乎有必要 pandas.melt()将数据转换为长格式。在

使用 Is there a way to plot a pandas series in ggplot? 作为一个模型,我正在使用ggplot中的肉类数据集。虽然 显示的数据似乎还可以,没有图例。处方 在我的例子中,修复链接底部的图例失败了。在

我在某个地方看到一个帖子,说图例显示只会失败 内联(在IPython笔记本中)。对我来说,它也没有显示 使用qt的图例(在Mac上)。在

from ggplot import *
import pandas as pd
%matplotlib inline

原始形式的“肉”数据帧。在

^{pr2}$

长格式的“肉”数据帧。在

meat_lng = pd.melt(meat, id_vars=['date'])
print meat_lng.head (2)

        date variable  value
0 1944-01-01     beef    751
1 1944-02-01     beef    713

plot = ggplot(aes(x='date', y='value', color='variable'), data=meat_lng) \
     + geom_line() \
     + ggtitle("Meat Production by Decade--Missing Legend")
print plot

。。图片::输出_6_0.png

<ggplot: (280905345)>

我有PNG格式的图形。我怎么能把它插在这里?在

我希望下面的几行字能给我一个传奇。在

plot = ggplot(aes(x='date', y='value', color='variable'), data=meat_lng) \
     + geom_line(size=2.0) \
     + ggtitle("Meat Production by Decade")

# Code that I hoped would fix the missing legend problem.
fig = plot.draw()
ax = fig.axes[0]
offbox = ax.artists[0]
offbox.set_bbox_to_anchor((1, 0.5), ax.transAxes)
fig.show()

::

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)

<ipython-input-11-9cd7998d1503> in <module>()
      4 fig = plot.draw()
      5 ax = fig.axes[0]
----> 6 offbox = ax.artists[0]
      7 offbox.set_bbox_to_anchor((1, 0.5), ax.transAxes)
      8 fig.show()


IndexError: list index out of range

Tags: to数据pandasdateplotvalue格式fig
1条回答
网友
1楼 · 发布于 2024-10-06 08:10:02

您引用的示例是使用ggplot版本0.5.8完成的。在后来的版本中,他们改变了一些东西,从散点图中删除了这个传说。在ggplot的github页面上有一个开放的问题,但是在问题解决之前,如果你想让图例出现,我建议使用旧版本。在

相关问题 更多 >