Skip to content

set_segments(get_segments()) makes lines coarse #20551

Closed

Description

@nschloe

After plotting with contourf, I would like to retrieve the lines and manipulate them. Unfortunately, I noticed that the result is much coarser than without manipulation. In fact, a simple lc.set_segments(lc.get_segments()) has this effect. I would have expected this does nothing at all.

MWE:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-1.1, 1.1, 100)
y = np.linspace(-1.1, 1.1, 100)

X, Y = np.meshgrid(x, y)
Z = X ** 2 + Y ** 2

c = plt.contour(X, Y, Z, levels=[1.0], colors="k")

# reset segments
lc = c.collections[0]
segments = lc.get_segments()
lc.set_segments(segments)

plt.gca().set_aspect("equal")
plt.show()
sc1 sc2
default with reset segments

This is with mpl 3.4.2.

Activity

  1. nschloe commented on Jun 30, 2021

    @nschloe
    ContributorAuthor

    Aha: There is

    c.allsegs
    

    which can be manipulated instead.

  2. dmatos2012 commented on Jun 30, 2021

    @dmatos2012
    Contributor

    Hi @nschloe, has your problem been resolved?

  3. tacaswell commented on Jun 30, 2021

    @tacaswell
    Member

    Interesting between 3.4.2 and the default branch this has changed from a LineCollection to a PathCollection which notable does not even have a get_segments.

  4. nschloe commented on Jun 30, 2021

    @nschloe
    ContributorAuthor

    get_segments() was wrong apparently, so problem solved for me.

  5. tacaswell commented on Jun 30, 2021

    @tacaswell
    Member

    @nschloe You identified a different bug which is why does lc.get_segments() aggressively simplify the curve ?!

    Internally all Collection flavors boil down to calling renderer.draw_path_collection and all of the sub-classes primarily provide nicer user-facing APIs to fabricate the paths that will be passed down to the renderer. In LineCollection rather than tracking both the user supplied data and the internal Path objects, we just keep the Path objects and re-extract segments on demand. To do this we use Path.iter_segments with defaults to asking the path if it should simplify the path (that is drop points that do not matter which is in turn defined by if the deflection away from "straight" is greater than some threshold). The Path objects we are holding have values in data-space, but the default value of "should simplify" and "what is the threshold for 'not mattering'" are both set so that they make sense once the path has been converted to pixel space (True and 1/9). In LineCollection.get_segments we are not passing anything special so we are cleaning the path to only include points that make the path deviate by ~0.1111 (which eye-balling looks about right). I think the fix here is to pass simplify=False in LineColleciton.get_segments().

  6. added this to the v3.5.0 milestone on Jun 30, 2021
  7. tacaswell commented on Jun 30, 2021

    @tacaswell
    Member

    And the change from LineCollection -> PathCollection was 04f4bb6 and is justified.

  8. added a commit that references this issue on Jul 6, 2021
    5570642
  9. modified the milestones: v3.5.0, v3.4.3 on Jul 7, 2021
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