【问题标题】:Plot and subplot情节和子情节
【发布时间】:2014-03-18 10:24:18
【问题描述】:

我想在一张图中绘制 3 个相似的数据,所以我尝试了这段代码:

f1 = open('C:/data/EF2011S.txt', 'r') 
f2 = open('C:/data/EF2012S.txt', 'r') 
f3 = open('C:/data/EF2013S.txt', 'r') 

获取第 1 行到第 365 行的值

x1_start, x1_end = 1, 365 
y1_start, y1_end = 1, 365 # 
y11_start, y11_end = 1, 365 # 

获取第 1 行到第 366 行的值

x2_start, x2_end = 1, 366    
y2_start, y2_end = 1, 3 line
y21_start, y21_end = 1, 366  

获取第 1 行到第 313 行的值

x3_start, x3_end = 1, 313 
y3_start, y3_end = 1, 313 
y31_start, y31_end = 1, 313 


x1 = np.genfromtxt('C:/data/EF2011S.txt', usecols=(0))
y1 = np.genfromtxt('C:/data/EF2011S.txt', usecols=(5))
y11 = np.genfromtxt('C:/data/EF2011S.txt', usecols=(10))

x2 = np.genfromtxt('C:/data/EF2012S.txt', usecols=(0))
y2 = np.genfromtxt('C:/data/EF2012S.txt', usecols=(5))
y21 = np.genfromtxt('C:/data/EF2012S.txt', usecols=(10))

x3 = np.genfromtxt('C:/data/EF2013S.txt', usecols=(0))
y3 = np.genfromtxt('C:/data/EF2013S.txt', usecols=(5))
y31 = np.genfromtxt('C:/data/EF2013S.txt', usecols=(10))

x1 = x1[x1_start - 1:x1_end]
y1 = y1[y1_start - 1:y1_end]
y11 = y11[y11_start - 1:y11_end]

x2 = x2[x2_start - 1:x2_end]
y2 = y2[y2_start - 1:y2_end]
y21 = y21[y21_start - 1:y21_end]

x3 = x3[x3_start - 1:x3_end]
y3 = y3[y3_start - 1:y3_end]
y31 = y31[y31_start - 1:y31_end]

fig = plt.figure()

ax1 = fig.add_subplot(311)

ax1.set_title(" ")    
ax1.set_xlabel(' ')
ax1.set_ylabel(' ')

ax1.plot(x1,y1,y11, c='r', label='the data')

ax2 = fig.add_subplot(312)

ax2.set_title(" ")    
ax2.set_xlabel(' ')
ax2.set_ylabel(' ')

ax2.plot(x2,y2,y21, c='b', label='the data')

ax3 = fig.add_subplot(313)

ax3.set_title(" ")    
ax3.set_xlabel(' ')
ax3.set_ylabel(' ')

ax3.plot(x3,y3,y31, c='g', label='the data')


leg1 = ax1.legend()
leg2 = ax2.legend()
leg3 = ax3.legend()

plt.show()

但我得到了错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py",
line 540, in runfile
execfile(filename, namespace)
File "C:/DATA2012/Plot1.py", line 58, in <module>
ax1.plot(x1,y1,y11, c='r', label='the data')
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 4137, in plot
for line in self._get_lines(*args, **kwargs):
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 317, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 279, in _plot_args
    raise ValueError('third arg must be a format string')
    ValueError: third arg must be a format string

错误是什么意思?

【问题讨论】:

    标签: python plot subplot


    【解决方案1】:

    每个 'y-value' 参数,必须与 'x-value' 参数匹配。试试看

    ax1.plot(x1,y1,x1,y11 ...)
    

    【讨论】:

    • 我试过了,但只绘制了图形的框架。为什么无法加载数据?
    • 有什么错误吗?您是否检查过您的数据在变量中是否正确?它现在应该可以工作了。自己用一些虚拟数据进行了测试。
    • 没有错误,但只绘制了帧。数据由 11 列组成。第一列是日期,其他列是科学数据。我只想绘制第 5 列和第 10 列。
    • 那我很抱歉,但我不知道现在是什么问题。
    • 图未显示数据图。仅显示图形的框架/窗口
    猜你喜欢
    • 1970-01-01
    • 2015-03-17
    • 2019-12-05
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多