Using data from an excel sheet to graph in python -
so have lot of data in excel spreadsheets. need graph through python. know how read data excel file using xlrd , know how graph in python using matplotlib. data looks has columns of x coordinates, y coordinates, , positive , negative y errors. need way for data imported spreadsheet , become points , error bars on graph. honest i'm new @ python , have no idea why code isn't working.
import xlrd import numpy np import matplotlib.pyplot plt file_location = "c:/users/rima/desktop/apjl731data.xlsx" workbook = xlrd.open_workbook(file_location) first_sheet = workbook.sheet_by_index(0) col in range(first_sheet.ncols): x = first_sheet.cell_value(0,col) y = first_sheet.cell_value(1,col) yerr = first_sheet.cell_value(2,col) plt.errorbar(x,y,yerr,fmt='r^') plt.show()
i haven't found how online, how make graphs in excel using python. i'm sure code missing lot make if work i'm not sure what. yerr, in order different error value on top , bottom of data point i've been passing array yerr = np.array([]) different values errors each point. have no idea how import data since positive errors , negative errors in different columns on spreadsheet. if know how import data please since make life easier since wouldn't have hand type 50 data point. thank you!
edit: example of data be
log(o/h)+12 positive error negative error virgo infall distance 8.56 0.05 0.05 4.61 8.59 0.03 0.03 - 8.54 0.04 0.06 2.97297 8.94 0.13 0.12 8.24493
i have gaps in data mark -, don't know if cause error when trying plot. need way skip lines. again.
edit 2: still having error here traceback.
thanks!
i have made few assumptions. assuming data this:
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
i have modified how load data load each column own array, eg.:
x = [first_sheet.cell_value(i, 0) in range(first_sheet.ncols)]
you can have positive+negative errors 1 value using errorbar passing array of form:
yerr = [y_error_negative, y_error_positive]
where y_error_negative
, y_error_positive
, arrays have same length y
.
you should have following:
import xlrd import numpy np import matplotlib.pyplot 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) in range(first_sheet.ncols)] y = [first_sheet.cell_value(i, 1) in range(first_sheet.ncols)] yerr_pos = [first_sheet.cell_value(i, 2) in range(first_sheet.ncols)] yerr_neg = [first_sheet.cell_value(i, 3) 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()
which gives this:
it's bit more difficult answer without more details.
edit:
if have '-' in data, there plenty of ways ignore it. so, quick hack way outlined above, re-check x-values:
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
you remove '-' , replace 0, example,
x = [float(i) if != '-' else 0 in x]
another way loop on values when loading them, , value if value.isdigit() else 0
, without having 2 list comprehensions.
or, can ignore said:
x = [float(i) in x if i!= '-']
it nicer not waste metallicity data if have generic upper limit on virgo infall distance. if keep getting typeerrors, give more information.
Comments
Post a Comment