'Different y scales in one python plot
I want to make a plot as the one attached
Where it has two y axes with different scales and all the parameters names are identified by colors in the small upper left box.
I tried the following
plt.figure()
p1, = plt.plot(s_pos, beta_x)
p2, = plt.plot(s_pos, beta_y)
plt.legend([p1, p2], [r'$\beta_x$', r'$\beta_y$'])
p3, = plt.plot(s_pos, dx)
plt.legend([p3], [r'$\eta_x$'])
But it showed the eta in the same scale of the plots (s_pos, betax, beta y) y axes and also it give me the eta only in the small box.
Solution 1:[1]
I tried the following adapted from the examples:
from mpl_toolkits.axes_grid1 import host_subplot import matplotlib.pyplot as plt
host = host_subplot(111)
par = host.twinx()
host.set_xlabel("s_pos")
host.set_ylabel(r'$\beta_x$')
host.set_ylabel(r'$\beta_y$')
par.set_ylabel("dx")
p1, = host.plot(s_pos, beta_x, label=r'$\beta_x$')
p2, = host.plot(s_pos, beta_y, label=r'$\beta_y$')
p3, = par.plot(s_pos, dx, label=r'$\eta_x$')
leg = plt.legend()
host.yaxis.get_label().set_color(p1.get_color())
leg.texts[0].set_color(p1.get_color())
host.yaxis.get_label().set_color(p2.get_color())
leg.texts[1].set_color(p2.get_color())
par.yaxis.get_label().set_color(p3.get_color())
leg.texts[2].set_color(p3.get_color())
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ely66 |



