1

I'm trying to plot two files of data of this type:

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

The third column has values from 0 to 5. I want to plot column 2 vs column 4, but, for lines with values in col 3 less than 2 (0 and 1), I want to shift col 2 by -0.1, and for lines with values greater than 3 (4 and 5) I want to shift col 2 by +0.1.

However my code seems to be shifting all values by +0.1. Here is what I have so far:

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()

Here is the plot:

plot

I'm pretty sure the problem is in my "ifs". I hope you can clear the way and/or show me a better option for this.

EternalGenin
  • 495
  • 2
  • 6
  • 14

1 Answers1

0

When you do your queries (if) you must ensure the conversion happens before the question so:

   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]))

, should actually be:

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]))

The same for your q variables. Also notice that asking 1 < x < 4 will intercept with x > 3 and x < 2. You should also correct this.

armatita
  • 12,825
  • 8
  • 48
  • 49