Python 有没有办法在ggplot中绘制熊猫系列?

Python 有没有办法在ggplot中绘制熊猫系列?,python,pandas,python-ggplot,Python,Pandas,Python Ggplot,我正在用熊猫和非熊猫做实验。好的建议很有用。这个问题涉及到两个问题,我遇到了两个问题。 在熊猫身上策划一个系列是很容易的 frequ.plot() 我不知道如何在ggplot文档中执行此操作。相反,我最终创建了一个数据帧: cheese = DataFrame({'time': frequ.index, 'count' : frequ.values}) ggplot(cheese, aes(x='time', y='count')) + geom_line() 我希望ggplot——一个“与

我正在用熊猫和非熊猫做实验。好的建议很有用。这个问题涉及到两个问题,我遇到了两个问题。 在熊猫身上策划一个系列是很容易的

frequ.plot()
我不知道如何在ggplot文档中执行此操作。相反,我最终创建了一个数据帧:

cheese = DataFrame({'time': frequ.index, 'count' : frequ.values})
ggplot(cheese, aes(x='time', y='count')) + geom_line()
我希望ggplot——一个“与熊猫紧密结合”的项目——能有一个简单的系列

第二个问题是,当x轴是一天中的某个时间点时,无法显示stat_smooth()。似乎这可能与此有关,但我没有代表在那里发布。我的代码是:

frequ = values.sampler.resample("1Min", how="count")
cheese = DataFrame({'time': frequ.index, 'count' : frequ.values})
ggplot(cheese, aes(x='time', y='count')) + geom_line() + stat_smooth()
任何关于非matplotlib绘图的帮助都将不胜感激。谢谢
(我使用的是ggplot 0.5.8)

这是一个更大的解决方案,但您可以使用
qplot
使用series快速速记绘图

from ggplot import *
qplot(meat.beef)

在处理多个股票价格和经济时间序列时,我经常在Python的ggplot中遇到这个问题。使用ggplot需要记住的关键是,数据最好以长格式组织,以避免任何问题。我使用快速两步流程作为解决方法。首先,我们来获取一些股票数据:

import pandas.io.data as web
import pandas as pd
import time
from ggplot import *

stocks = [ 'GOOG', 'MSFT', 'LNKD', 'YHOO', 'FB', 'GOOGL','HPQ','AMZN'] # stock list

# get stock price function #
def get_px(stock, start, end):
    return web.get_data_yahoo(stock, start, end)['Adj Close']

# dataframe of equity prices   
px = pd.DataFrame({n: get_px(n, '1/1/2014', date_today) for n in stocks})

px.head()
              AMZN     FB  GOOG   GOOGL    HPQ    LNKD   MSFT   YHOO
Date                                                                
2014-01-02  397.97  54.71   NaN  557.12  27.40  207.64  36.63  39.59
2014-01-03  396.44  54.56   NaN  553.05  28.07  207.42  36.38  40.12
2014-01-06  393.63  57.20   NaN  559.22  28.02  203.92  35.61  39.93
2014-01-07  398.03  57.92   NaN  570.00  27.91  209.64  35.89  40.92
2014-01-08  401.92  58.23   NaN  571.19  27.19  209.06  35.25  41.02
首先了解ggplot需要datetime索引成为pandas数据帧中的一列,以便在从宽格式切换到长格式时正确打印。我写了一个函数来解决这个问题。它只是从pandas系列索引创建一个类型为datetime的“Date”列

def dateConvert(df):
  df['Date'] = df.index
  df.reset_index(drop=True)
  return df
然后在df上运行函数。将结果用作pandas pd.melt中的对象,使用“日期”作为id_变量。现在可以使用标准的ggplot()格式打印返回的df


你说有更好的方法是什么意思?更短的这就是ggplot的语法。您创建了一个具有一定美感的图形,并为其添加了几何图形。@Tomaugsburger我编辑了这篇文章以澄清问题。您的cheese datafre只是frequ的两列的副本。只需输入fequ而不复制。就紧密集成而言……这是指能够将数据帧传递给ggplot。您看到的语法非常紧凑。目前无法将序列作为数据传递给ggplot函数(请参阅)。非常酷。我不知道这件事。理想情况下,我希望x轴是系列的索引,但对于快速绘图,它工作得很好。对第二个问题有什么建议吗?当x轴是一天中的某个时间点时,stat_smooth()不会绘制任何内容。请问,您是如何在此处添加绘图的?@Fred;你是说堆栈交换答案?是的。您是否以某种方式将图形内联或只是一个链接?我想我可以只看一下页面源代码。NameError:名称'pRet'没有定义我一直在尝试使用数据集来绘制类似的内容,但遇到了很多问题,特别是在
DatetimeIndex
方面。
px_returns = px.pct_change() # common stock transformation
cumRet = (1+px_returns).cumprod() - 1 # transform daily returns to cumulative 
cumRet_dateConverted = dateConvert(cumRet) # run the function here see the result below#

cumRet_dateConverted.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 118 entries, 2014-01-02 00:00:00 to 2014-06-20 00:00:00
Data columns (total 9 columns):
AMZN     117 non-null float64
FB       117 non-null float64
GOOG     59 non-null float64
GOOGL    117 non-null float64
HPQ      117 non-null float64
LNKD     117 non-null float64
MSFT     117 non-null float64
YHOO     117 non-null float64
Date     118 non-null datetime64[ns]
dtypes: datetime64[ns](1), float64(8)


data = pd.melt(cumRet_dateConverted, id_vars='Date').dropna() # Here is the method I use to format the data in the long format. Please note the use of 'Date' as the id_vars.

data = data.rename(columns = {'Date':'Date','variable':'Stocks','value':'Returns'}) # common to rename these columns
retPlot_YTD = ggplot(data, aes('Date','Returns',color='Stocks')) \
+ geom_line(size=2.) \
+ geom_hline(yintercept=0, color='black', size=1.7, linetype='-.') \
+ scale_y_continuous(labels='percent') \
+ scale_x_date(labels='%b %d %y',breaks=date_breaks('week') ) \
+ theme_seaborn(style='whitegrid') \
+ ggtitle(('%s Cumulative Daily Return vs Peers_YTD') % key_Stock) 

fig = retPlot_YTD.draw()
ax = fig.axes[0]
offbox = ax.artists[0]
offbox.set_bbox_to_anchor((1, 0.5), ax.transAxes)
fig.show()