matplotlib - Python: function with list of arguments of variable length -
i'm trying plot (on same figure) data excel sheet, , want input variable length list of strings, corresponding different materials. following error: typeerror: 'nonetype' object not iterable don't understand why. here code:
import xlrd import matplotlib.pyplot plt numpy import * def transmittance(*glass): wb=xlrd.open_workbook('schott_optical_glass_catalogue_excel_december_2012.xls') sheet1=wb.sheet_by_index(0) transm_index=[] #lista vuota plt.figure(1) plt.xlabel('wavelength $\lambda$[nm]') plt.ylabel('transmittance') item in glass: in range(sheet1.nrows): if sheet1.cell_value(i,0)==glass: reversed_transmission=sheet1.row_values(i,37,67) transm_index=reversed_transmission[::-1] new_transm_index=[float(ni) ni in transm_index ] wavel_range=sheet1.row_values(3,37,67) temp_wavel= [k.split('/')[1] k in wavel_range] wrange=map(int,temp_wavel[::-1]) plt.plot(wrange,new_transm_index, 'r-') plt.grid(true, which="both") plt.show() return new_transm_index, wrange if __name__=='__main__': new_transm_index=transmittance('n-basf64','n-bk7') print 'get tuple length , glass name: ' ,new_transm_index
i not reproduce typeerror describe in question (which if called transmittance()
without parameters). however, 2 different errors instead, when calling function xls file of same name found via google.
- you iterate items in
glass
, compare whole list instead of currentitem
- when create
new_transm_index
list, can't castfloat
, there empty strings in table; in case assume value zero.
finally, if want new_transm_index
hold list each item glass
(as described in comment), should use dictionary, mapping items (keys) respective lists (values).
... new_transm_index = {} # create dictionary item in glass: in range(sheet1.nrows): if sheet1.cell_value(i, 0) == item: # compare item, not list ... # not cast ' ' float, use 0 if not of type float new_transm_index[item] = [ni if type(ni) == float else 0 ni in transm_index] ... item in glass: # add plots items common diagram plt.plot(wrange,new_transm_index[item]) plt.grid(true, which="both") plt.show() ...
Comments
Post a Comment