Jorge
Jorge

Reputation: 11

3 y's with different scales plotted with matplotlib

I want to plot a function, and show 3 different y axis, each one with different scales, logarithmic and linear scales. I followed this and this other examples trying to modify it to take into account the difference I mentioned before.

EDIT: I have the data to plot a(t) = t^(1/2), and I can change the scale in the a-axis because I know the transformation z(a) = 1/a - 1, so it's like plotting z(t), but I want that a(t) and z(t) to look the same by changing how the y-axis are shown, so the a-axis and z-axis are equivalent obeying z(a).

The problem is that the plot shows 3 curves as if it were different functions, and I want them to look the same, therefore I don't trust the transformations for each y-axis.

The concrete question is, how can I just change the scale of the y-axis.

This is what I have done:

from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt

t_arr = np.linspace(-8,0,500)
t_arr = 10**t_arr
a_arr = [ti**(1./2) for ti in t_arr]
z_arr = [(1./ai - 1) for ai in a_arr]
T_arr = [1e-5*(1./ai) for ai in a_arr]

host = host_subplot(111, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)

par1 = host.twinx()
par2 = host.twinx()

par2.axis["right"].toggle(all=True)

host.set_xlabel('Time')
host.set_ylabel("Scale Factor a")
par1.set_ylabel("Redshift z")
par2.set_ylabel("Energy E")

p1, = host.plot(t_arr, a_arr)
p2, = par1.plot(t_arr, z_arr)
p3, = par2.plot(t_arr, T_arr)

host.set_xscale('log')
host.set_yscale('log')
par1.set_xscale('log')
par1.set_yscale('log')
par2.set_xscale('log')
par2.set_yscale('log')

host.set_xlim([1e-8, 1])
host.set_ylim([1e-4, 1e2])
par1.set_ylim([9999,-0.99])
par2.set_ylim([0.1, 9e-6])

offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right", axes=par2,
                                    offset=(offset, 0))

par2.axis["right"].toggle(all=True)

host.legend()

host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
par2.axis["right"].label.set_color(p3.get_color())

plt.draw()
plt.show()

This is the plot I get.

Upvotes: 1

Views: 1907

Answers (0)

Related Questions