在python 2.6.5上运行以下简单示例代码时遇到问题。 我已经看过其他无效文字问题的解决方案,在我看来,这通常发生在python假设它绘制的数据是整数并试图迭代数据时。但是,在创建图形对象之前,似乎已达到此错误。我可能错了,并希望有人指出我正确的方向。
import matplotlib.pyplot as plt
import numpy as np
x=np.array([1,2,3,4,5,6,7])
y=np.sin(x)
fig = plt.figure('test figure')
sub1=fig.add_subplot(1,1,1)
sub1.plot(x,y)
生成以下错误
Traceback (most recent call last):
File "C:\Users\u999999\Desktop\code\simple_example.py", line 7, in <module>
fig = plt.figure('test figure')
File "C:\Python\lib\site-packages\matplotlib\pyplot.py", line 241, in figure
num = int(num) # crude validation of num argument
ValueError: invalid literal for int() with base 10: 'test figure'
从评论中添加
我正在使用matplotlib版本0.99.3rc1
答案 0 :(得分:1)
您尝试访问的功能是传递a text label as num
。最初你只能在这里放一个图号,现在你可以输入一个数字或字符串标签。
此功能自2011年6月this commit起可用。此版本的第一个版本是version 1.1.0。你说你的版本是0.99.3rc2 - 所以这个功能不可用。我最好的建议是从那个(相当老的!)版本升级,除非有非常强烈的理由不这样做。
使您能够传递字符串代替整数图号的代码行是here,下面复制的短代码块用于说明点 - matplotlib.pyplot.figure()
的第一个参数被命名num
,默认为None
:
#snippet from matplotlib.pyplot.figure()
if num is None:
num = next_num
elif is_string_like(num):
figLabel = num
allLabels = get_figlabels()
if figLabel not in allLabels:
if figLabel == 'all':
warnings.warn("close('all') closes all existing figures")
num = next_num
else:
inum = allLabels.index(figLabel)
num = allnums[inum]