我需要使用IPython和ggplot来获取多个时间序列图上的图例



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

使用有没有办法在ggplot中绘制熊猫系列?作为一个模型,我正在玩ggplot中的肉数据集。尽管数据的显示似乎还可以,没有图例。处方在我的例子中,修复链接底部的图例失败了。

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

from ggplot import *
import pandas as pd
%matplotlib inline

原始形式的"肉"数据帧。

print meat.head (2)
        date  beef  veal  pork  lamb_and_mutton  broilers  other_chicken  
0 1944-01-01   751    85  1280               89       NaN            NaN   
1 1944-02-01   713    77  1169               72       NaN            NaN   
   turkey  
0     NaN  
1     NaN  

长格式的"肉"数据帧。

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

图像::output_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

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

最新更新