0

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?

2 Answers 2

1

'Date' is not a column in your dataframe dbk_df_csv. It appears to be the name of your index. This is what you KeyError is telling you.

Try this instead

ax2.plot(dbk_df_csv.index, dbk_df_csv['Adj Close'], color=color)

In fact, you can leave out the x values and it will be inferred from the data:

ax2.plot(dbk_df_csv['Adj Close'], color=color)
Sign up to request clarification or add additional context in comments.

1 Comment

This didn't helped. I edited my code examples with an excerpt of the actual data.
0

I found a solution for my problem. The problem was different types of x-values.

print(type(df_daily.index))
<class 'pandas.core.indexes.numeric.Int64Index'>

print(type(df_daily['date']))
<class 'pandas.core.series.Series'>

print(type(df_dbk.index))
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>

Based on the answer from @busybear I thought to transform the index from df_daily.

df_daily['Date'] = pd.to_datetime(df_daily['date'])
df_daily = df_daily.set_index('Date')

print(df_daily)
                 date  senti-pol  senti-vader
Date                                         
2019-10-01 2019-10-01  -0.060639    -0.174223
2019-10-02 2019-10-02  -0.080265     0.090761
2019-10-03 2019-10-03  -0.186335    -0.645464
2019-10-04 2019-10-04   0.014124    -0.043164
2019-10-05 2019-10-05  -0.035157     0.275379

print(type(df_daily.index))
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>

And with the changed index'es I was able to generate the plot.

fig, ax1 = plt.subplots(figsize=(20, 10))

color = 'tab:red'
ax1.set_xlabel('date')
ax1.set_ylabel('sentiment', color=color)
ax1.plot(df_daily.index, df_daily['senti-vader'], color=color, marker='.')
ax1.tick_params(axis='y', labelcolor=color)
ax1.tick_params(axis='x', rotation=45)

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.index, df_dbk['Adj Close'], color=color, marker='.')
ax2.tick_params(axis='y', labelcolor=color)
ax2.tick_params(axis='x', rotation=45)

fig.tight_layout()  # otherwise the right y-label is slightly clipped

plt.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.