1

How can I make a figure with 2 charts, and both charts have dual Y axis?

I know have to make dual y axis for a figure with 1 chart, but wasn't able to figure out how to do it for two charts.

This is the syntax I use for matplotlib: fig, ax = plt.subplots()

  • Does this answer your question? [How to plot in multiple subplots](https://stackoverflow.com/questions/31726643/how-to-plot-in-multiple-subplots) – Jody Klymak Jan 26 '23 at 00:12
  • You may also take a look at the documentation. https://matplotlib.org/stable/tutorials/intermediate/arranging_axes.html#basic-2x2-grid – Jody Klymak Jan 26 '23 at 00:14
  • i saw those answers, but i still dont get how to plot two charts with each having dual y axis (i am learning python as i go). I know how to plot 1 chart with dual y axis. I know how to plot multiple charts. But not sure how to do that together. – user20856754 Jan 26 '23 at 14:17

1 Answers1

0

This should be fairly simple:

fig, ax = plt.subplots(1, 2) # or (2, 1) depending on how you want them aligned
ax[0].plot(...)
ax[1].plot(...)

For the dual y-axis, follow this: https://matplotlib.org/stable/gallery/subplots_axes_and_figures/secondary_axis.html

EDIT: Here is an example, almost directly copied from the above documentation:

import numpy as np
import matplotlib.pyplot as plt

month = np.linspace(1, 12, 12)

DC_high_temp = np.array([43, 47, 56, 67, 76, 85, 89, 87, 81, 69, 59, 48]) # °F
DC_low_temp = np.array([25, 27, 35, 44, 54, 63, 68, 68, 59, 46, 37, 30]) # °F

SEATTLE_high_temp = np.array([46, 50, 54, 58, 64, 69, 73, 74, 69, 59, 50, 45]) # °F
SEATTLE_low_temp = np.array([36, 37, 39, 42, 48, 52, 56, 56, 52, 46, 40, 36]) # °F

def degF_to_degC(T):
   return (T - 32) * (5 / 9)
def degC_to_degF(T):
   return (T * (9 / 5)) + 32

fig, ax = plt.subplots(2,1)
plt.tight_layout(pad = 2.5) 
ax[0].plot(month, DC_high_temp, label = "High Temperature")
ax[0].plot(month, DC_low_temp, label = "Low Temperature")
ax[0].set_ylabel("Temperature (°F)")
ax[0].set_title("DC Average Monthly Temperature")
ax[0].legend()


ax[1].plot(month, DC_high_temp, label = "High Temperature")
ax[1].plot(month, DC_low_temp, label = "Low Temperature")
ax[1].set_ylabel("Temperature (°F)")
ax[1].set_title("Seattle Average Monthly Temperature")
ax[1].set_xlabel("Month")

sec_y_axis_0 = ax[0].secondary_yaxis('right', functions = (degF_to_degC, degC_to_degF))
sec_y_axis_0.set_ylabel("Temperature (°C)")

sec_y_axis_1 = ax[1].secondary_yaxis('right', functions = (degF_to_degC, degC_to_degF))
sec_y_axis_1.set_ylabel("Temperature (°C)")

plt.show()

Dual y-Axis Plot

  • Yes that creates two charts, but how do I add dual Y axis to both of them? – user20856754 Jan 26 '23 at 14:17
  • @user20856754, same method you would do to add a second y axis to a single chart, just use `ax[0]`, etc. to refer to it. – Jacob Ivanov Jan 26 '23 at 21:11
  • `fig, ax = plt.subplots(4,1) ax[0].plot(...) ax[1]=ax[0].twinx() ax[1].plot(...) ax[3].plot(...) ax[4]=ax[3].twinx() ax[4].plot(...)` results into two charts with dual y axis (as desired) + extra blank charts (not desired) – user20856754 Jan 27 '23 at 17:47
  • @user20856754, you should be using `plt.subplots(2, 1)`, then using `ax[0]` to refer to the first chart, and `ax[1]` to refer to the second chart. Hope that helps. – Jacob Ivanov Jan 27 '23 at 18:40
  • `x = np.arange(0, 10, 1) fig, ax = plt.subplots(2,1) ax[0].plot(x,x,'r--') ax[0].plot(x,x/2,'b') ax[1].plot(x,-x,'r')` it adds a second series, but it doesnt add a second Y axis. – user20856754 Jan 27 '23 at 19:24
  • @user20856754, see my post. – Jacob Ivanov Jan 28 '23 at 17:43