如何使用Pandas Python建立直方图子图

时间:2018-07-17 00:46:55

标签: python pandas matplotlib

我正试图绘制7个直方图以观察市场回报。我的代码如下。它引发以下错误

ValueError:x和y必须具有相同的第一尺寸,但形状为(1230,)和(1,)

我不理解此错误,因为我检查确认数据设置正确

>>> print(SPY['SPY'])

date
2013-07-16    151.7347
2013-07-17    152.1197
2013-07-18    152.9529
2013-07-19    153.2247
2013-07-22    153.5236
2013-07-23    153.1975



def plot_fat_tails():
    SPY = pdr.DataReader('SPY', 'iex', start, end).rename(columns={'close': 'SPY'})
    DIA = pdr.DataReader('DIA', 'iex', start, end).rename(columns={'close': 'DIA'})
    DAX = pdr.DataReader('DAX', 'iex', start, end).rename(columns={'close': 'DAX'})
    EWU = pdr.DataReader('EWU', 'iex', start, end).rename(columns={'close': 'EWU'})
    EWH = pdr.DataReader('EWH', 'iex', start, end).rename(columns={'close': 'EWH'})
    EWS = pdr.DataReader('EWS', 'iex', start, end).rename(columns={'close': 'EWS'})
    INDY = pdr.DataReader('INDY', 'iex', start, end).rename(columns={'close': 'INDY'})
    n_bins = 50
    fig, axs = plt.subplots(7, 1, sharex=True)
    # Remove horizontal space between axes
    fig.subplots_adjust(hspace=0)
    # Plot each graph, and manually set the y tick values
    axs[0].plot(SPY['SPY'], n_bins, density=True, histtype='bar')
    axs[1].plot(DIA['DIA'], n_bins, density=True, histtype='bar')
    axs[2].plot(DAX['DAX'], n_bins, density=True, histtype='bar')
    axs[3].plot(EWU['EWU'], n_bins, density=True, histtype='bar')
    axs[4].plot(EWH['EWH'], n_bins, density=True, histtype='bar')
    axs[5].plot(EWS['EWS'], n_bins, density=True, histtype='bar')
    axs[6].plot(INDY['INDY'], n_bins, density=True, histtype='bar')
    plot.show()

更正的代码:

def plot_fat_tails():
    SPY = pdr.DataReader('SPY', 'iex', start, end).rename(columns={'close': 'SPY'})
    DIA = pdr.DataReader('DIA', 'iex', start, end).rename(columns={'close': 'DIA'})
    DAX = pdr.DataReader('DAX', 'iex', start, end).rename(columns={'close': 'DAX'})
    EWU = pdr.DataReader('EWU', 'iex', start, end).rename(columns={'close': 'EWU'})
    EWH = pdr.DataReader('EWH', 'iex', start, end).rename(columns={'close': 'EWH'})
    EWS = pdr.DataReader('EWS', 'iex', start, end).rename(columns={'close': 'EWS'})
    INDY = pdr.DataReader('INDY', 'iex', start, end).rename(columns={'close': 'INDY'})
    n_bins = 10
    fig, axs = plt.subplots(7, 1, sharex=True)
    # Remove horizontal space between axes
    fig.subplots_adjust(hspace=0)
    # Plot each graph, and manually set the y tick values
    axs[0].**hist**(SPY['SPY'], n_bins, density=True, histtype='bar')
    axs[1].**hist**(DIA['DIA'], n_bins, density=True, histtype='bar')
    axs[2].**hist**(DAX['DAX'], n_bins, density=True, histtype='bar')
    axs[3].**hist**(EWU['EWU'], n_bins, density=True, histtype='bar')
    axs[4].**hist**(EWH['EWH'], n_bins, density=True, histtype='bar')
    axs[5].**hist**(EWS['EWS'], n_bins, density=True, histtype='bar')
    axs[6].**hist**(INDY['INDY'], n_bins, density=True, histtype='bar')
    **fig.show()**

1 个答案:

答案 0 :(得分:1)

我无法运行您的代码(请参见How to create a Minimal, Complete, and Verifiable example),而且由于您未包括完整的错误消息(包括行号),因此我无法确定错误的出处...但这是我的猜测:

您说您正在尝试绘制直方图,但是您正在使用matplotlib's plot(),它用于绘制具有相似数量的x和y值的线。在这种情况下,它试图将SPY['SPY'](形状(1230,))绘制为x,而将n_bins(形状(1,))绘制为y,从而显示错误消息。

您应该改用the hist() function

axs[0].hist(SPY['SPY'], n_bins, density=True, histtype='bar')
(...)