1
votes

I have 2 dataframe and I already set the date column to datetime format but it still returns a Value error. May I know is it because of the x-axis with 2 dataframes is different?

import pandas as pd
import matplotlib.pyplot as plt

df_1 = pd.read_csv("df.csv")
df_1.head()

            symbol  net_revenue
date        
2015-03-31  ETSY    58543
2015-06-30  ETSY    61365
2015-09-30  ETSY    65696
2015-12-31  ETSY    87895
2016-03-31  ETSY    81847

import pandas_datareader.data as web
stock = 'ETSY'
start ='2013-12-31'
import datetime as dt
end = '2020-09-30'

df2 = web.DataReader(stock,'yahoo',start,end)
df2.head()

High    Low Open    Close   Volume  Adj Close
Date                        
2015-04-16  35.740002   28.219999   31.000000   30.000000   19763300    30.000000
2015-04-17  30.299999   26.510000   29.770000   27.580000   3965500 27.580000
2015-04-20  28.900000   24.870001   28.770000   24.900000   3076200 24.900000
2015-04-21  26.040001   24.559999   24.969999   25.750000   2184700 25.750000
2015-04-22  26.240000   24.950001   26.000000   25.120001   1442500 25.120001
... ... ... ... ... ... ...
2020-09-24  116.099998  109.519997  112.550003  113.680000  4339000 113.680000
2020-09-25  118.684998  113.010002  113.699997  118.279999  2572600 118.279999
2020-09-28  123.949997  119.190002  120.370003  123.690002  3456500 123.690002
2020-09-29  125.699997  121.260002  123.610001  123.230003  2618000 123.230003
2020-09-30  125.589996  120.139999  123.190002  121.629997  2524500 121.629997

# Plot 2 dataframe into 1 graph

x = df1.index
y1 = df1['net_revenue']
y2 = df2['Adj Close']

fig, ax1 = plt.subplots(figsize=(20,8))
ax2 = ax1.twinx()

curve1 = ax1.plot(x, y1, label='etsy', color='r')
curve2 = ax2.plot(x, y2, label='close', color='b')

plt.plot()

The error:

ValueError: x and y must have same first dimension, but have shapes (23,) and (1376,)
1

1 Answers

2
votes

your df1 and df2 have different lengths. Try plotting each dataframe against its own index.

# I would suggest passing what you plot directly to the function
# that way you know which you are plotting against which
ax1.plot(df1.index, df1['net_revenue'], label='etsy', color='r')
ax2.plot(df2.index, df2['Adj Close'], label='close', color='b

You can also plot using pandas:

df1['net_revenue'].plot(ax=ax1, label='etsy', color='r')
df2['Adj Close'].plot(, ax=ax2, label='close', color='b')

Remember to convert your index to datetime type before plotting.