Multiple X axis that are non-linear to each other

I am trying to build a figure with two X axes that are non-linear to each other, with matplotlib. The plot that I want to get is as follows:

Plot

In principle, age depends on redshift. It is non-linear and must be calculated. I want to do both age and redshift on the x axis. How can i do this?

+5
source share
2 answers

A function twiny()may be what you are looking for.

import matplotlib.pyplot as plt
plt.loglog(range(100))
ax1 = plt.gca()
ax2 = ax1.twiny()
ax2.set_xticks([100,80,50])
ax2.set_xticklabels(['0','1','2'])
ax1.set_xlabel('redshift')
ax2.set_xlabel('age')
plt.show()

graph example for twiny () function

+3
source

I did it like this:

from mpl_toolkits.axes_grid.parasite_axes import SubplotHost 
fig = plt.figure(1, figsize=(figwidth,figheight))
ax = SubplotHost(fig, 1,1,1) 
fig.add_subplot(ax)

#plotting as usual

ax2 = ax.twin() # ax2 is responsible for "top" axis and "right" axis
ax2.axis["right"].toggle(ticklabels=False)
ax2.xaxis.set_major_formatter(FuncFormatter(fmt_zToEta)) #set eta coord format

#with a function for the Z to eta transform for plot labels
def fmt_zToEta(x, pos=None):
    #...
    return transformed_label 

I also remember starting with this redshift example; -)

, SubPlotHost , 100%, () , , .

: . fooobar.com/questions/114536/...

0

All Articles