1

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.

Mr. T
  • 11,960
  • 10
  • 32
  • 54
Jorge
  • 11
  • 3
  • Please read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) - the structure of your input a_arr is not defined. And what do you mean by "I don't trust the ticks"? That this is a bug? Why should they all show the same curve, when you have a different y-scaling in your parasite axis? I really don't understand your question. – Mr. T Jun 05 '18 at 07:12
  • But in general, it sounds like [this question](https://stackoverflow.com/q/3148808/8881141). – Mr. T Jun 05 '18 at 07:15
  • `x**(1./2)` is for sure a different function than `(1./x - 1)`. So since you *are* plotting different functions, it's not really clear what the question asks for. – ImportanceOfBeingErnest Jun 05 '18 at 09:37
  • @ImportanceOfBeingErnest. I am plotting a(t) = t^(1/2). But I can also plot z(t) because I know that z(a) = 1/a - 1. Since is just a simple transformation, I want the function a(t) and z(t) look the same by doing the a-xis equivalent to z-axis using the proper transformation z(a). – Jorge Jun 05 '18 at 16:45
  • @Mr.T a_arr is the data obeying a = t**(1./2). As I explained it above I can not make the correct transformation in the y-axis so the functions a(t), z(t), T(t) looks as the same plot, that's what I meant with "I don't trust the ticks" in the y-axis – Jorge Jun 05 '18 at 16:53
  • `Attempted to set non-positive ylimits for log-scale axis; No handles with labels found to put in legend.` With two error messages from your code, it is difficult to give you an advice. I still think that the example in the link does, what you want. – Mr. T Jun 05 '18 at 19:49

0 Answers0