-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Conversation
|
Sorry. Forgot to update my master branch. This should be right. |
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] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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']
There was a problem hiding this comment.
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?
@jenshnielsen The diff for e427b34 doesn't contain any reference bbox_extra_artists. Am I missing something? |
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. |
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] |
There was a problem hiding this comment.
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?
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 And for For some reason, it appears as though the |
@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.
I have a fix for the bounding box issue as well. Will submit another pull request after this one |
The issue is that when using bbox_extra_artist it forgets about the original bbox so another union is needed. |
@jenshnielsen Great! The issue is #1419. |
Closing this in favour of #1425. |
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.