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