我想從文件中讀取一些數據,然後取第一行,並使用matplotlib將其繪製在圖上。字符串,列表和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
成爲一個字符串或如何使它成爲一個列表。誰能幫忙?
你有你的文件'F'特殊字符在讀什麼書? –