TypeError: list indices must be integers, not STR

Joined
Jul 13, 2010
Messages
1
Reaction score
0
Hoping to get assistance.
Mostly using matplotlib.

the list FullSpreadsheet is able to be printed and works quite well.
we are trying to split FullSpreadsheet, by means of
Code:
for element in FullSpreadsheet:
    if element['mode'][1] == 'T':
        TransSpreadsheet.append(s)
    elif element['mode'][1] == 'R':
        ReflSpreadsheet.append(s)

but when attempting to plot that data, we receive the "TypeError: list indices must be integers, not str" Errors.

I will attach our FULL code, and the FULL file it is meant to iterate over.

Code:
#!/usr/bin/python2.6
import numpy as np
import os
import csv
import math
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.ticker as mticker
import matplotlib.font_manager
from matplotlib.dates import date2num,MonthLocator,DayLocator,HourLocator,DateFormatter

fig = plt.figure()
ax = fig.add_subplot(111)
loadfrom = '\\\\IRWiki\\share-IRWiki\\SigNoiseSpreadsheet.csv'
saveto = '\\\\IRWiki\\share-IRWiki\\XY Merged.png'
savetransto = '\\\\IRWiki\\share-IRWiki\\XY Transmission.png'
savereflto = '\\\\IRWiki\\share-IRWiki\\XY Reflectance.png'
FullSpreadsheet=mlab.csv2rec(loadfrom,delimiter=',')


TransSpreadsheet = []
ReflSpreadsheet = []
for element in FullSpreadsheet:
    if element['mode'][1] == 'T':
        TransSpreadsheet.append(s)
    elif element['mode'][1] == 'R':
        ReflSpreadsheet.append(s)
print TransSpreadsheet


ax.plot(TransSpreadsheet['date'], TransSpreadsheet['ycalibration'], '-o', ms=6, lw=2, alpha=0.7, mfc='orange', label='Y Calibration')
ax.plot(TransSpreadsheet['date'], TransSpreadsheet['xcalibration'], '-o', ms=6, lw=2, alpha=0.7, mfc='red', label='X Calibration')

        
ax.set_xlabel('Date (month - day)') 
ax.set_ylabel('Calibration (mm)')
ax.set_title('MidIR XY-Mirror Calibration vs Time')

#to allow for plotting 'over' empty cells, the [np.isfinite(referencevalue)] item is used.
#ax.plot(FullSpreadsheet['date'][np.isfinite(FullSpreadsheet['ycalibration'])], FullSpreadsheet['ycalibration'][np.isfinite(data['ycalibration'])], '-o', ms=6, lw=2, alpha=0.7, mfc='orange', label='Y Calibration')
#ax.plot(FullSpreadsheet['date'][np.isfinite(FullSpreadsheet['xcalibration'])], FullSpreadsheet['xcalibration'][np.isfinite(data['xcalibration'])], '-o', ms=6, lw=2, alpha=0.7, mfc='red', label='X Calibration')

 # format the ticks
days=DayLocator(interval=2)                 #major ticks will be on every second day of the month
hours=HourLocator(interval=8)               #minor ticks will be every 8 hours

ax.xaxis.set_major_locator(days)            # major x-axis tick every 2 days
ax.xaxis.set_major_formatter(DateFormatter('%m-%d')) #x-axis major tick labels
ax.xaxis.set_minor_locator(hours)           #minor x-axis ticks every 8 hours
ax.fmt_xdata = DateFormatter('%m-%d')
fig.autofmt_xdate(rotation=-90, ha='left')  #rotate and right align the xaxis labels
ax.yaxis.set_major_locator(mticker.MultipleLocator(base=1.0))
ax.yaxis.set_minor_locator(mticker.MultipleLocator(base=0.25))
plt.legend(loc='best', prop=matplotlib.font_manager.FontProperties(size=8))
                                            #prop sets the font size of the legend
ax.grid()                                   #turns on the graph grid
fig.savefig(saveto)

#opens the output image in its default image viewing program.
os.startfile(saveto)

ax.plot(TransSpreadsheet['date'][np.isfinite(TransSpreadsheet['ycalibration'])], TransSpreadsheet['ycalibration'][np.isfinite(TransSpreadsheet['ycalibration'])], '-o', ms=6, lw=2, alpha=0.7, mfc='orange', label='Y Calibration')
ax.plot(TransSpreadsheet['date'][np.isfinite(TransSpreadsheet['xcalibration'])], TransSpreadsheet['xcalibration'][np.isfinite(TransSpreadsheet['xcalibration'])], '-o', ms=6, lw=2, alpha=0.7, mfc='red', label='X Calibration')

 # format the ticks
days=DayLocator(interval=2)                 #major ticks will be on every second day of the month
hours=HourLocator(interval=8)               #minor ticks will be every 8 hours

ax.xaxis.set_major_locator(days)            # major x-axis tick every 2 days
ax.xaxis.set_major_formatter(DateFormatter('%m-%d')) #x-axis major tick labels
ax.xaxis.set_minor_locator(hours)           #minor x-axis ticks every 8 hours
ax.fmt_xdata = DateFormatter('%m-%d')
fig.autofmt_xdate(rotation=-90, ha='left')  #rotate and right align the xaxis labels
ax.yaxis.set_major_locator(mticker.MultipleLocator(base=1.0))
ax.yaxis.set_minor_locator(mticker.MultipleLocator(base=0.25))
plt.legend(loc='best', prop=matplotlib.font_manager.FontProperties(size=8))
                                            #prop sets the font size of the legend
ax.grid()                                   #turns on the graph grid
fig.savefig(savetransto)

ax.plot(ReflSpreadsheet['date'][np.isfinite(ReflSpreadsheet['ycalibration'])], ReflSpreadsheet['ycalibration'][np.isfinite(ReflSpreadsheet['ycalibration'])], '-o', ms=6, lw=2, alpha=0.7, mfc='orange', label='Y Calibration')
ax.plot(ReflSpreadsheet['date'][np.isfinite(ReflSpreadsheet['xcalibration'])], ReflSpreadsheet['xcalibration'][np.isfinite(ReflSpreadsheet['xcalibration'])], '-o', ms=6, lw=2, alpha=0.7, mfc='red', label='X Calibration')

 # format the ticks
days=DayLocator(interval=2)                 #major ticks will be on every second day of the month
hours=HourLocator(interval=8)               #minor ticks will be every 8 hours

ax.xaxis.set_major_locator(days)            # major x-axis tick every 2 days
ax.xaxis.set_major_formatter(DateFormatter('%m-%d')) #x-axis major tick labels
ax.xaxis.set_minor_locator(hours)           #minor x-axis ticks every 8 hours
ax.fmt_xdata = DateFormatter('%m-%d')
fig.autofmt_xdate(rotation=-90, ha='left')  #rotate and right align the xaxis labels
ax.yaxis.set_major_locator(mticker.MultipleLocator(base=1.0))
ax.yaxis.set_minor_locator(mticker.MultipleLocator(base=0.25))
plt.legend(loc='best', prop=matplotlib.font_manager.FontProperties(size=8))
                                            #prop sets the font size of the legend
ax.grid()                                   #turns on the graph grid
fig.savefig(savereflto)




#if you get a really odd graph with non-linear lines, you probably imported the .csv file into excel
#and it most likely destroyed your date formatting....

i have no idea why i cant get the TransSpreadsheet or ReflSpreadsheet to be properly utilize-able...


note: had to change the file to a .txt due to forum limitations...
 

Attachments

  • SigNoiseSpreadsheet.csv.txt
    7.7 KB · Views: 447

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top