代码之家  ›  专栏  ›  技术社区  ›  polonius11

如何使用pandas python构建柱状图子图

  •  0
  • polonius11  · 技术社区  · 7 年前

    我正试图绘制7个柱状图来观察市场回报。我的代码在下面。它抛出了以下错误

    值错误: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 回复  |  直到 7 年前
        1
  •  1
  •   Diziet Asahi    7 年前

    我无法运行您的代码(请参见 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')
    (...)