2

I'm trying to get an "inverse" second x-axis to a x-axis in log-scale. secondary_xaxis works fine but I can't format the ticks as usually. Is this a bug or am I missing something?

Here's what I do:

import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(figsize=(0.6*4,0.6*3))
y = np.random.rand(10)
x = np.linspace(30,200,num=10)

p = plt.plot(x, y, marker='o', markersize=3)
plt.xscale("log")

def m2rho(m):
    return 1 / m * 1000

def rho2m(rho):
    return 1 / rho / 1000

secax = axs.secondary_xaxis('top', functions=(m2rho, rho2m))

from matplotlib.ticker import ScalarFormatter, NullFormatter
for axis in [axs.xaxis, secax.xaxis]:
    axis.set_major_formatter(ScalarFormatter())
    axis.set_minor_formatter(NullFormatter())
axs.set_xticks([50, 100, 200])
secax.set_xticks([20,10,5])

plt.tight_layout()
plt.show()

Resulting in:

xtixs on top are wrong

So essentially the second axis on top should display the ticks 20, 10 and 5 in non-scientific numbers, but it doesn't.

  • 3
    It's not currently possible to change the formatter. If you think this is a missing feature (to which I agree), you can open an issue on the [matplotlib issue tracker](https://github.com/matplotlib/matplotlib/issues) – ImportanceOfBeingErnest Nov 06 '19 at 14:39
  • Thanks! I couldn't find that information, for me it looked like `secax` behaves like any axis. - I will do that. – Gunther Rocket Nov 07 '19 at 10:51
  • I'm running into similar issues, is there currently any workaround? I tried giving a list of ticks when creating the secondary axis (e.g. `axs.secondary_xaxis('top', functions=(m2rho, rho2m), ticks=ticks)`), but these tick values seem to be ignored. – Joris Jun 04 '20 at 16:15
  • I opened a [bugreport](https://github.com/matplotlib/matplotlib/issues/15621) which is closed by now. I didn't try it again but maybe you can get an updated version of matplotlib where it's fixed. – Gunther Rocket Jun 07 '20 at 13:04

0 Answers0