用Pyplot绘制特定值的特定行(Plot specific lines for specific values with Pyplot)

编程入门 行业动态 更新时间:2024-10-17 23:23:26
用Pyplot绘制特定值的特定行(Plot specific lines for specific values with Pyplot)

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

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

第三列的值为0到5.我想绘制列2与列4,但对于列3中值小于2(0和1)的行,我想将列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()

这是情节:

情节

我很确定问题出在我的“ifs”上。 我希望你能清楚的方式和/或给我一个更好的选择。

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.

最满意答案

当你做你的查询( 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 > 3和x < 2 。 你也应该改正这一点。

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.

更多推荐

本文发布于:2023-08-06 19:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455751.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定值   Plot   Pyplot   values   lines

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!