Matplotlib: 'savefig' throw TypeError when 'linewidths' property is set


When the 'linewidths' property is set, calling 'savefig' throws 'TypeError: cannot perform reduce with flexible type'. Here is a MWE:

# Create sample data.
x = np.arange(-3.0, 3.0, 0.1)
y = np.arange(-2.0, 2.0, 0.1)
X, Y = np.meshgrid(x, y)
Z = 10.0 * (2*X - Y)
# Plot sample data.
plt.contour(X, Y, Z, colors = 'black', linewidths = '1') 
plt.savefig('test.pdf')

It is not a problem with the figure rendering (calling 'plt.show()' works fine). If the linewidths property is not set, e.g. changing the second last line to

plt.contour(X, Y, Z, colors = 'black') 

'savefig' works as intended. Is this a bug or have i missed something?


Answers:


This is not a bug, since the documentation for plt.contour() specifies that linewidths should be a [ None | number | tuple of numbers ] while you provide a number as a string.

Here is my output with your code (I am using matplotlib 1.4.3).

>>> matplotlib.__version__
'1.4.3'

Your code 'works' under Python 2.7 but the linewidths parameter is effectively ignored, producing plots that look like this, regardless of the value (this was with linewidths='10'.

linewidths as a string

In contrast on Python 3.4 I get the following error:

TypeError: unorderable types: str() > int()

Setting linewidths to an int (or a float) as follows produces the correct output and works on both Python 2.7 and Python 3.4. Again, this is with it set to 10:

plt.contour(X, Y, Z, colors = 'black', linewidths = 10) 

linewidth as an int