Overlay two Data Frames in one graph

Question:

I have two different dataframes both are the same size and have the same values for the Date column.

        Date     Open     High      Low    Close        Volume    Market Cap
0 2018-04-16  8337.57  8371.15  7925.73  8058.67  5.631310e+09  1.415710e+11
1 2018-04-15  7999.33  8338.42  7999.33  8329.11  5.244480e+09  1.358120e+11
2 2018-04-14  7874.67  8140.71  7846.00  7986.24  5.191430e+09  1.336820e+11
3 2018-04-13  7901.09  8183.96  7758.93  7895.96  7.764460e+09  1.341140e+11
4 2018-04-12  6955.38  7899.23  6806.51  7889.25  8.906250e+09  1.180480e+11

Both data frames have the same structure. same column indexes and same number of rows.

print(df.dtypes)
print(df2.dtypes)

Both give

Date          datetime64[ns]
Open                 float64
High                 float64
Low                  float64
Close                float64  
Volume               float64
Market Cap           float64
dtype: object

I would like to plot Date on the x axis and Market Cap on the y axis. From my research I believe it is not possible to merge the plot function of a dataframe ie.

df.plot(x='Date', y = 'Market Cap', kind = 'line')
df2.plot(x='Date', y = 'Market Cap', kind = 'line')

My question is what is a good method to obtain the graph that I am trying to create? Should I use groupby, keys, or pyplot

to be clear i would like an overlay of the separate two graphs:

seperate two graphs

So far I have tried the code:

import matplotlib.pyplot as plt

# Init subplots
fig, axes = plt.subplots(1,1);

# Init ax with the first plot.
ax = df['Market Cap'].plot()

# Plot second df using ax from the first plot.
_ = df2['Market Cap'].plot(ax = ax)

plt.show()

Which results in an improper overlay:

results in improper overlay

Asked By: Kevin Riordan

||

Answers:

Try using subplots.

import matplotlib.pyplot as plt

# Init subplots
fig, axes = plt.subplots(1,1);

# Init ax with the first plot.
ax = df['Market Cap'].plot()

# Plot second df using ax from the first plot.
_ = df2['Market Cap'].plot(ax = ax)

plt.show()
Answered By: Victor Valente
ax = df.plot(x='Date', y = 'Market Cap', kind = 'line')
df2.plot(x='Date', y = 'Market Cap', kind = 'line', ax=ax)

Results Courtesy of @scottBoston

1

Answered By: Kevin Riordan