使用Pyplot绘制特定值的特定行

时间:2016-05-10 17:25:03

标签: python-2.7 matplotlib readlines

我试图绘制这种类型的两个数据文件:

name1.fits 0 0 2.40359218172
name2.fits 0 0 2.15961244263

第三列的值为0到5.我想绘制第2列和第4列,但是,对于col 3中的值小于2(0和1)的行,我想将col 2移动-0.1 ,对于值大于3(4和5)的行,我想将col 2移动+0.1。

但是我的代码似乎将所有值都移动了+0.1。以下是我到目前为止的情况:

import matplotlib.pyplot as plt
import numpy as np

with open('file1.txt') as data, open('file2.txt') as stds:

    lines1 = data.readlines()
    lines2 = stds.readlines()

    x1a = []
    x2a = []
    x1b = []
    x2b = []
    x1c = []
    x2c = []
    y1a = []
    y2a = []
    y1b = []
    y2b = []
    y1c = []
    y2c = []

    for line1 in lines1:
        p = line1.split()
        if p[2] < 2:        
            x1a.append(float(p[1]))
            y1a.append(float(p[3]))
        elif 1 < p[2] < 4:        
            x1b.append(float(p[1]))
            y1b.append(float(p[3]))
        elif p[2] > 3:        
            x1c.append(float(p[1]))
            y1c.append(float(p[3]))

    for line2 in lines2:
        q = line2.split()
        if q[2] < 2:        
            x2a.append(float(q[1]))
            y2a.append(float(q[3]))
        elif 1 < q[2] < 4:        
            x2b.append(float(q[1]))
            y2b.append(float(q[3]))
        elif q[2] > 3:        
            x2c.append(float(q[1]))
            y2c.append(float(q[3]))

    x1a = np.array(x1a)
    x2a = np.array(x2a)
    x1b = np.array(x1b)
    x2b = np.array(x2b)
    x1c = np.array(x1c)
    x2c = np.array(x2c)
    y1a = np.array(y1a)
    y2a = np.array(y2a)
    y1b = np.array(y1b)
    y2b = np.array(y2b)
    y1c = np.array(y1c)
    y2c = np.array(y2c)

minorLocator = AutoMinorLocator(5)

fig, ax = plt.subplots(figsize=(8, 8))
fig.subplots_adjust(left=0.11, right=0.95, top=0.94)

plt.plot(x1a-0.1,y1a,'b^',mec='blue',label=r'B0',ms=8)
plt.plot(x2a-0.1,y2a,'r^',mec='red',fillstyle='none',mew=0.8,ms=8)
plt.plot(x1b,y1b,'bo',mec='blue',label=r'B0',ms=8)
plt.plot(x2b,y2b,'ro',mec='red',fillstyle='none',mew=0.8,ms=8)
plt.plot(x1c+0.1,y1c,'bx',mec='blue',label=r'B0',ms=8)
plt.plot(x2c+0.1,y2c,'rx',mec='red',fillstyle='none',mew=0.8,ms=8)

plt.axis([-1.0, 3.0, 0., 4])
ax.xaxis.set_tick_params(labeltop='on')
ax.yaxis.set_minor_locator(minorLocator)

plt.show()

这是情节:

plot

我很确定问题出现在我的&#34; ifs&#34;中。我希望你能清楚道路和/或给我一个更好的选择。

1 个答案:

答案 0 :(得分:0)

当您进行查询(if)时,您必须确保转换发生在问题之前:

   for line1 in lines1:
        p = line1.split()
        if p[2] < 2:        
            x1a.append(float(p[1]))
            y1a.append(float(p[3]))
        elif 1 < p[2] < 4:        
            x1b.append(float(p[1]))
            y1b.append(float(p[3]))
        elif p[2] > 3:        
            x1c.append(float(p[1]))
            y1c.append(float(p[3]))

,实际应该是:

for line1 in lines1:
    p = line1.split()
    if float(p[2]) < 2:     # changed here   
        x1a.append(float(p[1]))
        y1a.append(float(p[3]))
    elif 1 < float(p[2]) < 4:  # There seems to be a problem with this if      
        x1b.append(float(p[1]))
        y1b.append(float(p[3]))
    elif float(p[2]) > 3:     # changed here    
        x1c.append(float(p[1]))
        y1c.append(float(p[3]))

q个变量相同。另请注意,询问1 < x < 4会拦截x > 3x < 2。你也应该纠正这个。