so I found this code here in stackover flow: Plot on primary and secondary x and y axis with a reversed y axis
#1 Import Library
import matplotlib.pyplot as plt
%matplotlib
import numpy as np
import pandas as pd
#2 IMPORT DATA
sfData=pd.read_excel('data/streamflow validation.xlsx',sheet_name='Sheet1')
#3 Define Data
x = sfData['Year']
y1 = sfData['Observed']
y2 = sfData['Simulated']
y3 = sfData['Areal Rainfall']
# Or we can use loc for defining the data
x = list(sfData.iloc[:, 0])
y1 = list(sfData.iloc[:, 1])
y2 = list(sfData.iloc[:, 2])
y3 = list(sfData.iloc[:, 3])
#4 Plot Graph
fig, ax1 = plt.subplots(figsize=(12,10))
# increase space below subplot
fig.subplots_adjust(bottom=0.3)
# Twin Axes
# Secondary axes
ax2 = ax1.twinx()
ax2.bar(x, y3, width=15, bottom=0, align='center', color = 'b', data=sfData)
ax2.set_ylabel(('Areal Rainfall(mm)'),
fontdict={'fontsize': 12})
# invert y axis
ax2.invert_yaxis()
# Primary axes
ax1.plot(x, y1, color = 'r', linestyle='dashed', linewidth=3, markersize=12)
ax1.plot(x, y2, color = 'k', linestyle='dashed', linewidth=3, markersize=12)
#5 Define Labels
ax1.set_xlabel(('Years'),
fontdict={'fontsize': 14})
ax1.set_ylabel(('Flow (m3/s)'),
fontdict={'fontsize': 14})
#7 Set limit
ax1.set_ylim(0, 45)
ax2.set_ylim(800, 0)
ax1.set_xticklabels(('Jan 2003', 'Jan 2004', 'Jan 2005', 'Jan 2006', 'Jan 2007', 'Jan 2008', 'Jan 2009' ),
fontdict={'fontsize': 13})
for tick in ax1.get_xticklabels():
tick.set_rotation(90)
#8 set title
ax1.set_title('Stream Flow Validation 1991', color = 'g')
#7 Display legend
legend = fig.legend()
ax1.legend(['Observed', 'Simulated'], loc='upper left', ncol=2, bbox_to_anchor=(-.01, 1.09))
ax2.legend(['Areal Rainfall'], loc='upper right', ncol=1, bbox_to_anchor=(1.01, 1.09))
#8 Saving the graph
fig.savefig('output/figure1.png')
fig.savefig('output/figure1.jpg')
However, my output figure becomes like this:
Now I wonder how can I delete those white parts so it can just show purely blue bar graphs?
In one of my datasets, it worked perfectly fine, just like below:
but i would love to delete those white border in the bar graph so it won't end up like the first picture in any of the future datasets.