我正在尝试从文件中读取一些数据,然后使用 matplotlib 将第一行与其他一些数据绘制在图表上。
我的代码是
import numpy as np
import matplotlib.pyplot as plt
with open("file path",'r') as f:
s= f.readlines()
y=(s[0])
x=np.arange(0.00,9.04,0.04)
plt.plot(x,y)
plt.ylabel('Probability Distribution')
plt.xlabel('Photometric Redshift')
plt.title('r2')
plt.show()
我收到以下错误消息
Traceback (most recent call last):
File "dsl_2_python.py", line 36, in <module>
plt.plot(x,y)
File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 2817, in plot
ret = ax.plot(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 3996, in plot
for line in self._get_lines(*args, **kwargs):
File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 330, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 289, in _plot_args
linestyle, marker, color = _process_plot_format(tup[-1])
File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 126, in _process_plot_format
'Unrecognized character %c in format string' % c)
ValueError: Unrecognized character 0 in format string
>
我认为这与y
作为一个字符串而不是一个列表有关,但s
似乎是一个列表,所以我不知道为什么y
会变成一个字符串或如何使它成为一个列表。任何人都可以帮忙吗?