Skip to content
Advertisement

Plot multiple columns of dataframe in multiple plots (Python)

i want to plot multiple columns from a dataframe in multiple plots.

My code looks like this:

fig, axs = plt.subplots(6)
axs[0].plot(data.Date,[data[aapl],data['High_'+aapl],data['Low_'+aapl]])
axs[1].plot(data.Date,[data[msft],data['High_'+msft],data['Low_'+msft]])
axs[2].plot(data.Date,[data[fb],data['High_'+fb],data['Low_'+fb]])
axs[3].plot(data.Date,[data[amzn],data['High_'+amzn],data['Low_'+amzn]])
axs[4].plot(data.Date,[data[nflx],data['High_'+nflx],data['Low_'+nflx]])
axs[5].plot(data.Date,[data[googl],data['High_'+googl],data['Low_'+googl]])

The error i get is:

ValueError: x and y must have same first dimension, but have shapes (1995,) and (3, 1995)

I know x and y need to have the same shape, but how can i solve this problem here? My entire data is within one dataframe and should have the same shape. Is it possible to pass a list as a y value and change the shape?

My data looks like this: enter image description here

aapl = "EOD/AAPL - Adj_Close"
msft = "EOD/MSFT - Adj_Close"
fb = "EOD/FB - Adj_Close"
amzn = "EOD/AMZN - Adj_Close"
nflx = "EOD/NFLX - Adj_Close"
googl = "EOD/GOOGL - Adj_Close"

Advertisement

Answer

I am not sure what output you are expecting, but the following code can be used to resolve the above error. I have matched the stocks for the sample data, but the column names are different.

import matplotlib.pyplot as plt
import pandas as pd

fig, axes = plt.subplots(6,1, figsize=(12,9))
axes[0].plot([data.Date,data.Date,data.Date],[data['Adj Close_AAPL'],data['High_AAPL'],data['Low_AAPL']])
axes[1].plot([data.Date,data.Date,data.Date],[data['Adj Close_MSFT'],data['High_MSFT'],data['Low_MSFT']])
axes[2].plot([data.Date,data.Date,data.Date],[data['Adj Close_FB'],data['High_FB'],data['Low_FB']])
axes[3].plot([data.Date,data.Date,data.Date],[data['Adj Close_AMZN'],data['High_AMZN'],data['Low_AMZN']])
axes[4].plot([data.Date,data.Date,data.Date],[data['Adj Close_NFLX'],data['High_NFLX'],data['Low_NFLX']])
axes[5].plot([data.Date,data.Date,data.Date],[data['Adj Close_GOOGL'],data['High_GOOGL'],data['Low_GOOGL']])

plt.show()

enter image description here

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement