Skip to content

bug fix for pcolorfast to allow direct color #1317

Closed

Description

@2sn

Could you please change in the current branch in

axes.py, line 7582
(Axes.pcolorfast)

      nr, nc = C.shape

to
nr, nc = C.shape[:2]

this way one can pass [nx,ny,3] or [nx,ny,4] arrays to the routine - for
which the PcolorImage it calls is made (style == "pcolorimage")

-Alexander

From e555f98ca915eb89c7df1cff21261efb506fc29a Mon Sep 17 00:00:00 2001
From: Alexander Heger matplotlib@2sn.net
Date: Fri, 28 Sep 2012 15:08:27 +1000
Subject: [PATCH] BF - fix pcolor fast to allow direct color


lib/matplotlib/axes.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py
index 56017a2..6a4a87d 100644
--- a/lib/matplotlib/axes.py
+++ b/lib/matplotlib/axes.py
@@ -7582,7 +7582,7 @@ class Axes(martist.Artist):
if norm is not None: assert(isinstance(norm, mcolors.Normalize))

     C = args[-1]
  •    nr, nc = C.shape
    
  •    nr, nc = C.shape[:2]
     if len(args) == 1:
         style = "image"
         x = [0, nc]
    
    --
    1.7.11.4

Activity

  1. efiring commented on Sep 28, 2012

    @efiring
    Member

    What you are calling a "bug" is not a bug, it is simply part of the present design of the pcolor family, including pcolormesh and pcolorfast. These methods are presently designed to be quite similar in their inputs, and to accept color-mapped inputs only, not arrays of RGB or RGBA. Changing that might or might not be a good idea, but it is certainly not trivial.

    As an alternative, or in addition, one could add methods (and pyplot functions) for NonUniformImage and PcolorImage, which could accept RGB or RGBA arrays directly.

    For pcolorfast, the sticking point is the case where the pcolormesh method is used. It uses a Quadmesh, which is a collection, and at present collections in general are not designed to handle anything other than color mapping. I suspect this is not terribly hard to change, but it would require some thought, care, and testing.

  2. 2sn commented on Sep 28, 2012

    @2sn
    Author

    Dear Eric,

    the point is that if you just add these 4 characters, pcolorfast can use
    n x m * [3|4] arrays. It won't break anything else adding these, so
    why not add it and make my application work? I assume it would be
    useful to other as well.

    I use of for the case "pcolorimage" where x and y are just vectors.

    It really is just this one change that is an oversight to make things
    work more general in this routine, fixing the other methods can be done
    later independently and I am happy to have a look and try to fix it; it
    may need some testing on various platforms and cases outside of what I
    can access.

    But I have to admit so far I am rather discouraged to never receive any
    positive feedback on obvious easy additions I have proposed. The
    developer circle seems pretty closed. And I would have liked to add
    many things ... what can I do?

    Yes, it would be good to change in general, in fact I plan to implement
    a different approach using colour functions instead of tables, something
    I did in IDL and would like to keep.

    Best wishes,
    Alexander

    On 28/09/12 16:56, Eric Firing wrote:

    What you are calling a "bug" is not a bug, it is simply part of the
    present design of the pcolor family, including pcolormesh and
    pcolorfast. These methods are presently designed to be quite similar in
    their inputs, and to accept color-mapped inputs only, not arrays of RGB
    or RGBA. Changing that might or might not be a good idea, but it is
    certainly not trivial.

    As an alternative, or in addition, one could add methods (and pyplot
    functions) for NonUniformImage and PcolorImage, which could accept RGB
    or RGBA arrays directly.

    For pcolorfast, the sticking point is the case where the pcolormesh
    method is used. It uses a Quadmesh, which is a collection, and at
    present collections in general are not designed to handle anything other
    than color mapping. I suspect this is not terribly hard to change, but
    it would require some thought, care, and testing.

    —
    Reply to this email directly or view it on GitHub
    #1317 (comment).

  3. efiring commented on Sep 28, 2012

    @efiring
    Member

    Adding those 4 characters would make pcolorfast work on n x m x (3/4) arrays for some inputs, not for others (those that trigger quadmesh rendering). At the very least this inconsistency would need to be noted in the docstring. I see your point that it would not break any existing code, so it is tempting in that sense. I will reconsider.

    I don't recall any of your previous suggestions, but I think that if you look through the git history, you will see that there have been many mpl contributors, so I don't agree with your assertion that the "developer circle seems pretty closed".

  4. 2sn commented on Sep 28, 2012

    @2sn
    Author

    Dear Eric,

    Adding those 4 characters would make pcolorfast work on n x m x (3/4)
    arrays for some inputs, not for others (those that trigger quadmesh
    rendering). At the very least this inconsistency would need to be noted
    in the docstring. I see your point that it would not break any existing
    code, so it is tempting in that sense. I will reconsider.

    yes, it would trigger an exception for those cases where the method does
    not support direct colour. Those would need to be fixed later if
    anybody want to use them, I am happy to have a look if there is interest.

    But now it triggers an exception for the "pcolorimage" where all else
    would work fine already. This is why I urge you to do it - it completes
    a new feature at essentially no cost (unless someone really had built
    code that relies on pcolorfast throwing en exception in that case).

    If one wants to add this for the other methods in pcolorfast at a later
    time, this is the first step as well.

    I don't recall any of your previous suggestions, but I think that if you
    look through the git history, you will see that there have been many mpl
    contributors, so I don't agree with your assertion that the "developer
    circle seems pretty closed".

    no, I had a suggestion on text alignment, an addition that also would
    not have broken anything existing. Just added a new feature. I have
    written now some extra code that actually solved some of the questions
    that came up in the discussion - this was very useful - but they add new
    kw parameters and are a bigger change but add a missing part halful for
    aligning text symbol markers - I don't want to go through the trouble of
    making a patch for this if all my submissions are just rejected. The
    original patch, though its shortcomings, is still the only "natural"
    extension of the exiting code and should go in as is.

    I could also add some fixes to descriptions/documentation, etc., as I go
    through the code, which I think should be useful as well.

    I just thinks it would be useful that if I fix things this goes back
    into the general repository. BTW, I have not found a good description
    on how to add new examples to show new features or recipes for how to do
    things.

    Best wishes,
    Alexander

  5. 2sn commented on Sep 28, 2012

    @2sn
    Author

    The previous code suggestions was a pull request entitled "NF - Text numeric values for ha and va".

  6. WeatherGod commented on Oct 14, 2012

    @WeatherGod
    Member

    Alexander, it has been a while since you last replied. Are you still intending to put together a PR for this feature you wish to add? I am not against such a feature, provided that it includes all of the appropriate changes to documentation and test cases.

    I am still a little confused about the use-case, but I would be willing to review it when I see the entire change all at once. Note, the feature freeze for v1.2.0 has long since past, so if you were trying to get this in for that, it wasn't going to happen anyway.

  7. 2sn commented on Oct 14, 2012

    @2sn
    Author

    Dear Benjamin,

    I have been on travel for the last 2 weeks and still have about half a
    week to go (incl. 24h travel from US tu AU).

    Well, the change is really just adding a four characters in one line and
    no documented feature breaks. Do you really need a PR for that? The
    online development documentation suggests to send small changes to the
    list - maybe that needs to be updated - but I can generate a PR when I
    am back.

    1.2 is not critical for me, I use the master branch anyway, but it would
    be good to see this added to the development.

    The use case is that you can provide your own image data in RGB[A]
    format already and not use the current colormap feature. It allows very
    different kind of visualization - you can use all 2**24 colors. Or once
    could add the alpha channel for partial transparency in select regions.
    For example, imaging to use color (hue) for direction of a flow and
    saturation for the magnitude of velocity (just an example, it does not
    work well - of course they need to be translated to RGB from HSL).

    In any case, it does not break anything old, adds capability, and does
    not require opaque changes to the code.

    Best wishes,
    Alexander

    On 13/10/12 21:02, Benjamin Root wrote:

    Alexander, it has been a while since you last replied. Are you still
    intending to put together a PR for this feature you wish to add? I
    am not against such a feature, provided that it includes all of the
    appropriate changes to documentation and test cases.

    I am still a little confused about the use-case, but I would be
    willing to review it when I see the entire change all at once. Note,
    the feature freeze for v1.2.0 has long since past, so if you were
    trying to get this in for that, it wasn't going to happen anyway.

    --- Reply to this email directly or view it on GitHub:
    #1317 (comment)

  8. WeatherGod commented on Oct 14, 2012

    @WeatherGod
    Member

    Alex,

    On Sunday, October 14, 2012, Alexander Heger wrote:

    Dear Benjamin,

    I have been on travel for the last 2 weeks and still have about half a
    week to go (incl. 24h travel from US tu AU).

    Well, the change is really just adding a four characters in one line and
    no documented feature breaks. Do you really need a PR for that?

    We need a PR for the change, plus docs, plus tests. If we start adding
    small features here and there with the promise of future documentation, we
    will have lots of things accidentally breaking in the future whenever
    someone makes an innocuous change.

    It isn't that it is 4 character change. It is that matplotlib is so big!
    Remember, open source is very much about scratching your own itch. I have
    plenty of my own itches to scratch, so unless someone else has the same
    itch as yours, you should expect to shepard your patch through.

    As for the docs, perhaps that should be clarified. Your change is small, at
    first, but it is incomplete. When completed, it is something that should
    be a PR.

    The use case is that you can provide your own image data in RGB[A]
    format already and not use the current colormap feature. It allows very
    different kind of visualization - you can use all 2**24 colors. Or once
    could add the alpha channel for partial transparency in select regions.

    Why not use imshow() for this? Is your grid irregular?
    Cheers!
    Ben Root

  9. 2sn commented on Oct 14, 2012

    @2sn
    Author

    """
    example
    """

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.mlab import bivariate_normal

    def test():
    N = 1000
    x = np.exp(np.linspace(-2.0, 3.0, N))
    y = np.exp(np.linspace(-2.0, 2.0, N))

     X, Y = np.meshgrid(x, y)
     X1 = 0.5*(X[:-1,:-1] + X[1:,1:])
     Y1 = 0.5*(Y[:-1,:-1] + Y[1:,1:])
    
     ZR = bivariate_normal(X1, Y1, 0.1, 0.2, 1.27, 1.11) + 
    

    100._bivariate_normal(X1, Y1, 1.0, 1.0, 0.23, 0.72)
    ZG = bivariate_normal(X1, Y1, 0.1, 0.2, 2.27, 0.11) +
    100._bivariate_normal(X1, Y1, 1.0, 1.0, 0.43, 0.52)
    ZB = bivariate_normal(X1, Y1, 0.1, 0.2, 0.27, 2.11) +
    100._bivariate_normal(X1, Y1, 1.0, 1.0, 0.53, 0.92)
    ZA = bivariate_normal(X1, Y1, 0.1, 0.2, 3.27,-1.11) +
    100._bivariate_normal(X1, Y1, 1.0, 1.0, 0.23, 0.82)

     Z = np.ndarray(ZR.shape + (4,))
     Z[...,0] = ZR /ZR.max()
     Z[...,1] = ZG /ZG.max()
     Z[...,2] = ZB /ZB.max()
     Z[...,3] = np.exp(-ZA /ZA.max())
    
     Z = (Z*255).astype(np.uint8)
    
     fig = plt.figure()
    
     ax = fig.add_subplot(1,1,1)
     i = ax.pcolorfast(x, y, Z)
    
     # from matplotlib.image import PcolorImage
     # i = PcolorImage(ax, x, y, Z)
     # ax.images.append(i)
     # xl, xr, yb, yt = x[0], x[-1], y[0], y[-1]
     # ax.update_datalim(np.array([[xl, yb], [xr, yt]]))
     # ax.autoscale_view(tight=True)
     plt.show()
    

    On 13/10/12 21:02, Benjamin Root wrote:

    Alexander, it has been a while since you last replied. Are you still intending to put together a PR for this feature you wish to add? I am not against such a feature, provided that it includes all of the appropriate changes to documentation and test cases.

    I am still a little confused about the use-case, but I would be willing to review it when I see the entire change all at once. Note, the feature freeze for v1.2.0 has long since past, so if you were trying to get this in for that, it wasn't going to happen anyway.


    Reply to this email directly or view it on GitHub:
    #1317 (comment)

  10. 2sn commented on Oct 14, 2012

    @2sn
    Author

    Dear Ben,

    OK, I will update the documentation as well and generate a PR.

    yes, I want to plot large non-uniform images.
    (NonUniformImage is too slow)

    Best wishes,
    Alexander

    PS - I did have another pull request on text alignment that came with
    documentation, I think that is still pending as well. There was some
    discussion on github, but here was no better solution (my assessment) on
    generalizing text aliment; I plan to submit another PR that will provide
    a solution for text aligned with fixed-sized markers but require 2 new
    keywords to text - this is what I tried to avoid.

    On 13/10/12 23:36, Benjamin Root wrote:

    Alex,

    On Sunday, October 14, 2012, Alexander Heger wrote:

    Dear Benjamin,

    I have been on travel for the last 2 weeks and still have about half a
    week to go (incl. 24h travel from US tu AU).

    Well, the change is really just adding a four characters in one line and
    no documented feature breaks. Do you really need a PR for that?

    We need a PR for the change, plus docs, plus tests. If we start adding
    small features here and there with the promise of future documentation, we
    will have lots of things accidentally breaking in the future whenever
    someone makes an innocuous change.

    It isn't that it is 4 character change. It is that matplotlib is so big!
    Remember, open source is very much about scratching your own itch. I have
    plenty of my own itches to scratch, so unless someone else has the same
    itch as yours, you should expect to shepard your patch through.

    As for the docs, perhaps that should be clarified. Your change is small, at
    first, but it is incomplete. When completed, it is something that should
    be a PR.

    The use case is that you can provide your own image data in RGB[A]
    format already and not use the current colormap feature. It allows very
    different kind of visualization - you can use all 2**24 colors. Or once
    could add the alpha channel for partial transparency in select regions.

    Why not use imshow() for this? Is your grid irregular?
    Cheers!
    Ben Root


    Reply to this email directly or view it on GitHub:
    #1317 (comment)

  11. tacaswell commented on Dec 20, 2013

    @tacaswell
    Member

    @2sn Did a PR ever come out of this?

  12. 2sn commented on Dec 20, 2013

    @2sn
    Author

    Dear Tom,

    sorry, it did not. It's a small change - if there is interested, I can
    try based on the current version.

    -Alexander

    On 20/12/13 14:00, Thomas A Caswell wrote:

    @2sn Did a PR ever come out of this?


    Reply to this email directly or view it on GitHub:
    #1317 (comment)

  13. rabernat commented on Feb 11, 2014

    @rabernat

    I would be interested in using this feature.

  14. added this to the milestone on Mar 3, 2014
  15. tacaswell commented on Mar 3, 2014

    @tacaswell
    Member

    @2sn I think there is interest in a PR to implement this. We are triaging for 1.4.0 now, so if you can get it in soon (with in a week) then we can try to get it in.

  16. rabernat commented on Mar 4, 2014

    @rabernat

    Great! The workaround I found is very clunky, so I remain interested.

    On Mon, Mar 3, 2014 at 11:49 AM, Thomas A Caswell
    notifications@github.comwrote:

    @2sn https://github.com/2sn I think there is interest in a PR to
    implement this. We are triaging for 1.4.0 now, so if you can get it in soon
    (with in a week) then we can try to get it in.

    Reply to this email directly or view it on GitHubhttps://github.com//issues/1317#issuecomment-36530192
    .

  17. 2sn commented on Mar 4, 2014

    @2sn
    Author

    Dear Thomas,

    thanks for the heads up.

    Unfortunately I am too much overloaded with proposals and teaching this
    week.

    -Alexander

    On 04/03/14 03:49, Thomas A Caswell wrote:

    @2sn I think there is interest in a PR to implement this. We are triaging for 1.4.0 now, so if you can get it in soon (with in a week) then we can try to get it in.


    Reply to this email directly or view it on GitHub:
    #1317 (comment)

  18. earthastronaut commented on May 20, 2014

    @earthastronaut

    For what it's worth, this would be a useful feature.

  19. 2sn commented on May 21, 2014

    @2sn
    Author

    yes. If you are interested, I made some extended color function library
    at 2sn.org/python3/color.py You can use the pcolorimage defined at the
    bottom.

    On 21/05/14 04:47, astrodsg wrote:

    For what it's worth, this would be a useful feature.


    Reply to this email directly or view it on GitHub:
    #1317 (comment)

  20. petehuang commented on Dec 30, 2016

    @petehuang
    Contributor

    PR still welcome for this.

    Recommend labeling with new-contributor-friendly and Difficulty: Low

  21. modified the milestones: , on Sep 24, 2017
  22. efiring commented on May 23, 2019

    @efiring
    Member

    Closed by #8690.

  23. modified the milestones: needs sorting, v3.1.0 on May 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions