我做了一些假设。假设你的数据是这样的:x y yerr_positive yerr_negative
1 1 0.1 0.2
2 2 0.1 0.2
3 3 0.1 0.2
4 4 0.1 0.2
我还修改了如何略微加载数据,以便将每列加载到其自己的数组中,例如:x = [first_sheet.cell_value(i, 0) for i in range(first_sheet.ncols)]
通过传递以下形式的数组,可以使用errorbar对一个值产生正负错误:yerr = [y_error_negative, y_error_positive]
其中,y_error_negative和y_error_positive是与y具有相同长度的数组。
然后,您应该拥有以下内容:import xlrd
import numpy as np
import matplotlib.pyplot as plt
file_location = "C:/Users/Rima/Desktop/apjl731data.xlsx"
workbook = xlrd.open_workbook(file_location)
first_sheet = workbook.sheet_by_index(0)
x = [first_sheet.cell_value(i, 0) for i in range(first_sheet.ncols)]
y = [first_sheet.cell_value(i, 1) for i in range(first_sheet.ncols)]
yerr_pos = [first_sheet.cell_value(i, 2) for i in range(first_sheet.ncols)]
yerr_neg = [first_sheet.cell_value(i, 3) for i in range(first_sheet.ncols)]
yerr = [yerr_neg, yerr_pos]
plt.errorbar(x,y,yerr,fmt='r^')
plt.axis([0,5,0,5])
plt.show()
因此:
如果没有更多的细节,回答起来会有点困难。
编辑:
如果数据中有“-”,有很多方法可以忽略它。所以,按照我上面概述的方法,您可以重新检查x值:x y yerr_positive yerr_negative
1 1 0.1 0.2
- 2 0.1 0.2
3 3 0.1 0.2
4 4 0.1 0.2
然后删除“-”并替换为0,例如x = [float(i) if i != '-' else 0 for i in x]
另一种方法是在加载值时循环这些值,并在没有两个列表理解的情况下执行value if value.isdigit() else 0。
或者,你可以完全忽略它,就像你说的:x = [float(i) for i in x if i!= '-']
最好不要浪费你的金属含量数据,如果你能有一些一般的处女座入口距离上限。如果你一直有打字错误,请提供更多信息。