如何使用python构建直方图子图

2024-05-16 12:54:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图绘制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()**

Tags: columnstrueclosebardensitystartendrename
1条回答
网友
1楼 · 发布于 2024-05-16 12:54:39

我无法运行您的代码(请参见How to create a Minimal, Complete, and Verifiable example),并且我无法确定错误来自何处,因为您没有包含完整的错误消息,包括行号。。。但我的猜测是:

你说你试图绘制一个直方图,但是你使用的是matplotlib's ^{},它用来绘制一条x和y值数目相似的线。在本例中,它试图将SPY['SPY'](shape(1230,))绘制为x,而将n_bins(shape(1,))绘制为y,因此出现错误消息。你知道吗

您应该改用the ^{} function

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

相关问题 更多 >