Skip to content

Fix log scaling for pcolor and pcolormesh #29783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 31, 2025

Conversation

dstansby
Copy link
Member

@dstansby dstansby commented Mar 20, 2025

PR summary

Fixes #29615. Instead of explicitly setting the data lims with the min/max values of the edges, pass all the values to allow update_datalim to handle them appropriately. I also took the oppurtunity to de-duplicate the limit setting code in pcolor() and pcolormesh(), since I needed to update them to match anyway.

The tests are both very similar, but I couldn't think of a good way to de-duplicate testing pcolor and pcolormesh - suggestions very welcome.

In terms of performance, running the below script locally I see ~10% slowdown with pcolormesh, and no significant slowdown with pcolor.

import numpy as np
import matplotlib.pyplot as plt
import time

x = np.arange(512, dtype=float)
y = np.arange(512, dtype=float)
z = np.arange(len(x) * len(y)).reshape(len(x), len(y))

ts = []
t = time.time()
for i in range(25):
    fig, ax = plt.subplots()

    # ax.pcolor(x, y, z)
    ax.pcolormesh(x, y, z)
    ax.set_yscale("log")

    fig.canvas.draw()
    dt = time.time() - t

    if i > 0:
        # Allow one warmup loop
        ts.append(dt)
        print(f"t = {dt} s")
    plt.close("all")
    t = time.time()

print(f"Mean:  {np.mean(ts)}")
print(f"Stdev: {np.std(ts)}")

pcolormesh

Before:
Mean: 0.08725245793660481
Stdev: 0.005315622438177594

After:
Mean: 0.09809311230977376
Stdev: 0.006966432274025711

pcolor

Before:
Mean: 1.8416528701782227
Stdev: 0.020062453166251135

After:
Mean: 1.8436991373697917
Stdev: 0.021314677717322438

PR checklist

@dstansby dstansby marked this pull request as ready for review March 20, 2025 18:21
@jklymak
Copy link
Member

jklymak commented Mar 24, 2025

You changed pcolor and pcolormesh, but are showing an example with scatter. What do the before and after plots look like? I can't really evaluate what has changed.

@dstansby
Copy link
Member Author

Gah, thanks for spotting that 🤦 . I've updated the script, and added the timings I get. pcolormesh seems to suffer ~10% performance wise, but pcolor doesn't change.

@jklymak
Copy link
Member

jklymak commented Mar 24, 2025

I guess I was more interested in the visual change, which I think this has?

@dstansby
Copy link
Member Author

Here's before:

before

And after:

after

Using:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(512, dtype=float)
y = np.arange(512, dtype=float)
z = np.arange(len(x) * len(y)).reshape(len(x), len(y))

fig, ax = plt.subplots()
ax.pcolormesh(x, y, z)
ax.set_yscale("log")

plt.show()

@jklymak
Copy link
Member

jklymak commented Mar 25, 2025

Okay? I guess I'm not sure how you choose ymin in the case that y[0]=0. Is this the accepted solution for this for other undefined y/xlimits?

@dstansby
Copy link
Member Author

If one scatters the same x/y points used for the pcolormesh (that includes a zero value in the ys), there's similar behaviour:

scatter_compare

I'm not sure why pcolormesh chooses a slightly lower y-limit, but I'd say that's in the weeds and this is a definitely improvement on past behaviour.

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(512, dtype=float)
y = np.arange(512, dtype=float)
print(y[0])
z = np.arange(len(x) * len(y)).reshape(len(x), len(y))

fig, axs = plt.subplots(ncols=2)
ax = axs[0]
ax.pcolormesh(x, y, z)
ax.set_yscale("log")

ax = axs[1]
ax.scatter(x, y)
ax.set_yscale("log")

plt.show()

@timhoffm
Copy link
Member

I'm not sure why pcolormesh chooses a slightly lower y-limit,

T.b.c: This may be a center vs. edge topic. Plot only chooses limits based on the points (centers of the mesh). The mesh may choose limits based on its edges.

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I agree this is an improvement.

However, I'm a little concerned that data at zero is being represented at all in this scale. I see this pretty often when folks do pcolormesh(k, t, spectra) and k[0]=0, and the left hand limit makes the left-most bin ends up spread out arbitrarily wide. It would be nice if this explicitly indicated what the xmin would be set to, based on what is in k, whereas I still have no idea based on this.

@timhoffm
Copy link
Member

I believe k[0]=0 is not special for us: Actually, the "center" coordinates of the mesh do not matter at all. AFAIK, we calculate the edges of the mesh and the limits are then determined from the positive edge coordinates (0 and negative coordinates are ignored). Note that even for all k>0 the edges can get negative (see #29615 (comment)).

@dstansby
Copy link
Member Author

Yes, I think that's right. If one scatters the edge coordinates of the mesh, the bottom limit is set similarly:
scatter_edges

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(512, dtype=float)
y = np.arange(512, dtype=float)
print(y[0])
z = np.arange(len(x) * len(y)).reshape(len(x), len(y))

fig, axs = plt.subplots(ncols=2)
ax = axs[0]
ax.pcolormesh(x, y, z)
ax.set_yscale("log")

ax = axs[1]
edges = np.concat([y - 0.5, [y[-1] + 0.5]])
ax.scatter(edges, edges)
ax.set_yscale("log")

plt.show()

The top limit is different because scatter adds some padding, but pcolormesh clips to the final edge.

@dstansby
Copy link
Member Author

Since this has two approvals with no changes since either, I'll merge this tomorrow unless there are any objections before then.

@dstansby dstansby merged commit a3aad58 into matplotlib:main Mar 31, 2025
41 checks passed
@dstansby dstansby added this to the v3.11.0 milestone Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: pcolormesh's default x/y range might break set_scale('log')
4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy