Skip to content

Off-axes scatter() points unnecessarily saved to PDF when coloured #2488

Closed

Description

@mspacek

Scatter plotting a bunch of points while specifying the colour of each point, then changing the axes limits so none of the points are visible, and then saving the result to a PDF, results in a file just as big as if the points were all visible within their default axes limits. This doesn't seem to happen if the colour arg isn't passed to scatter(). I haven't tried, but specifying other kinds of point specific attributes, like size, might also trigger the problem . Also, I haven't tried any of the other vector backends, but they may be affected as well.

This came out of #2423.

Example code:

import numpy as np
x = np.random.random(20000)
y = np.random.random(20000)
c = np.random.random(20000)

figure()
scatter(x, y)
pyplot.savefig('scatter.pdf')
xlim(2, 3) # move axes away for empty plot
pyplot.savefig('scatter_empty.pdf')
'''
file sizes in bytes:
scatter.pdf:       324187
scatter_empty.pdf:   6617
'''
figure()
scatter(x, y, c=c)
pyplot.savefig('scatter_color.pdf')
xlim(2, 3) # move axes away for empty plot
pyplot.savefig('scatter_color_empty.pdf')
'''
file sizes in bytes:
scatter_color.pdf:       410722
scatter_color_empty.pdf: 413541
'''

Activity

  1. tacaswell commented on Jan 17, 2014

    @tacaswell
    Member

    Closed as I believe that #2423 fixed this problem.

  2. mspacek commented on Jan 18, 2014

    @mspacek
    ContributorAuthor

    I was under the impression from @mdboom that scatter() had a very different code path, and fixing this for scatter would be much more difficult than for plot():

    #2423 (comment)

    Has scatter been tested? I should pull from git to test it out again, but I'm not quite feeling up for that right now...

  3. tacaswell commented on Jan 20, 2014

    @tacaswell
    Member

    @mspacek Yeah, you are right, closed this erroneously.

  4. mdboom commented on Jan 21, 2014

    @mdboom
    Member

    Yes -- scatter is so much more flexible -- each item can have its own transform, and the only way to determine (in the general case) if a patch is off the axes is to actually transform all of its points anyway, which probably doesn't result in terribly large savings (though I suppose one save the stroking time). If you pre-determine that all of the transformations scale/translation without rotation/skew, one could simply transform the bounding box of the patch to determine whether it's outside of the image, and this would probably be fast enough to be worth the effort.

  5. petehuang commented on Jan 7, 2017

    @petehuang
    Contributor

    Sharing my results on 1.5.3 using TkAgg:

    01/06/2017  11:31 PM           324,044 scatter.pdf
    01/06/2017  11:31 PM           454,639 scatter_color.pdf
    01/06/2017  11:31 PM           457,371 scatter_color_empty.pdf
    01/06/2017  11:31 PM           326,419 scatter_empty.pdf
    
  6. modified the milestones: , on Oct 3, 2017
  7. github-actions commented on Mar 3, 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!

  8. mspacek commented on May 2, 2023

    @mspacek
    ContributorAuthor

    This issue remains in matplotlib 3.7.1, Python 3.8, Qt5QAgg backend:

    import matplotlib.pyplot as plt
    import numpy as np
    x = np.random.random(20000)
    y = np.random.random(20000)
    c = np.random.random(20000)
    
    plt.figure()
    plt.scatter(x, y)
    plt.savefig('scatter.pdf')
    plt.xlim(2, 3) # move axes away for empty plot
    plt.savefig('scatter_empty.pdf')
    '''
    file sizes in bytes:
    scatter.pdf:       327682
    scatter_empty.pdf:   6313
    '''
    plt.figure()
    plt.scatter(x, y, c=c)
    plt.savefig('scatter_color.pdf')
    plt.xlim(2, 3) # move axes away for empty plot
    plt.savefig('scatter_color_empty.pdf')
    '''
    file sizes in bytes:
    scatter_color.pdf:       582991
    scatter_color_empty.pdf: 583963  <--- should be much smaller
    '''
  9. 11 remaining items

  10. added
    keepItems to be ignored by the “Stale” Github Action
    on May 11, 2024
  11. henry4ni commented on Nov 1, 2025

    @henry4ni

    Hello! Is this issue still unresolved?
    I’m interested in contributing to this issue as my first open-source contribution.

  12. rcomer commented on Nov 1, 2025

    @rcomer
    Member

    @henry4ni anyone is welcome to work on anything but please note that "Good First Issue” means this is suitable for someone new to Matplotlib, but not necessarily new to coding/python in general. "Medium difficulty" means it is not suitable for very inexperienced developers.

  13. added 7 commits that reference this issue on Nov 12, 2025
    cd4220b
    f2fd5c2
    8dbcc97
    4104dd5
    f121a95
    426e820
    f9f329e
  14. removed
    keepItems to be ignored by the “Stale” Github Action
    on Feb 5, 2026
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

    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesPerformancebackend: pdf🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions