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

ValueError after moving legend and rcParams.update #6113

Closed
2 tasks
DSLituiev opened this issue Mar 5, 2016 · 2 comments
Closed
2 tasks

ValueError after moving legend and rcParams.update #6113

DSLituiev opened this issue Mar 5, 2016 · 2 comments
Assignees
Milestone

Comments

@DSLituiev
Copy link

Minimal example:

import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np

N = 100
x = np.arange(N)
plt.scatter(x,  x+ np.random.randn(N), label = "tips")
plt.legend()
ax = plt.gca()

# Locate the legend to the optimal position (automatic location does not work)
legend = ax.get_legend()
labels = (x.get_text() for x in legend.get_texts())
ax.legend(legend.legendHandles, labels, loc='lower right')

# Reset parameters
plt.matplotlib.rcParams.update(plt.matplotlib.rcParamsDefault)

# Plot again
plt.plot([0,1], [0,1])

Produces error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.4/dist-packages/IPython/core/formatters.py in __call__(self, obj)
    335                 pass
    336             else:
--> 337                 return printer(obj)
    338             # Finally look for special method names
    339             method = _safe_get_formatter_method(obj, self.print_method)

/usr/local/lib/python3.4/dist-packages/IPython/core/pylabtools.py in <lambda>(fig)
    205 
    206     if 'png' in formats:
--> 207         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    208     if 'retina' in formats or 'png2x' in formats:
    209         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

/usr/local/lib/python3.4/dist-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    115 
    116     bytes_io = BytesIO()
--> 117     fig.canvas.print_figure(bytes_io, **kw)
    118     data = bytes_io.getvalue()
    119     if fmt == 'svg':

/usr/local/lib/python3.4/dist-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2149         origedgecolor = self.figure.get_edgecolor()
   2150 
-> 2151         self.figure.dpi = dpi
   2152         self.figure.set_facecolor(facecolor)
   2153         self.figure.set_edgecolor(edgecolor)

/usr/local/lib/python3.4/dist-packages/matplotlib/figure.py in _set_dpi(self, dpi)
    409     def _set_dpi(self, dpi):
    410         self._dpi = dpi
--> 411         self.dpi_scale_trans.clear().scale(dpi, dpi)
    412         self.callbacks.process('dpi_changed', self)
    413     dpi = property(_get_dpi, _set_dpi)

/usr/local/lib/python3.4/dist-packages/matplotlib/transforms.py in scale(self, sx, sy)
   1993         scale_mtx = np.array(
   1994             [[sx, 0.0, 0.0], [0.0, sy, 0.0], [0.0, 0.0, 1.0]],
-> 1995             np.float_)
   1996         self._mtx = np.dot(scale_mtx, self._mtx)
   1997         self.invalidate()

ValueError: could not convert string to float: 'figure'

<matplotlib.figure.Figure at 0x7fc0f80c3860>
  • Matplotlib version, Python version and Platform (Windows, OSX, Linux ...)

{'dirty': False,
'error': None,
'full-revisionid': 'c0728d23ca5adbff6b79cda3153d0d36bff12c55',
'version': '1.5.1+1304.gc0728d2'}

Python 3.4
Ubuntu 14

  • How did you install Matplotlib and Python (pip, anaconda, from source ...)
    pip3 github
@tacaswell
Copy link
Member

This is something between a mpl issue and an IPython issue.

In #4286 mpl added 'figure' as a valid dpi to pass to savefig (which will be the new default in v2.0). This added a normalization step in to Figure.savefig (which does a bunch of kwarg processing and implements a some context-manager like behavior) and then calls out to print_figure

That PR added #5720 added some checks in print_figure to deal with the case where print_figure is called from the 'save' button in the GUI, but no dpi is passed in.

In this case the inline backend is grabbing the dpi from rcparams and explicitly passing it into print_figure hence the string is leaking through.

@tacaswell tacaswell added this to the 1.5.2 (Critical bug fix release) milestone Mar 6, 2016
@tacaswell tacaswell self-assigned this Mar 6, 2016
tacaswell added a commit to tacaswell/matplotlib that referenced this issue Mar 6, 2016
The string 'figure' is a valid value for dpi.  Always check if this
is the input, not just if we look it up from the rcparams.

closes matplotlib#6113
tacaswell added a commit to tacaswell/ipython that referenced this issue Mar 6, 2016
In mpl 1.5.0 'figure' became a valid dpi value, in 2.0 it will
become the default.

xref matplotlib/matplotlib#6113
@tacaswell
Copy link
Member

Thanks for this report! You know you have found a good bug when you get both a mpl and an IPython PR out of it 😉

minrk added a commit to ipython/ipython that referenced this issue Mar 6, 2016
In mpl 1.5.0 'figure' became a valid dpi value, in 2.0 it will
become the default.

xref matplotlib/matplotlib#6113

...
minrk added a commit to ipython/ipython that referenced this issue Mar 6, 2016
In mpl 1.5.0 'figure' became a valid dpi value, in 2.0 it will
become the default.

xref matplotlib/matplotlib#6113

...
minrk added a commit to ipython/ipython that referenced this issue Mar 6, 2016
In mpl 1.5.0 'figure' became a valid dpi value, in 2.0 it will
become the default.

xref matplotlib/matplotlib#6113

...
tacaswell added a commit to tacaswell/matplotlib that referenced this issue May 22, 2016
The string 'figure' is a valid value for dpi.  Always check if this
is the input, not just if we look it up from the rcparams.

closes matplotlib#6113
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

No branches or pull requests

4 participants
@tacaswell @jenshnielsen @DSLituiev and others