Reputation: 1803
I am attempting to plot 7 histograms to observe returns in the market. My code is below. It is throwing the following error
ValueError: x and y must have same first dimension, but have shapes (1230,) and (1,)
I do not understand this error because my check is confirming the data is set up correctly
>>> 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()
Corrected Code:
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()**
Upvotes: 0
Views: 81
Reputation: 40667
I can't run your code (see How to create a Minimal, Complete, and Verifiable example) and I can't be sure where the error comes from since you did not include the full error message, including the line number... But here is my guess:
You say you are trying to plot a histogram, but you are using matplotlib's plot()
which is used to plot a line with a similar number of x and y values. In this case, it's trying to plot SPY['SPY']
(shape (1230,)) as x, versus n_bins
(shape (1,)) as y, hence the error message.
You should be using the hist()
function instead:
axs[0].hist(SPY['SPY'], n_bins, density=True, histtype='bar')
(...)
Upvotes: 1