Skip to content

[Bug]: clabels with logarithmic axes #27081

Open
@Jean1995

Description

@Jean1995

Bug summary

I am using clabels. However, when introducing logarithmic axes, the inline placement becomes inaccurate.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

delta = 0.025
x = np.geomspace(1e-2, 1e3, 40)
y = np.geomspace(1e-2, 1e3, 40)
X, Y = np.meshgrid(x, y)
Z = np.log(X) + np.log(Y)

fig, ax = plt.subplots(nrows=1, figsize=(3, 3))


CS = ax.contour(X, Y, Z)
ax.clabel(CS, inline=1, fontsize=10)
ax.set_xscale('log')
ax.set_yscale('log')

Actual outcome

example

Expected outcome

What is quite interesting: If I create several subplots with an identical content, using shared x and y axes, all but the first plot will show the correct behaviour.

This code:

fig, ax = plt.subplots(nrows=1, ncols=3, sharey=True, sharex=True, figsize=(10, 3.2))

CS = ax[0].contour(X, Y, Z)
ax[0].clabel(CS, inline=1, fontsize=10)
ax[0].set_xscale('log')
ax[0].set_yscale('log')

CS = ax[1].contour(X, Y, Z)
ax[1].clabel(CS, inline=1, fontsize=10)
ax[1].set_xscale('log')
ax[1].set_yscale('log')

CS = ax[2].contour(X, Y, Z)
ax[2].clabel(CS, inline=1, fontsize=10)
ax[2].set_xscale('log')
ax[2].set_yscale('log')

with the X, Y, Z values from above, lead to this plot:

working

Note that this only provides the correct results for sharex=True and sharey=True, if one (or both) are set to False, results are invalid.

Additional information

No response

Operating system

Linux and MacOS

Matplotlib Version

3.7.3

Matplotlib Backend

QtAgg

Python version

3.8.10

Jupyter version

6.0.3

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions