I have been struggling with plotting a bar with very small and very big values. I have tried to plot a chart with two y-axes.
However, the output cannot represent the data and the green columns at different y-axes do not allow to compare with other data. Is there a solution to change the green column to show the data on 2015 - 2019, with the 2020 data represented on the second y-axis? Or is there any other good solution?
The code:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
dict_ = {'date': [pd.Timestamp('20150720'),pd.Timestamp('20160720'),pd.Timestamp('20170720'),pd.Timestamp('20180720'),pd.Timestamp('20190720'),pd.Timestamp('20200720')],
'BKNG': [15.22, 6.36, 5.05, 5, 9.3641, -3],
'MCD' : [25.22, 11.36, 7.05, 9, 8.3641, -6],
'YUM' : [52.22, 21.36, 25.05, 26, 21.3641, -1000]
}
df = pd.DataFrame(dict_)
df['date'] = df['date'].dt.year
df.set_index('date',inplace=True)
fig1,ax = plt.subplots(figsize=(10,6))
ax.bar(df.index+0.0, df['BKNG'],width=0.1,label='BKNG')
ax.bar(df.index+0.1, df['MCD'],width=0.1,label='MCD')
plt.grid(True)
plt.legend()
plt.xlabel('date')
plt.ylabel('value')
plt.title('ROE')
ax2 = ax.twinx()
ax2.bar(df.index+0.2, df['YUM'],width=0.1,label='YUM', color='g')
plt.legend(loc=3)
plt.ylabel('YUM')