Skip to content

Add MATLAB-Like contourc #367

Closed
Closed

Description

@ddale

Original report at SourceForge, opened Sun Oct 10 22:25:55 2010

This would probably be in mlab. I haven't checked but the count plotting functions probably use a private function similar to contourc behind the scenes. Having access to this would be great. Currently I am creating a figure. drawing a contour plot, than closing the figure.

Activity

  1. brendanarnold commented on Jun 30, 2011

    @brendanarnold

    I'd like to say that I too would appreciate this, and judging by the mailing list so would a few others who would like to be able to extract points from a contour.

  2. pelson commented on Jun 30, 2012

    @pelson
    Member

    You can get the paths that a contour would produce with:

    import matplotlib.pyplot as plt
    import numpy
    
    
    data = numpy.arange(1200).reshape(30, 40)
    cs = plt.contour(data, levels=[10])
    
    for collection in cs.collections:
        for path in collection.get_paths():
            print path
    

    Does this do what a contourc function might do?

  3. dmcdougall commented on Aug 23, 2012

    @dmcdougall
    Member

    @pelson Yes, but Matlab's actual contourc does not require you to produce a plot. It's basically a 'low-level' contour generator, storing the result in an array rather than plotting it. See here for details.

  4. WeatherGod commented on Aug 23, 2012

    @WeatherGod
    Member

    Note, there is a subtle difference between the contouring results from contour and contourf (mostly in domain edge handling and masked areas). So, a contourc function would have to know which one of these the user would want.

    Essentially, contour() and contourf() are creating a ContourSet object, which is fairly close to what we want, but it does greatly blur the boundary between the low-level processing and the figure/axes/coloring parts of the process. Maybe some work could be done to more cleanly delineate the plot-making part and the calculation/processing part.

  5. ianthomas23 commented on Aug 23, 2012

    @ianthomas23
    Member

    The contour calculations are coordinated by the method _get_allsegs_and_allkinds of classes derived from ContourSet (currently QuadContourSet and TriContourSet). These methods are fairly self-contained, but do depend on various things being set up correctly before they are called.

    I frequently use code like @pelson's example above and have passed on similar examples to others. Although you have to put up with the overhead of creating a dummy Axes, there is no unwanted graphical output. I have never benchmarked exactly how much of an overhead there is but my suspicion is that it is negligible for all but the simplest of examples which will be lightning fast anyway.

    I always emphasize that care is needed with the example code. Each path may be just an array of vertices corresponding to a single open or closed polygon, which is what most people expect, but a path may also correspond to 2 or more polygons if the member codes is set to indicate at which indices the polygons start. Naive use of the paths may not be what is wanted; often the use of path.to_polygons() is better.

  6. pelson commented on Oct 18, 2012

    @pelson
    Member

    @ianthomas23 : I found your write-up very useful. Do you have any appetite to wrap this up in a function, or do you think that is asking for trouble compared to the example that I gave?

  7. ianthomas23 commented on Oct 18, 2012

    @ianthomas23
    Member

    @pelson: I'd like to put this on hold for a few weeks more.

    I am currently working on a rewrite of the behind-the-scenes C code to add extra functionality and improve performance of contour and contourf. Depending on how it goes and the approval of the community, it may or may not result in changes to how the contour results are stored and hence how this contourc idea would be implemented. I'd prefer to concentrate on this at the moment, and then someone (possibly me!) can look at improvements to the python contour code which may help with contourc and perhaps the other issue recently discussed about how a ContourSet is not an Artist. I am only a couple of weeks away from having something concrete to demonstrate.

  8. WeatherGod commented on Oct 18, 2012

    @WeatherGod
    Member

    @ianthomas23, let me know when you get to the point where you want closer reviews of results. I have a huge dataset of gridded fields, and work very closely to the results of contourf (converting them into polygons for shapefiles).

  9. mdboom commented on Oct 22, 2012

    @mdboom
    Member

    @ianthomas23: Thanks for this! contour has needed some rationalization for a long time.

  10. aaren commented on Nov 7, 2013

    @aaren

    Disclaimer: Do not do this!

    Maybe this isn't good practice (see below) but for now you can call the
    underlying C function directly, which avoids doing any drawing. I think this is
    the closest to a contourc like function.

    import numpy as np
    from matplotlib._cntr import Cntr
    
    # some exciting test function
    x = np.linspace(-3, 3, 100)
    A, B = np.meshgrid(x, x)
    fab = np.sin(A**2) + np.cos(B**2)
    
    # the actual call 
    contour = Cntr(A, B, fab)
    # let's find where f(a, b) = 0
    zero_traces = contour.trace(0)
    
    # bundle all the points together (ignoring 'allkinds' type arrays)
    zero_contour = np.row_stack(traces[:len(traces) / 2])
    

    It would be easy to map to this function directly. @ianthomas23 are you
    planning to have the same interface for the c code as for plt.contour? (less
    all the drawing stuff)

  11. ianthomas23 commented on Nov 7, 2013

    @ianthomas23
    Member

    @aaren: Please do not do this nor encourage others to. Accessing the underlying C code in this manner is completely unsupported and bypasses reams of consistency checks that are very important. Simple examples may well work fine, but more complicated ones could cause a segmentation violation or produce garbage output. Use @pelson's code above along with path.to_polygons().

  12. mfouesneau commented on Mar 4, 2014

    @mfouesneau

    I hacked an attempt here: https://github.com/mfouesneau/mpl_contourc
    It is based on the ContourSet and QuadContourSet classes but only computes the contours. So in principle, it keeps the consistency with mpl checks and current implementation

  13. tacaswell commented on Mar 5, 2014

    @tacaswell
    Member

    @mfouesneau Could you put that in a PR? That is the path of least resistance to get your code into mpl.

  14. mfouesneau commented on Mar 5, 2014

    @mfouesneau

    @tacaswell sorry but PR meaning?

  15. efiring commented on Mar 5, 2014

    @efiring
    Member

    @tacaswell, simply putting it in a PR might be premature, since I think it represents quite a bit of code duplication. We need to consider whether this can be part of a refactoring, so that there is little or no duplication required to provide the contour paths and the full contour plot.
    @mfouesneau, "PR" is "Pull Request".

  16. 7 remaining items

  17. mfouesneau commented on Aug 18, 2014

    @mfouesneau

    If you want to merge skimage into mpl yes.

    The version I made (link above) is only a re-write of what was in mpl at that time. It only provides a simple intermediate class as QuadContourSet etc, but offers access to the polygons.

  18. tacaswell commented on Aug 18, 2014

    @tacaswell
    Member

    Sorry I wasn't clear. I meant skimage already provides this functionality so it is less important that mpl provides it as well, I am not suggesting merging (or even depending on) skimage.

    If you want to do the refactoring we will happily review/merge it, but this is not very high on my list of priorities right now.

  19. mfouesneau commented on Aug 18, 2014

    @mfouesneau

    What would be most efficient? writing a class and making ContourSet a child or what?

  20. petehuang commented on Dec 31, 2016

    @petehuang
    Contributor

    @tacaswell Sounds like we should make a call on whether or not this functionality should be in MPL and close if not

  21. rmchurch commented on Jan 12, 2017

    @rmchurch

    One argument against relying on skimage is its only for 2d datasets, whereas MPL is in position (with some refactoring) to make this functionality possible for irregular, triangulated data also (with tricontour).

  22. mrshannon commented on Jul 20, 2017

    @mrshannon

    This would be difficult to refactor given the current structure. Currently most of the work of preparing the data and handing it off to the C/C++ code is done in QuadContourSet for contour and TriContourSet for tricontour, both of which are children of ContourSet. But ContourSet handles some of the calculation setup as well as the graphical side of things. To separate out calculation from plotting all plotting related tasks would have to be removed from ContourSet and a new mixin created that re-introduced it. Then the hierarchy would look something like:

    ContourSet -> QuadContourSet ---> QuadContourSetPlotter
    ContourPlotter ----------------/ 
    

    and

    ContourSet -> TriContourSet ---> TriContourSetPlotter
    ContourPlotter ---------------/ 
    

    Also, another argument against using skimage is that it does not support masked arrays.

  23. modified the milestones: , needs sorting on Oct 6, 2022
  24. rcomer commented on Jul 7, 2023

    @rcomer
    Member

    Is this one now addressed by ContourPy?
    https://contourpy.readthedocs.io/en/v1.1.0/

  25. ianthomas23 commented on Jul 8, 2023

    @ianthomas23
    Member

    It is indeed, thanks for the reminder @rcomer.

    I am closing this issue, a mere 12 years after it was opened.

  26. rmchurch commented on Jul 8, 2023

    @rmchurch

    Looks like ContourPy does not solve for tricontour setups

  27. ianthomas23 commented on Jul 8, 2023

    @ianthomas23
    Member

    Indeed, it is called ContourPy not TricontourPy.

  28. removed this from the future releases milestone on Sep 11, 2023
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions