Skip to content

too large file size created by the errorbar of matplotlib #3345

Closed

Description

@ellot

I was tring to plot a lot of data with errors using errobar of matplotlib. The eps or pdf file is very large, much larger than created by IDL. I have tried to use rasterized=True, but it does not work. If I only use plt.plot(x,y) to plot the data without errors, it can create a much smaller file. I am using OSX 10.9 and Anaconda. Here is the example of my code

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
x = np.linspace(-5., 5., 5000)
y = 3 * np.exp(-0.5 * (x - 1.3)2 / 0.82)
y += np.random.normal(0., 0.2, x.shape)
f = plt.figure(0)
plt.errorbar(x,y, yerr=0.2,fmt='')
f.savefig('test.pdf')

Activity

  1. tacaswell commented on Aug 6, 2014

    @tacaswell
    Member

    what does matplotlib.__version__ give?

  2. added this to the v1.4.x milestone on Aug 6, 2014
  3. ellot commented on Aug 14, 2014

    @ellot
    Author

    I use matplotlib v1.3.1.
    I have tried this in Fedora with matplotlib v1.3.1, it also has the same problem.
    Is it fixed in higher version?

  4. jenshnielsen commented on Aug 14, 2014

    @jenshnielsen
    Member

    Could you please give some numbers for reference?

    I would suggest testing the Cairo backend as a work around:

    import matplotlib
    matplotlib.use('cairo')
    import numpy as np
    import matplotlib.pyplot as plt
    np.random.seed(0)
    x = np.linspace(-5., 5., 5000)
    y = 3 * np.exp(-0.5 * (x - 1.3)**2 / 0.8**2)
    y += np.random.normal(0., 0.2, x.shape)
    f = plt.figure(0)
    plt.errorbar(x,y, yerr=0.2,fmt='')
    f.savefig('test.pdf')

    These are the results that I get from testing your plot (MPL 1.4.x branch). I don't have access to IDL so I have no clue about the sizes that it generates.

    • Errorbar 1.505 MB
    • Errorbar Cairo 186 KB
    • Regular Plot 59 KB
    • Regular Plot Cairo 33 KB

    Another thing is that the errorbars in this plot makes no sense IMHO since they are all on top of each other. I would reduce the number of errorbars with the errorevery argument to make them separable. That will also likely reduce the file size.

  5. ellot commented on Aug 14, 2014

    @ellot
    Author

    The original backend is MacOSX, the file size is about 1.5 MB. If I create an eps file and then use ps2pdf, the pdf file size is about 232 KB.

  6. ellot commented on Aug 14, 2014

    @ellot
    Author

    I have tried use cairo backend with OSX 10.9, but I got an error
    "Fatal Python error: PyThreadState_Get: no current thread
    Abort trap: 6"

  7. jenshnielsen commented on Aug 14, 2014

    @jenshnielsen
    Member

    How large is the IDL file?

    You probably don't have the right dependencies for the cairo backend. You will need either cairocffi or pycairo installed and working but without some more details about how you installed matplotlib and python it is impossible to know. The cairo backend certainly works on OS X 10.9

  8. tacaswell commented on Aug 14, 2014

    @tacaswell
    Member

    @jkseppan I have a hunch that is related to a bug that was reported to me verbally, but hasn't made it into the tracker yet.

    When saving collections the pdf backend creates a new nested group (not sure if that is the correct pdf term) for every element instead of putting all of the elements in a single group.

    I also suspect that this is related to the some what funny way we use graphic contexts.

  9. ellot commented on Aug 15, 2014

    @ellot
    Author

    IDL can not create PDF file directly. It can create an eps file, and then I use ps2pdf to get a PDF file about 180 KB.

  10. ellot commented on Aug 15, 2014

    @ellot
    Author

    I use Anaconda, the matplotlib is included in Anaconda.

  11. jenshnielsen commented on Aug 15, 2014

    @jenshnielsen
    Member

    Thanks

    It does't look like conda packs pycairo or cairocffi so that will not work right away.

  12. jkseppan commented on Aug 18, 2014

    @jkseppan
    Member

    It seems that the errorbars get rendered using separate XObjects instead of just outputting the moveto and lineto operations in the page, and that blows up the file size.

  13. mdboom commented on Aug 25, 2014

    @mdboom
    Member

    @jkseppan: That makes sense. Markers (which include ticks) all use XObjects, which normally results in a reduction in file size when those markers are used frequently. But maybe ticks are simple enough that not using XObjects is actually better.

  14. added 2 commits that reference this issue on Sep 7, 2014
    017939d
    3d38633
  15. jkseppan commented on Sep 7, 2014

    @jkseppan
    Member

    See #3485 for a proposed fix.

  16. jkseppan commented on Sep 7, 2014

    @jkseppan
    Member

    There could be a deeper problem somewhere. Why is draw_path_collection being called with collections of one path each? It seems that there is code in Collection.draw that is intended to call draw_markers for such collections.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions