Skip to content

Fixed large pcolormesh rendering with bbox_inches='tight'. #4045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2015

Conversation

pelson
Copy link
Member

@pelson pelson commented Jan 28, 2015

Replaces #4017.
Closes #3095.

@pelson pelson added this to the v1.4.3 milestone Jan 28, 2015
@tacaswell
Copy link
Member

This does not account for having a non-trivial offset array.

@pelson
Copy link
Member Author

pelson commented Jan 28, 2015

This does not account for having a non-trivial offset array.

Can you provide a simple example? I've never properly understood the offsets in some collection contexts.

@tacaswell
Copy link
Member

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 1)

X, Y = np.meshgrid(range(25), range(20))

C = np.random.rand(*X.shape)
offsets = (np.random.randn(2, 24*19) 

ax.pcolormesh(X, Y, C, offsets=offsets, transOffset=ax.transData)

so

which has an odd extra shift from (0, 0) to (3, 2.3) for the lower left. I am also not really clear on why this is useful, but it is something that the code does.

@tacaswell
Copy link
Member

The code for this in agg ends up in c++ layer and gets really hairy really fast.

@tacaswell
Copy link
Member

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 1)

X, Y = np.meshgrid(range(25), range(20))

C = np.random.rand(*X.shape)
offsets = np.random.randn(24*19, 2) * .1

ax.pcolormesh(X, Y, C)#, offsets=offsets, transOffset=ax.transData)
fig.savefig('so.png', bbox_inches='tight')

gives me

<ipython-input-26-382f89238547> in <module>()
----> 1 exec(compile(open('/tmp/ipython-5366aRU.py').read(), '/tmp/ipython-5366aRU.py', 'exec')) # PYTHON-MODE

/tmp/ipython-5366aRU.py in <module>()

/home/tcaswell/.virtualenvs/mpl_master_3k/lib/python2.7/site-packages/matplotlib-1.4.x.dev0-py2.7-linux-x86_64.egg/matplotlib/figure.pyc in savefig(self, *args, **kwargs)
   1474             self.set_frameon(frameon)
   1475 
-> 1476         self.canvas.print_figure(*args, **kwargs)
   1477 
   1478         if frameon:

/home/tcaswell/.virtualenvs/mpl_master_3k/lib/python2.7/site-packages/matplotlib-1.4.x.dev0-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt5agg.pyc in print_figure(self, *args, **kwargs)
    159 
    160     def print_figure(self, *args, **kwargs):
--> 161         FigureCanvasAgg.print_figure(self, *args, **kwargs)
    162         self.draw()
    163 

/home/tcaswell/.virtualenvs/mpl_master_3k/lib/python2.7/site-packages/matplotlib-1.4.x.dev0-py2.7-linux-x86_64.egg/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2166                 bbox_filtered = []
   2167                 for a in bbox_artists:
-> 2168                     bbox = a.get_window_extent(renderer)
   2169                     if a.get_clip_on():
   2170                         clip_box = a.get_clip_box()

/home/tcaswell/.virtualenvs/mpl_master_3k/lib/python2.7/site-packages/matplotlib-1.4.x.dev0-py2.7-linux-x86_64.egg/matplotlib/collections.pyc in get_window_extent(self, renderer)
    208         # TODO:check to ensure that this does not fail for
    209         # cases other than scatter plot legend
--> 210         return self.get_datalim(transforms.IdentityTransform())
    211 
    212     def _prepare_points(self):

/home/tcaswell/.virtualenvs/mpl_master_3k/lib/python2.7/site-packages/matplotlib-1.4.x.dev0-py2.7-linux-x86_64.egg/matplotlib/collections.pyc in get_datalim(self, transData)
   1707 
   1708     def get_datalim(self, transData):
-> 1709         return (self.get_transform() - transData).transform_bbox(self._bbox)
   1710 
   1711     @staticmethod

/home/tcaswell/.virtualenvs/mpl_master_3k/lib/python2.7/site-packages/matplotlib-1.4.x.dev0-py2.7-linux-x86_64.egg/matplotlib/transforms.pyc in transform_bbox(self, bbox)
   1352         requirement for matplotlib figures), see :class:`TransformedBbox`.
   1353         """
-> 1354         return Bbox(self._transform.transform(bbox.get_points()))
   1355 
   1356     def get_affine(self):

AttributeError: 'CompositeGenericTransform' object has no attribute '_transform'

Note, for smarter transforms including caching (a common
requirement for matplotlib figures), see :class:`TransformedBbox`.
"""
return Bbox(self._transform.transform(bbox.get_points()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line should be return Bbox(self..transform(bbox.get_points()))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

@pelson
Copy link
Member Author

pelson commented Jan 29, 2015

Thanks for the example @tacaswell. I did not know you could do that!
I'll take a look at dealing with the offsets, but ultimately if this is holding up a release, I'd be comfortable trading a major performance bug with pcolormesh, for a minor cut-off known bug with a not-well-used part of the same routine.

@tacaswell
Copy link
Member

I concur about the trade off.

@tacaswell tacaswell added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Jan 31, 2015
@tacaswell tacaswell merged commit 44ff97e into matplotlib:v1.4.x Jan 31, 2015
tacaswell added a commit that referenced this pull request Jan 31, 2015
BUG : Fix run-away memory usage with pcolormesh + bbox_inches='tight'
@pelson pelson deleted the quadmesh_big branch February 1, 2015 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants