Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 419
jtlz2 Avatar asked Oct 18 '22 05:10

jtlz2


1 Answers

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()

like image 64
Marco Avatar answered Oct 21 '22 00:10

Marco