PeterBe
PeterBe

Reputation: 852

Change axis in matplotlib

I have the following code:

import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline

hours = list(range(25)) # [0, 1, 2, ... 22, 23, 24]
labels = [f'{h:02d}:00' for h in hours] # ["00:00", "01:00", ... "23:00", "24:00"]

load = [2000, 2000, 0, 0, 0, 0, 0, 2000, 2000, 2000, 2000, 2000,0,0, 0, 0, 0, 2000, 2000,2000, 2000, 2000, 0,0,0, 0]    
temperature = [21, 21.6, 22, 21.3, 20.8, 20.4, 20.1, 20, 20.6, 21.1, 21.5, 21.8, 22, 21.4, 20.9, 20.5, 20.2, 20, 20.7, 21.2, 21.6, 21.9, 22, 21.4, 21]

plt.figure(linewidth=1, figsize=(9, 5))
ax = plt.gca()

ax.plot(hours, load[0:25], color="goldenrod",drawstyle="steps-post",  linewidth=3)

ax.plot(hours, load[0:25], color="gold",drawstyle="steps-post",  linewidth=3, alpha=.8, label = 'Electrical power') # <- drawstyle argument.
ax.set_xlabel("Time of day", fontsize=16, labelpad=8)
ax.set_ylabel("Electrical power in W", fontsize=14, labelpad=8)
ax.set_xlim(0, 24)
ax.set_ylim(0, 3000)    
plt.xticks(hours, labels=labels, rotation=90)
plt.grid(axis='y', alpha=.4)
ax.tick_params(axis='both', which='major', labelsize=14)

ax2 = ax.twinx()
ax2.plot(hours, temperature, color="red",  linewidth=3, label = 'Temperature')
ax2.set_ylabel("Temperature in °C", fontsize=14, labelpad=8)
ax2.set_ylim(20, 22.5)  
ax2.tick_params(axis='both', which='major', labelsize=14)

fig = plt.gcf()

fig.legend(loc='center left', bbox_to_anchor=(0.25, 1.03), fontsize=14, ncol=3)
fig.tight_layout()
ax.patch.set_visible(False)
fig.savefig('ControlStrategy_Conventional.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()

I would like to change the axis. So the temperature should be displayed in the left axis and the load on the right axis. I tried to change it but the resulting plot looked weird. Can anyone tell me what to do? I'd appreciate every comment.

Upvotes: 0

Views: 133

Answers (1)

Stef
Stef

Reputation: 30679

You can first create the twin-axis and then freely select what to plot on what axis. Further, for clarity and readability, I prefer to stick with one interface (object oriented or pyplot) and not mix them:

hours = list(range(25))
labels = [f'{h:02d}' for h in hours[::2]]

fig,ax = plt.subplots(figsize=(9, 5), linewidth=1)
ax2 = ax.twinx()

ax.plot(hours, temperature, color="red",  linewidth=3, label = 'Temperature')
ax2.plot(hours, load[0:25], color="gold",drawstyle="steps-post",  linewidth=3, alpha=.8, label = 'Electrical power')
ax2.fill_between(hours, load[0:25],  step="post", color="yellow")
ax.set_zorder(1)
ax.patch.set_visible(False)

ax.set_xlabel("Time of day", fontsize=16, labelpad=8)
ax.set_xlim(0, 24)
ax.set_xticks(hours[::2])
ax.set_xticklabels(labels=labels)
ax.tick_params(axis='both', which='major', labelsize=14)
ax.grid(axis='y', alpha=.4)
ax.set_ylabel("Temperature in °C", fontsize=14, labelpad=8)
ax.set_ylim(20, 22.5)  
ax2.set_ylabel("Electrical power in W", fontsize=14, labelpad=8)
ax2.set_ylim(0, 3000)    

fig.legend(loc='center left', bbox_to_anchor=(0.25, 1.03), fontsize=14, ncol=3)
fig.tight_layout()

enter image description here

Upvotes: 1

Related Questions