I have a dataframe of 6 columns that I want to plot as bar plot. The last 3 columns are on the on a secondary axis. One of the last 3 columns has values with much higher scale than the other two and I would like to add a 3rd Y axis to plot it with appropriate scale. In the following code example, I managed to add one a secondary Y-axis. But I could not find a way to add an additional (3rd) Y-axis.
Here is an example code:
# %config IPCompleter.greedy=True
import pandas as pd
import matplotlib.pyplot as plt
# Set the plotting settings
plotStyleDict = {'axes.titlesize' : 18,
'axes.titleweight' :'bold',
'axes.labelsize' : 15,
'lines.linewidth' : 3,
'lines.markersize' : 10,
'xtick.labelsize' : 12,
'ytick.labelsize' : 12}
plt.style.use(plotStyleDict)
dataDict = {'Year': ['1st Year','2st Year','3rd Year','4th Year'],
'ERRh': [0, 0.71, 0.46, 0.35],
'HRRh': [0.0, 0.66, 0.33, 0.14],
'EREh': [0.0, 0.38, 0.20, 0.11],
'ERRc': [1.2, 1.62, 2.04, 0.04],
'HRRc': [1.36, 2.27, 4.83, 0.09],
'EREc': [150, 120, 90, 70]}
# Create a dataframe
dfDataTest=pd.DataFrame.from_dict(dataDict, orient='columns', dtype=None, columns=None)
# Set Year column as index
dfDataTest.index = dfDataTest.Year
# delete Year column
dfDataTest.drop('Year', inplace = True, axis=1)
# To rename df columns with Greek symbols to be easier to show them as legends on the plot
orig_cols = ['ERRh', 'HRRh','EREh','ERRc', 'HRRc', 'EREc']
rename_cols = [r'$\eta_{ER,h}$', r'$\eta_{HR,h}$', r'$\eta_{th,h}$',r'$\eta_{ER,c}$',
r'$\eta_{HR,c}$',r'$\eta_{th,c}$']
rename_dict = dict(zip(orig_cols, rename_cols))
# Bar plot the dataframe with last 3 columns being on secondary axis
ax1FunEnergy = dfDataTest [orig_cols].rename(columns=rename_dict).\
plot(kind='bar',color=['firebrick','red', 'darksalmon','darkblue','blue','royalblue'],
figsize=(16,6), edgecolor=['black','black','black','black','black','black'],
stacked=False, secondary_y= [r'$\eta_{ER,c}$', r'$\eta_{HR,c}$',r'$\eta_{th,c}$'])
plt.xticks(rotation=30, horizontalalignment="center")
plt.title("Energy & Hydraulic Recoveries for Heating and Cooling",fontsize=18, weight='bold')
ax1FunEnergy.set_ylabel(r'$\eta_{ER,h} , \eta_{HR,h} , \eta_{th,h} [-]$')
ax1FunEnergy.right_ax.set_ylabel(r'$\eta_{ER,c} , \eta_{HR,c} , \eta_{th,c} [-]$')
plt.grid(True)
plt.show()
This results into the following plot:
The last column scale is too large compared to the rest. So the other two columns plotted on the secondary Y axis are barely visible. So my questions are as follows:
Thanks in advance! :)
For pandas plots, complex graphs are difficult to create, so I created a graph with the general matplotlib to add a third axis.
import numpy as np
fig,ax = plt.subplots(figsize=(16,6))
labels = dfDataTest.index.tolist()
x = np.arange(len(labels))
ax2 = ax.twinx()
ax3 = ax.twinx()
ax.set_xlim(-0.5, 4)
ax.set_ylim(0, 1)
ax2.set_ylim(0, 5)
ax3.set_ylim(0,160)
ax.set_xlabel('Year')
ax.set_ylabel(r'$\eta_{ER,h} , \eta_{HR,h} , \eta_{th,h} [-]$', size=18)
ax2.set_ylabel(r'$\eta_{ER,h} , \eta_{HR,h} , \eta_{th,h} [-]$', size=18)
ax3.set_ylabel(r'$\eta_{ER,c} , \eta_{HR,c} , \eta_{th,c} [-]$', size=18)
color=['firebrick','red','darksalmon','darkblue','blue','royalblue']
width = 0.1
p1 = ax.bar(x-(width*3), dfDataTest['ERRh'], width=width, color=color[0], align='center', label=r'$\eta_{ER,h}$')
p2 = ax.bar(x-(width*2), dfDataTest['HRRh'], width=width, color=color[1], align='center', label=r'$\eta_{ER,h}$')
p3 = ax.bar(x-width, dfDataTest['EREh'], width=width, color=color[2], align='center', label=r'$\eta_{ER,h}$')
p4 = ax2.bar(x, dfDataTest['ERRc'], width=width, color=color[3], align='center', label=r'$\eta_{HR,h}, \eta_{th,h}$')
p5 = ax2.bar(x+(width*1), dfDataTest['HRRc'], width=width, color=color[4], align='center', label=r'$\eta_{HR,h}, \eta_{th,h}$')
p6 = ax3.bar(x+(width*2), dfDataTest['EREc'], width=width, color=color[5], align='center', label=r'$\eta_{ER,c} , \eta_{HR,c}$')
lns = [p1,p2,p3,p4,p5,p6]
ax.legend(handles=lns, loc='best')
ax3.spines['right'].set_position(('outward', 60))
ax3.xaxis.set_ticks([])
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.set_title("Energy & Hydraulic Recoveries for Heating and Cooling",fontsize=18, weight='bold')
ax3.set_ylabel(r'$\eta_{ER,c} , \eta_{HR,c} , \eta_{th,c} [-]$')
fig.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.