Skip to main content
 首页 » 编程设计

python matplotlib plotfile显式使用 float

2025年05月04日13jpfss

我有一个简单的数据文件要绘制。

这是一个数据文件的内容,我将它命名为“ttry”:

  0.27                   0 
  0.28                   0 
  0.29                   0 
   0.3                   0 
  0.31                   0 
  0.32                   0 
  0.33                   0 
  0.34                   0 
  0.35                   0 
  0.36                   0 
  0.37                   0 
  0.38    0.00728737997257 
  0.39     0.0600137174211 
   0.4       0.11488340192 
  0.41      0.157321673525 
  0.42      0.193158436214 
  0.43      0.233882030178 
  0.44      0.273319615912 
  0.45      0.311556927298 
  0.46      0.349879972565 
  0.47      0.387602880658 
  0.48      0.424211248285 
  0.49      0.460390946502 
   0.5      0.494855967078 
  0.51      0.529406721536 
  0.52      0.561814128944 
  0.53      0.594307270233 
  0.54      0.624228395062 
  0.55      0.654492455418 
  0.56      0.683984910837 
  0.57      0.711762688615 
  0.58      0.739368998628 
  0.59      0.765775034294 
   0.6      0.790895061728 
  0.61      0.815586419753 
  0.62      0.840192043896 
  0.63      0.863082990398 
  0.64      0.886231138546 
  0.65      0.906292866941 
  0.66      0.915809327846 
  0.67      0.911436899863 
  0.68      0.908179012346 
  0.69      0.904749657064 
   0.7      0.899519890261 
  0.71      0.895147462277 
  0.72      0.891632373114 
  0.73      0.888803155007 
  0.74      0.884687928669 
  0.75      0.879029492455 
  0.76      0.876114540466 
  0.77      0.872170781893 
  0.78      0.867541152263 
  0.79       0.86274005487 
   0.8      0.858367626886 
  0.81      0.854080932785 
  0.82      0.850994513032 
  0.83      0.997170781893 
  0.84       1.13477366255 
  0.85       1.24296982167 
  0.86       1.32690329218 
  0.87       1.40397805213 
  0.88       1.46836419753 
  0.89       1.52306241427 
   0.9       1.53232167353 
  0.91       1.52906378601 
  0.92       1.52211934156 
  0.93         1.516718107 
  0.94       1.51543209877 
  0.95       1.50660150892 
  0.96       1.50137174211 
  0.97       1.49408436214 
  0.98       1.48816872428 
  0.99       1.48088134431 
     1        1.4723079561 

然后我使用 matplotlib.pyplot.plotfile 来绘制它。这是我的 python 脚本

from matplotlib import pyplot 
pyplot.plotfile("ttry", cols=(0,1), delimiter=" ") 
pyplot.show() 

但是出现如下错误:

C:\WINDOWS\system32\cmd.exe /c ttry.py 
Traceback (most recent call last): 
File "E:\research\ttry.py", line 2, in <module> 
pyplot.plotfile("ttry",col=(0,1),delimiter=" ") 
File "C:\Python33\lib\site-packages\matplotlib\pyplot.py", line 2311, in plotfile 
checkrows=checkrows, delimiter=delimiter, names=names) 
File "C:\Python33\lib\site-packages\matplotlib\mlab.py", line 2163, in csv2rec 
rows.append([func(name, val) for func, name, val in zip(converters, names, row)]) 
File "C:\Python33\lib\site-packages\matplotlib\mlab.py", line 2163, in <listcomp> 
rows.append([func(name, val) for func, name, val in zip(converters, names, row)]) 
File "C:\Python33\lib\site-packages\matplotlib\mlab.py", line 2031, in newfunc 
return func(val) 
ValueError: invalid literal for int() with base 10: '0.00728737997257' 
shell returned 1 
Hit any key to close this window... 

很明显,python 只是将 yaxis 数据视为 int。那么如何告诉 python 我对 yaxis 数据使用 float?

请您参考如下方法:

它根据前几个值暗示第二列的 int 类型,这些值都是 int。要使其检查所有行,请将 checkrows = 0 添加到参数中,即:

pyplot.plotfile("ttry", cols=(0,1), delimiter=" ", checkrows = 0) 

这是一个来自 matplotlib.mlab.csv2rec 的参数,查看更多信息 here .