Matplotlib:'savefig' 在设置 'linewidths' 属性 时抛出 TypeError
Matplotlib: 'savefig' throw TypeError when 'linewidths' property is set
当设置 'linewidths' 属性 时,调用 'savefig' 会抛出 'TypeError: cannot perform reduce with flexible type'。这是一个 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')
这不是图形渲染的问题(调用'plt.show()' 可以正常工作)。如果未设置线宽 属性,例如将倒数第二行更改为
plt.contour(X, Y, Z, colors = 'black')
'savefig' 按预期工作。这是一个错误还是我错过了什么?
这不是错误,因为 plt.contour()
的 documentation 指定 linewidths
应该是 [ None
|编号 |数字元组 ] 而你提供一个数字作为 string
。
这是我的代码输出(我使用的是 matplotlib 1.4.3
)。
>>> matplotlib.__version__
'1.4.3'
你的代码 'works' 在 Python 2.7 下,但是 linewidths
参数被有效地忽略了,生成的图看起来像这样,不管值如何(这是 linewidths='10'
.
对比 Python 3.4 我得到以下错误:
TypeError: unorderable types: str() > int()
将 linewidths
设置为 int
(或 float
),如下所示会产生正确的输出并适用于 Python 2.7 和 Python 3.4 .同样,它设置为 10
:
plt.contour(X, Y, Z, colors = 'black', linewidths = 10)
当设置 'linewidths' 属性 时,调用 'savefig' 会抛出 'TypeError: cannot perform reduce with flexible type'。这是一个 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')
这不是图形渲染的问题(调用'plt.show()' 可以正常工作)。如果未设置线宽 属性,例如将倒数第二行更改为
plt.contour(X, Y, Z, colors = 'black')
'savefig' 按预期工作。这是一个错误还是我错过了什么?
这不是错误,因为 plt.contour()
的 documentation 指定 linewidths
应该是 [ None
|编号 |数字元组 ] 而你提供一个数字作为 string
。
这是我的代码输出(我使用的是 matplotlib 1.4.3
)。
>>> matplotlib.__version__
'1.4.3'
你的代码 'works' 在 Python 2.7 下,但是 linewidths
参数被有效地忽略了,生成的图看起来像这样,不管值如何(这是 linewidths='10'
.
对比 Python 3.4 我得到以下错误:
TypeError: unorderable types: str() > int()
将 linewidths
设置为 int
(或 float
),如下所示会产生正确的输出并适用于 Python 2.7 和 Python 3.4 .同样,它设置为 10
:
plt.contour(X, Y, Z, colors = 'black', linewidths = 10)