Skip to content

pcolorimage/pcolorfast silently drops alpha #13328

Open

Description

@anntzer

Bug report

Bug summary

When pcolorfast uses pcolorimage mode (i.e., for rectangular meshes with unequal spacing), an alpha keyword gets silently dropped even though there appears to be code to handle that in the definition of pcolorfast:

            elif style == "pcolorimage":
                im = mimage.PcolorImage(self, x, y, C,
                                        cmap=cmap,
                                        norm=norm,
                                        alpha=alpha,  # <- here
                                        **kwargs)

Code for reproduction

plt.gca().pcolorfast(
    [4, 8, 10], [7, 8, 10, 11], np.array([[0, 1], [2, 3], [4, 5]]),
    alpha=.5, cmap="viridis")

Actual outcome

The alpha channel is not applied. Compare e.g. with

plt.gca().pcolorfast(
    (0, 1), (2, 3), np.array([[0, 1], [2, 3], [4, 5]]),
    alpha=.5, cmap="viridis")

which uses AxesImage and correctly applies alpha.

Expected outcome

per above

Matplotlib version

  • Operating system: linux
  • Matplotlib version: 3.0 and master
  • Matplotlib backend (print(matplotlib.get_backend())): qt5agg
  • Python version: 3.7
  • Jupyter version (if applicable): n/a
  • Other libraries: n/a

Activity

  1. jklymak commented on Jan 30, 2019

    @jklymak
    Member

    Is there a reason to even keep pcolorfast around? I'd vote to deprecate it, unless there is some user base out there that depends on it...

  2. anntzer commented on Jan 30, 2019

    @anntzer
    ContributorAuthor

    Well, I was planning to push some PRs to improve its functionality.

    Specifically, right now it's a bit a pain to write robust code that works with imshow() independently of the setting of origin/rcParams["image.origin"] (see also https://matplotlib.org/devdocs/tutorials/intermediate/imshow_extent.html). Assume, for example, that the image x axis corresponds to xmin... xmax and the y axis corresponds to ymin... ymax. Then for imshow() to work independently of the origin, one needs to write something like

    imshow(data, extent=(xmin, xmax, ymin, ymax) if origin == "lower" else (xmin, xmax, ymax, ymin))
    

    (+/-0.5's)
    IOW the logical connection between indices and extents is not so great (plus one needs to remember whether it's xmin, xmax, ymin, ymax or xmin, ymin, xmax, ymax).

    OTOH, pcolorfast makes the connection much more obvious:

    pcolorfast((xmin, xmax), (ymin, ymax), data)
    

    where ymin always corresponds to the first row of data and ymax to the last row.
    Well of course right now pcolorfast has no "origin" kwarg, but that's the PR I'm preparing :)

  3. jklymak commented on Jan 30, 2019

    @jklymak
    Member

    That seems like a whole method for a pretty narrow edge case. pcolormesh takes an x and y argument and removes half-pixel ambiguity:

    m,n = X.shape()
    pcolormesh(np.linspace(xmin, xmax, n+1), np.linspace(ymin, ymax, m+1), X)

    seems like something better to point users towards, rather than a magic method that we will argue about the meaning of xmin for the next 10 years (i.e. is it the center of the pixel or the outer edge?).

  4. anntzer commented on Jan 30, 2019

    @anntzer
    ContributorAuthor

    But pcolormesh returns a QuadMesh, which is much less efficient than an AxesImage (it draws one quadrilateral at a time)... whereas pcolorfast (by design) detects the fast (regular) case and uses an AxesImage when possible.

  5. jklymak commented on Jan 30, 2019

    @jklymak
    Member

    I'm 👎 on having 3 (well actually 4 if we count pcolor) different methods that do subtly different things to accomplish the same goal. Two is fine (imshow and pcolormesh, I'd assume), but I'm against any modifications to pcolorfast. If you think imshow needs to be improved, I could see that argument.

  6. anntzer commented on Jan 30, 2019

    @anntzer
    ContributorAuthor

    In my view, the way origin, extent, and explicit x/ylimits play together in imshow() is close to beyond repair (the fact that you need a whole tutorial to understand how three kwargs interact with one another (https://matplotlib.org/devdocs/tutorials/intermediate/imshow_extent.html) seems just wrong), and is going to be very hard to fix with proper deprecations. So I'm basically exploiting pcolormesh as a new place to set up a more reasonable API...

  7. jklymak commented on Jan 30, 2019

    @jklymak
    Member

    My vote would be use imshow for basic stuff, and pcolormesh for more complicated. If you need extra performance, wrap imshow in whatever you need to get imshow to work. I don't see that imshow's brokenedness is a good reason to add more (breakable) API to pcolorfast.

  8. anntzer commented on Jan 30, 2019

    @anntzer
    ContributorAuthor

    Well, I can write my imshow wrapper but no one else will benefit from it, whereas improvements to pcolorfast would benefit everyone.
    pcolormesh's API is also not totally compatible with pcolorfast, as it doesn't allow just passing (xmin, xmax), (ymin, ymax), but requires a full list of coordinates.
    It's also not totally clear to me what you mean by breakability of the pcolorfast API.

  9. jklymak commented on Jan 31, 2019

    @jklymak
    Member

    I wrote a longer post, but it boils down to:

    What is the point of pcolorfast, other than you like x=(xmin, xmax), y=(ymin, ymax)?

    I'd be very much against pcolorfast or anything else getting an origin kwarg like imshow currently has. The only reason its there is to accomodate users who don't know what np.flipud is, and if you can't google how to flip a matrix...

  10. anntzer commented on Jan 31, 2019

    @anntzer
    ContributorAuthor

    The problem of flipud is that now the indices on the matrix have changed, which is problematic if you want to point to someone else "oh, look at the feature at coordinates (42, 124)" (or if they already have a bunch of coordinates for you to look at, to plot over, etc.) and that someone else is using other software (e.g. MATLAB, cough cough -- $dayjob stuff; or it could even just be the orientation of the camera on some experimental bench) to visualize their data (and frankly it is easier for me to adapt my plots to match however MATLAB orients them and the camera's orientation, rather than to change other people's MATLAB code or to reorient the camera).

  11. jklymak commented on Jan 31, 2019

    @jklymak
    Member

    Again, I don't see what the big deal is. pcolormesh(np.random.randn(32,32)); ax.set_ylim(31, 0) gives you same thing explicitly that imshow does by default. My point is that the API in imshow tries to do too much, most of which would more elegantly by standard axis manipulation.

    Again, beyond the slight API difference, what is the point of pcolorfast thats not provided by imshow (fast image dump) or pcolormesh?

  12. anntzer commented on Jan 31, 2019

    @anntzer
    ContributorAuthor

    From an API point of view, pcolormesh is much better than imshow (because it effectively allows setting the extents in a sane, origin-independent manner -- my y-axis is not necessarily 0-31, I can e.g. be zooming around a given pixel coordinates in a larger image), but from a performance point of view, pcolormesh is significantly slower for large images:

    import timeit
    from matplotlib import pyplot as plt
    import numpy as np
    
    shape = 640, 480
    
    print("imshow")
    fig, ax = plt.subplots()
    ax.imshow(np.random.standard_normal(shape))
    count, time = timeit.Timer("fig.canvas.draw()", globals={"fig": fig}).autorange()
    print(time / count)
    plt.close("all")
    
    print("pcolormesh")
    fig, ax = plt.subplots()
    ax.pcolormesh(np.random.standard_normal(shape))
    count, time = timeit.Timer("fig.canvas.draw()", globals={"fig": fig}).autorange()
    print(time / count)
    plt.close("all")
    

    with agg

    imshow
    0.012593437450414057
    pcolormesh
    0.07825877419963945
    

    (mplcairo also has a slower pcolormesh).

    (Actually, for very small images, pcolormesh is a bit faster than imshow, which is a bit curious... I'll investigate.)

    So once again, I'm treating pcolorfast as something with the performance of imshow (because it uses imshow in the case I care about, i.e. regular grid) but the saner extents API of pcolormesh.

  13. anntzer commented on Feb 1, 2019

    @anntzer
    ContributorAuthor

    After #13330 I can probably drop adding an origin kwarg (a standalone method is just fine).
    As for the half-pixel ambiguity (#13328 (comment)), having (xmin, xmax) refer to pixel edges rather than pixel centers is consistent with both pcolor(mesh) and with the extents kwarg to imshow.

  14. ImportanceOfBeingErnest commented on Feb 14, 2019

    @ImportanceOfBeingErnest
    Member

    Having myself spent a significant amount of time finding an error in someone else's code that used imshow's extent the wrong way and caused camera image calibrations to fail, I can feel with anyone who has found for himself a good solution for such problem. If that is pcolorfast, so be it - it certainly is more intuitive in certain cases.

    Concerning the initial issue, can PColorImage just error out when alpha is supplied and tell the user to use the slower method?

  15. anntzer commented on Feb 14, 2019

    @anntzer
    ContributorAuthor

    IIRC it's actually not that hard to make it support alpha, I must have the patch somewhere, just needs tests... and of course went down a rabbit hole of other "not really bugs but likely not optimal behavior" elsewhere.

  16. efiring commented on Feb 18, 2019

    @efiring
    Member

    I wrote pcolorfast for my own use case: plotting shipboard ADCP data, which is unevenly spaced in x but usually evenly spaced in y. The idea was to take advantage of the 3 levels of performance vs generality that others had already implemented: images are fast but not general, pcolormesh is general but not fast, and nonuniform image is in the middle, and applies to the most common nonuniform use case of rectilinear grids. I still think that supporting all 3 of these is well worth doing, and it is just a question of filling in the gaps, fixing the bugs, and ironing out the APIs. Work is needed on all three of these. Quadmesh is a marvel--but also a nightmare and a minefield, and pcolormesh wraps it in such a way that the land mines are sitting there, just waiting to blow off someone's foot.

  17. github-actions commented on May 31, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  18. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on May 31, 2023
  19. timhoffm commented on May 31, 2023

    @timhoffm
    Member

    We should at least warn on the ignored alpha.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    keepItems to be ignored by the “Stale” Github Actiontopic: color/alpha

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions