jtlz2
jtlz2

Reputation: 8437

Matplotlib: Plot two x axes, one linear and one with logarithmic ticks

(Heavily edited:)

In python matplotlib, I want to plot y against x with two xscales, the lower one with linear ticks and the upper one with logarithmic ticks.

The lower x values are an arbitrary function of the upper ones (in this case the mapping is func(x)=np.log10(1.0+x)). Corollary: The upper x tick positions are the same arbitrary function of the lower ones.

The positions of the data points and the tick positions for both axes must be decoupled.

I want the upper axis's logarithmic tick positions and labels to be as tidy as possible.

What is the best way to produce such a plot?

Related: http://matplotlib.1069221.n5.nabble.com/Two-y-axis-with-twinx-only-one-of-them-logscale-td18255.html

Similar (but unanswered) question?: Matplotlib: how to set ticks of twinned axis in log plot

Could be useful: https://stackoverflow.com/a/29592508/1021819

Upvotes: 5

Views: 8225

Answers (2)

jtlz2
jtlz2

Reputation: 8437

Here is an attempt at an answer after speaking to a few people and with thanks to @BusyBeaver.

I agree the question was ill-posed and will amend it to clarify (help welcome!).

I do think this is a useful one to have written down on stackoverflow.

Code:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator

# Necessary functions

def tick_function(x):
    """Specify tick format"""
    return ["%2.f" % i for i in x]

def func(x):
    """This can be anything you like"""
    funcx=np.log10(1.0+x)
    return funcx

z=np.linspace(0.0,4.0,20)

np.random.seed(seed=1234)
y=np.random.normal(10.0,1.0,len(z))

# Set up the plot
fig,ax1 = subplots()
ax1.xaxis.set_minor_locator(AutoMinorLocator())
ax1.yaxis.set_minor_locator(AutoMinorLocator())

# Set up the second axis
ax2 = ax1.twiny()

# The tick positions can be at arbitrary positions
zticks=np.arange(z[0],z[-1]+1)
ax2.set_xticks(func(zticks))
ax2.set_xticklabels(tick_function(zticks))
ax2.set_xlim(func(z[0]),func(z[-1]))
ax1.set_ylim(5.0,15.0)

ax1.set_xlabel(r'$\log_{10}\left(1+z\right)$')
ax2.set_xlabel(r'$z$')
ax1.set_ylabel('amplitude/arb. units')

plt.tick_params(axis='both',which = 'major', labelsize=8, width=2)
plt.tick_params(axis='both',which = 'minor', labelsize=8, width=1)

_=ax1.plot(func(z),y,'k.')

plt.savefig('lnopz2.png')

Plot generated from the above code

I am not sure how to control the upper ax2 minor ticks (e.g. every 0.5).

Upvotes: 0

Marco
Marco

Reputation: 2092

You may find Axes.twiny() and Axes.semilogx() useful.

import numpy as np
import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()

x = np.arange(0.01, 10.0, 0.01) # x-axis range
y = np.sin(2*np.pi*x) # simulated signal to plot

ax1.plot(x, y, color="r") # regular plot (red)
ax1.set_xlabel('x')

ax2 = ax1.twiny() # ax1 and ax2 share y-axis
ax2.semilogx(x, y, color="b") # semilog plot (blue)
ax2.set_xlabel('semilogx')

plt.show()

Upvotes: 3

Related Questions