1

Can anyone give me a tip on how I could add in a legend to my plot? I am happy with how the plot looks just need a legend : )

# data from pandas df's
zoom_plot = fc1_dataset.loc['2021-8-9'].between_time('5:00', '17:00')
_no_vfd_sig = zoom_plot[['fc1_flag','duct_static','duct_static_setpoint']]
_vfdsig = zoom_plot.vfd_speed


# using subplots() function
fig, ax = plt.subplots(figsize=(25,8))
plt.title('Visually verify FDD is flagging OK')
 
# using the twinx() for creating another
# axes object for secondary y-Axis
ax2 = ax.twinx()
ax.plot(zoom_plot.index, _no_vfd_sig)
ax2.plot(zoom_plot.index, _vfdsig, color = 'r')
 
# giving labels to the axises
ax.set_xlabel('Date')
ax.set_ylabel('Duct Pressure and FDD Flag')

# secondary y-axis label
ax2.set_ylabel('% Fan Speed')

# defining display layout
plt.tight_layout()
 
# show plot
plt.show()

Output: enter image description here

bbartling
  • 3,288
  • 9
  • 43
  • 88

1 Answers1

1

same as before code...

plot1, = ax.plot(zoom_plot.index, _no_vfd_sig)
plot2, = ax2.plot(zoom_plot.index, _vfdsig, color = 'r')

ax.set_xlabel('Date')
ax.set_ylabel('Duct Pressure and FDD Flag')

# secondary y-axis label
ax2.set_ylabel('% Fan Speed')

plt.legend([plot1,plot2],["plot 1", "plot 2"])

# defining display layout
plt.tight_layout()

# show plot
plt.show()
tzinie
  • 717
  • 5
  • 9
  • Could you ever give me a tip on where to place your code? Im not having any luck getting a legend to populate, no errors nothing populates tho – bbartling Nov 07 '21 at 15:33
  • @HenryHub I edited my response and now it works for me. Of course you can add all the label names that you wish. – tzinie Nov 07 '21 at 16:06
  • Any chance you could help me with this question too? https://stackoverflow.com/questions/69886610/matplotlib-multiple-y-axis-pandas-plot – bbartling Nov 08 '21 at 19:37