Skip to content
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

axes.legend TypeError in v2.2.0 #10784

Closed
ewels opened this issue Mar 14, 2018 · 3 comments · Fixed by #10787
Closed

axes.legend TypeError in v2.2.0 #10784

ewels opened this issue Mar 14, 2018 · 3 comments · Fixed by #10787
Milestone

Comments

@ewels
Copy link

ewels commented Mar 14, 2018

This is a follow up to #10476

After v2.2.0 came out, I expected my tool to start working again as the above bug had been fixed. Unfortunately, my minimal example was too minimal and there was a second, very similar, bug that was missed.

It seems that adding the argument ncol=5 to the axes.legend call in the previous minimal example triggers the error that I'm now seeing.

Minimal example:

#!/usr/bin/env python

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
print matplotlib.__version__ # 2.2.0rc1

fig = plt.figure()
axes = fig.add_subplot(111)

d1 = [29388871, 12448, 40, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
d2 = [28396236, 981940, 22171, 537, 123, 88, 41, 42, 40, 26, 26, 84, 6, 2, 0, 0, 0, 0, 0]
axes.plot(d1, label='series 1')
axes.plot(d2, label='series 2')
axes.legend(ncol=5, mode='expand')

#### THIS IS WHERE THE CRASH HAPPENS
plt.tight_layout(rect=[0,0.08,1,0.92])

fig.savefig('test.png', format='png', bbox_inches='tight')
plt.close(fig)
$ python minimal_example.py
2.2.0
Traceback (most recent call last):
  File "minimal_example.py", line 20, in <module>
    plt.tight_layout(rect=[0,0.08,1,0.92])
  File "/Users/ewels/miniconda2/envs/work/lib/python2.7/site-packages/matplotlib/pyplot.py", line 1366, in tight_layout
    fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
  File "/Users/ewels/miniconda2/envs/work/lib/python2.7/site-packages/matplotlib/figure.py", line 2275, in tight_layout
    pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
  File "/Users/ewels/miniconda2/envs/work/lib/python2.7/site-packages/matplotlib/tight_layout.py", line 328, in get_tight_layout_figure
    pad=pad, h_pad=h_pad, w_pad=w_pad)
  File "/Users/ewels/miniconda2/envs/work/lib/python2.7/site-packages/matplotlib/tight_layout.py", line 115, in auto_adjust_subplotpars
    if ax.get_visible()])
  File "/Users/ewels/miniconda2/envs/work/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 4182, in get_tightbbox
    bb.append(child._legend_box.get_window_extent(renderer))
  File "/Users/ewels/miniconda2/envs/work/lib/python2.7/site-packages/matplotlib/offsetbox.py", line 266, in get_window_extent
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
  File "/Users/ewels/miniconda2/envs/work/lib/python2.7/site-packages/matplotlib/offsetbox.py", line 388, in get_extent_offsets
    for c in self.get_visible_children()]
  File "/Users/ewels/miniconda2/envs/work/lib/python2.7/site-packages/matplotlib/offsetbox.py", line 259, in get_extent
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
  File "/Users/ewels/miniconda2/envs/work/lib/python2.7/site-packages/matplotlib/offsetbox.py", line 478, in get_extent_offsets
    sep, self.mode)
  File "/Users/ewels/miniconda2/envs/work/lib/python2.7/site-packages/matplotlib/offsetbox.py", line 81, in _get_packed_offsets
    sep = (total - sum(w_list)) / (len(w_list) - 1.)
TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

Note that my full axes.label() call is as follows:

axes.legend(loc='lower center', bbox_to_anchor=(0, -0.22, 1, .102), ncol=5, mode='expand', fontsize=8, frameon=False)

Matplotlib version

  • Operating system: OSX, Linux
  • Matplotlib version: v2.2.0
  • Matplotlib backend (print(matplotlib.get_backend())): MacOSX
  • Python version: 2.7, 3.4, 3.5, 3.6
  • Other libraries: MultiQC: https://github.com/ewels/MultiQC

matplotlib installed using pip, python installed using conda locally (OSX) and on Travis CI.

Pinging @tacaswell, @dstansby and @afvincent as you fixed the previous bug 😉

@afvincent
Copy link
Contributor

@ewels Just by curiosity: you are passing ncol=5 because in practice you have actually more than just 2 data sets (d1 and d2) to plot, aren't you?

@afvincent
Copy link
Contributor

Looking at @tacaswell's fix (b177c85), it seems that the case

if total is None:
    total = 1.0

if taken care of just a bit to late (total can be called in a subtraction a few lines before). I'll check if that fixes the issue and open an PR if it does.

@ewels
Copy link
Author

ewels commented Mar 15, 2018

Hmm, sorry @afvincent - looks like my reply to your first comment never made it yesterday. The minimal example is a stripped down version of the code I'm using in my tool MultiQC, here. These plots can have large numbers of samples, so yes there can often be more than just 2 datasets.

Thanks for looking into this!

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 a pull request may close this issue.

3 participants