Skip to content

[Bug]: secondary_xaxis produces ticks at incorrect locations #29067

@dkweiss31

Description

@dkweiss31

Bug summary

It is possible I'm doing this incorrectly, but for a very simple example secondary_xaxis puts tick marks at incorrect locations. Modifying slightly the interpolation example from here https://matplotlib.org/stable/gallery/subplots_axes_and_figures/secondary_axis.html:

Code for reproduction

fig, ax = plt.subplots(constrained_layout=True)
xdata = np.arange(0, 11, 0.4)
ydata = np.random.randn(len(xdata))
ax.plot(xdata, ydata, label='Plotted data')
ax.set_xlabel('X [m]')
ax.legend()
xnew = xdata**2


def forward(x):
    return np.interp(x, xdata, xnew)


def inverse(x):
    return np.interp(x, xnew, xdata)

secax = ax.secondary_xaxis('top', functions=(forward, inverse))
secax.xaxis.set_minor_locator(AutoMinorLocator())
secax.set_xlabel('$X_{other}$')

plt.show()

Actual outcome

image

Expected outcome

Notice that e.g. 0 on the lower axis is not aligned with 0 on the top and 10 on the bottom is not aligned with 100 on the top.

Additional information

No response

Operating system

OS/X

Matplotlib Version

3.9.2

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.10.14

Jupyter version

7.2.2

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions