Skip to content

axis limits don't update on add_artist #19290

Open

Description

@nschloe

MWE

from matplotlib.patches import Circle
import matplotlib.pyplot as plt

# draw circle outside of unit square (default limits)
c = Circle(xy=[3.0, 1.0], radius=0.5)
plt.gca().add_artist(c)

plt.show()

Result:
screenshot

Activity

  1. timhoffm commented on Jan 13, 2021

    @timhoffm
    Member

    Usage (DOC, API)

    You should use add_patch() instead of add_artist().

    add_artist states:

    Use add_artist only for artists for which there is no dedicated "add" method

    I agree that docs could be more explicit here. (And maybe the API is not that great in that we burden the "right" way of adding more specialized artists on the user).

    Bug

    In principle add_patch() should update the limits. However this does not happen on an empty plot. Only if we have plotted something else before the updates happen correctly.

    from matplotlib.patches import Circle
    import matplotlib.pyplot as plt
    
    ax = plt.gca()
    ax.plot(0, 0, 'o')  # update to include circle limits only happens if we have plotted this before.
    # draw circle outside of unit square (default limits)
    c = Circle(xy=[3.0, 1.0], radius=0.5)
    ax.add_patch(c)
    
    plt.show()
    
  2. QuLogic commented on Jan 13, 2021

    @QuLogic
    Member

    This is on purpose/known; from the docs:

    Use add_artist only for artists for which there is no dedicated "add" method; and if necessary, use a method such as update_datalim to manually update the dataLim if the artist is to be included in autoscaling.

  3. timhoffm commented on Jan 14, 2021

    @timhoffm
    Member

    @QuLogic The docs are not very precise here: If you use add_patch(), the limit updated is cared for (modulo the bug I've described above).

  4. nschloe commented on Jan 14, 2021

    @nschloe
    ContributorAuthor

    You should use add_patch() instead of add_artist().

    I never realized that. So far, I've always used add_artist and it seems to be quite common, too. (See
    the answers on https://stackoverflow.com/questions/9215658/plot-a-circle-with-pyplot.) I'll go ahead and spread the word.

  5. timhoffm commented on Jan 14, 2021

    @timhoffm
    Member

    I'm considering changing this, so that add_patch will automatically dispatch to the correct function. IMHO users should not have to care about which function to call for which artist.

  6. added this to the v3.5.0 milestone on Jan 21, 2021
  7. tacaswell commented on Jan 21, 2021

    @tacaswell
    Member

    This may be fixed by #18216

  8. modified the milestones: v3.5.0, v3.6.0 on Sep 25, 2021
  9. modified the milestones: v3.6.0, v3.7.0 on Aug 24, 2022
  10. gorogm commented on Nov 1, 2022

    @gorogm

    This may be fixed by #18216

    Doesn't seem to be fixed by that :(

  11. modified the milestones: v3.7.0, v3.7.1 on Feb 14, 2023
  12. modified the milestones: v3.7.1, v3.7.2 on Mar 4, 2023
  13. modified the milestones: v3.7.2, v3.7.3 on Jul 5, 2023
  14. modified the milestones: v3.7.3, v3.8.0 on Sep 9, 2023
  15. removed this from the v3.8.0 milestone on Sep 15, 2023
  16. carlosgmartin commented on Nov 10, 2025

    @carlosgmartin
    Contributor

    In principle add_patch() should update the limits. However this does not happen on an empty plot. Only if we have plotted something else before the updates happen correctly.

    This is very weird behavior. What's the reason for this?

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