Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
dstansby merged 6 commits intomatplotlib:mainfromdstansby:pcolormesh-log-lims
Mar 31, 2025

Conversation

dstansby
Copy link
Member

@dstansbydstansby commentedMar 20, 2025
edited
Loading

PR summary

Fixes#29615. Instead of explicitly setting the data lims with the min/max values of the edges, pass all the values to allowupdate_datalim to handle them appropriately. I also took the oppurtunity to de-duplicate the limit setting code inpcolor() andpcolormesh(), 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 testingpcolor andpcolormesh - suggestions very welcome.

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

importnumpyasnpimportmatplotlib.pyplotaspltimporttimex=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()foriinrange(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()-tifi>0:# Allow one warmup loopts.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

@dstansbydstansby marked this pull request as ready for reviewMarch 20, 2025 18:21
@jklymak
Copy link
Member

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
MemberAuthor

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

@jklymak
Copy link
Member

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

@dstansby
Copy link
MemberAuthor

Here's before:

before

And after:

after

Using:

importnumpyasnpimportmatplotlib.pyplotaspltx=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

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

@dstansby
Copy link
MemberAuthor

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

scatter_compare

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

importnumpyasnpimportmatplotlib.pyplotaspltx=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

@jklymakjklymak 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 dopcolormesh(k, t, spectra) andk[0]=0, and the left hand limit makes the left-most bin ends up spread out arbitrarily wide. It would be nice if thisexplicitly indicated what the xmin would be set to, based on what is ink, whereas I still have no idea based on this.

@timhoffm
Copy link
Member

I believek[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
MemberAuthor

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

importnumpyasnpimportmatplotlib.pyplotaspltx=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 becausescatter adds some padding, butpcolormesh clips to the final edge.

timhoffm reacted with thumbs up emoji

@dstansby
Copy link
MemberAuthor

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

@dstansbydstansby merged commita3aad58 intomatplotlib:mainMar 31, 2025
41 checks passed
@dstansbydstansby added this to thev3.11.0 milestoneMar 31, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@QuLogicQuLogicQuLogic left review comments

@jklymakjklymakjklymak approved these changes

@timhoffmtimhoffmtimhoffm approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
v3.11.0
Development

Successfully merging this pull request may close these issues.

[Bug]: pcolormesh's default x/y range might breakset_scale('log')
4 participants
@dstansby@jklymak@timhoffm@QuLogic

[8]ページ先頭

©2009-2025 Movatter.jp