Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Tables: Fix get_window_extent for table #1418

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

Closed
wants to merge 1 commit into from

Conversation

jenshnielsen
Copy link
Member

This allows a table to be added to bbox_extra_artists.
See ipython/ipython#2508

Got rid of bbox_all and replaced it with Bbox.union.
Furthermore the get_window_extent is a method on the cells not the key of the cell
so change that to avoid an attribute error.

@dmcdougall
Copy link
Member

bbox_all was changed to BBox.union in #1409, so you'll need to rebase this to pull in those changes.

@jenshnielsen
Copy link
Member Author

Sorry. Forgot to update my master branch. This should be right.

@dmcdougall
Copy link
Member

Sorry. Forgot to update my master branch.

Not at all :)

@@ -279,7 +279,7 @@ def get_children(self):

def get_window_extent(self, renderer):
'Return the bounding box of the table in window coords'
boxes = [c.get_window_extent(renderer) for c in self._cells]
boxes = [self._cells[c].get_window_extent(renderer) for c in self._cells]
Copy link
Member

Choose a reason for hiding this comment

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

Is this line longer than 79 characters?

Copy link
Member

Choose a reason for hiding this comment

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

Also, this confused me until I realised that self._cells is a dictionary, not a list.

In [1]: d = {'a': 1, 'b': 2, 'c': 3}

In [2]: print [c for c in d]
['a', 'c', 'b']

In [3]: print [c for c in d.keys()]
['a', 'c', 'b']

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you think we should add a comment explaining that it is a dict?

@dmcdougall
Copy link
Member

@jenshnielsen The diff for e427b34 doesn't contain any reference bbox_extra_artists. Am I missing something?

@jenshnielsen
Copy link
Member Author

The issue came from this ipython issue ipython/ipython#2508. The table is not added to the default artists taken into account by bbox_inches='tight' clipping the table in the ipython notebook.
Then calling savefig with the table added to the bbox_extra_artists like this:
fig.savefig('prueba.png', dpi=300, bbox_inches='tight', bbox_extra_artists = [ the_table ])
resulted in an assertion error in get_window_extent which this pull request fixes.

@dmcdougall
Copy link
Member

The issue came from this ipython issue ipython/ipython#2508. The table is not added to the default artists taken into account by bbox_inches='tight' clipping the table in the ipython notebook.
Then calling savefig with the table added to the bbox_extra_artists like this:
fig.savefig('prueba.png', dpi=300, bbox_inches='tight', bbox_extra_artists = [ the_table ])
resulted in an assertion error in get_window_extent which this pull request fixes.

Ah, awesome! Thanks.

@@ -279,7 +279,8 @@ def get_children(self):

def get_window_extent(self, renderer):
'Return the bounding box of the table in window coords'
boxes = [c.get_window_extent(renderer) for c in self._cells]
boxes = [self._cells[c].get_window_extent(renderer)
for c in self._cells]
Copy link
Member

Choose a reason for hiding this comment

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

Either a comment, or replace

for c in self._cells -> for c in _self.cells.keys()

Which do you think is better?

@dmcdougall
Copy link
Member

There is a bigger problem here. Consider the following example:

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import numpy as np

a = np.array([[ 100.        ,  152.84493519,  233.63122532,  263.7018431 ,
                259.22927686,  243.56305545],
              [ 100.        ,  147.64885749,  194.26319507,  156.2985505 ,
                169.1050851 ,  124.84312468],
              [ 100.        ,  195.46940792,  273.37157492,  296.54100691,
                271.93708044,  358.30174259],
              [ 100.        ,  216.44727433,  308.30389994,  243.70797244,
                335.3325307 ,  396.22671612]])
fig = plt.figure()
ax = fig.add_subplot(1,1,1)

y = np.mean(a, axis=0)
ind = range(len(y))
ax.bar(ind, y, facecolor='#777777',
       align='center', ecolor='black',
       bottom=4)

labels = ['a', 'b', 'c', 'd', 'e', 'f']
rowlab = ['row1', 'row2', 'row3', 'row4']
ax.set_xticklabels(labels)
ax.xaxis.set_visible(False)
the_table = ax.table(cellText=a, colLabels=labels, rowLabels=rowlab)

fig.savefig('table1.png', bbox_inches='tight')
fig.savefig('table2.png', bbox_inches='tight', bbox_extra_artsits=[the_table])

I get the following output for table1.png

table1

And for table2.png

table2

For some reason, it appears as though the bbox_extra_artists isn't taking the union of the bounding boxes properly.

@dmcdougall
Copy link
Member

@jenshnielsen I confirm your patch fixes the original error. This bounding box issue is separate, in my opinion, so don't worry about addressing it in this branch. I'll open a separate issue with the bounding box problem in it.

… bbox_extra_artists.

The get_window_extent is a method on the cells not the key of the cells.
@jenshnielsen
Copy link
Member Author

I have a fix for the bounding box issue as well. Will submit another pull request after this one

@jenshnielsen
Copy link
Member Author

The issue is that when using bbox_extra_artist it forgets about the original bbox so another union is needed.

@dmcdougall
Copy link
Member

@jenshnielsen Great! The issue is #1419.

@jenshnielsen
Copy link
Member Author

The fix for #1419 should be in #1420

@dmcdougall
Copy link
Member

Closing this in favour of #1425.

@dmcdougall dmcdougall closed this Oct 22, 2012
dmcdougall added a commit that referenced this pull request Oct 23, 2012
@jenshnielsen jenshnielsen deleted the table branch July 20, 2014 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants