I have two DataFrames with dates on the x-axis. I would like to plot them in one figure with two scales.
DataFrame #1
print(df_daily.head())
date senti-pol senti-vader
0 2019-10-01 -0.060639 -0.174223
1 2019-10-02 -0.080265 0.090761
2 2019-10-03 -0.186335 -0.645464
3 2019-10-04 0.014124 -0.043164
4 2019-10-05 -0.035157 0.275379
DataFrame #2
print(df_dbk.head())
Open High Low Close Adj Close Volume
Date
2019-10-02 6.650 6.720 6.560 6.566 6.566 15527318
2019-10-04 6.520 6.531 6.369 6.480 6.480 16042648
2019-10-07 6.489 6.489 6.348 6.481 6.481 11130966
2019-10-08 6.515 6.529 6.205 6.304 6.304 13736758
2019-10-09 6.300 6.375 6.256 6.294 6.294 8625379
The second data frame is missing some dates. I guess this is my problem when I try to plot it (example with data):
from io import StringIO
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df_senti = ' date senti-pol senti-vader\n0 2019-10-01 -0.060639 -0.174223\n1 2019-10-02 -0.080265 0.090761\n2 2019-10-03 -0.186335 -0.645464\n3 2019-10-04 0.014124 -0.043164\n4 2019-10-05 -0.035157 0.275379'
df_daily = pd.read_csv(StringIO(df_senti),sep='\s+')
print(df_daily)
import yfinance as yf
df_dbk = yf.download("DBK.DE", start="2019-10-02", end="2019-10-10", interval="1d") # day+1 otherwise wrong data
print(df_dbk)
fig, ax1 = plt.subplots()
color = 'tab:red'
ax1.set_xlabel('date')
ax1.set_ylabel('sentiment', color=color)
ax1.plot(df_daily['date'], df_daily['senti-vader'], color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
color = 'tab:blue'
ax2.set_ylabel('share price', color=color) # we already handled the x-label with ax1
ax2.plot(df_dbk['Date'], df_dbk['Adj Close'], color=color)
ax2.tick_params(axis='y', labelcolor=color)
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.show()
The error which I receive is
KeyError: 'Date'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-11-e4156bac397f> in <module>
24 color = 'tab:blue'
25 ax2.set_ylabel('share price', color=color) # we already handled the x-label with ax1
---> 26 ax2.plot(df_dbk['Date'], df_dbk['Adj Close'], color=color)
27 ax2.tick_params(axis='y', labelcolor=color)
Is there a nice way to ignore the missing dates in the second DataFrame?