I have temperature data from multiple devices on different boards, for example, on board 1 I have temperature for the PCB itself, and 3 different FETs, and similarly for boards 2 and 3.
I read the data into a dataframe, and want to plot the data together with the same color for each board tested, but with different markers for each of the devices on the boards. For example, all the measurements from board 1 would be blue, with the PCB temp using marker '+', FET1 using marker 'v' etc.
I read the files in like this:
for file_name in glob.glob(path+'*.csv'):
filename[i] = os.path.basename(file_name)
print(filename[i])
#x[i]= np.genfromtxt(path+ filename[i], delimiter=',',skip_header=20,usecols=(2,4,6,8))
x[i]=pd.read_csv(path+filename[i], header=0,usecols=[2,4,6,8], skiprows=12,names=['PCB', 'FET1', 'FET2', 'FET3'])
and create an array of dataframes.
I then plot the different columns :
colors=['r','b','g','c','m']
for i in range(len(filename)):
#plt.figure()
plt.plot(sc.decimate(x[i]['PCB'],5),'-+'+colors[i],label="PCB")
plt.plot(sc.decimate(x[i]['FET1'],5),'-v'+colors[i],label='FET1')
plt.plot(sc.decimate(x[i]['FET2'],5),'-x'+colors[i],label='FET2')
plt.plot(sc.decimate(x[i]['FET3'],5),'-o'+colors[i],label='FET3')
leg=np.append(leg, filename[i][0:7])
#plt.show()
plt.show()
plt.legend(leg)
The markers are correctly displayed, but as I iterated through the dataframes, the color information was lost. How can I plot the data and arrange it so that legend uses the same color per group of lines (as grouped by index i)?
Here's some sample data: file 1:
Name:,Data Instr INSTR 3/5/2020 11:51:59,,,,,,,,,,,,
Owner:,lab1,,,,,,,,,,,,
Comments:,,,,,,,,,,,,,
Acquisition Date:,3/5/2020 11:51,,,,,,,,,,,,
&Instrument:,34970A,Address:,ASRL11::INSTR,Modules:,1,Slot3:,34901A,,,,,,
Total Channels:,4,,,,,,,,,,,,
Channel,Name,Function,Range,Resolution,AdvSettings,Scale,Gain,Offset,Label,Test,Low,High,HWAlarm
316,PCB_CTR,Temp (Type K),None,C,Temp (Type K)#1#0.016#Auto#0.001#C#Internal#0#false,FALSE,1,0,C,High Only,0,105,Alarm 1
317,Q24,Temp (Type K),None,C,Temp (Type K)#1#0.016#Auto#0.001#C#Internal#0#false,FALSE,1,0,C,High Only,0,105,Alarm 1
318,Q25,Temp (Type K),None,C,Temp (Type K)#1#0.016#Auto#0.001#C#Internal#0#false,FALSE,1,0,C,High Only,0,105,Alarm 1
319,Q18,Temp (Type K),None,C,Temp (Type K)#1#0.016#Auto#0.001#C#Internal#0#false,FALSE,1,0,C,High Only,0,105,Alarm 1
Scan Control:,Start Action:,Immediately,Stop Action:,User Terminated,,,,,,,,,
Scan,Time,316 <PCB_CTR> (C),Alarm 316,317 <Q24> (C),Alarm 317,318 <Q25> (C),Alarm 318,319 <Q18> (C),Alarm 319,,,,
1,3/5/2020 11:51:59:168,30.471,0,29.241,0,29.165,0,33.302,0,,,,
2,3/5/2020 11:52:01:152,32.197,0,30.634,0,30.564,0,34.819,0,,,,
3,3/5/2020 11:52:03:152,33.795,0,32.019,0,31.879,0,36.848,0,,,,
4,3/5/2020 11:52:05:152,35.315,0,33.383,0,33.236,0,38.282,0,,,,
5,3/5/2020 11:52:07:152,36.965,0,34.734,0,34.62,0,39.946,0,,,,
6,3/5/2020 11:52:09:152,38.255,0,36.054,0,35.776,0,41.18,0,,,,
7,3/5/2020 11:52:11:152,39.467,0,37.328,0,37.028,0,42.258,0,,,,
file 2
Name:,Data Instr INSTR 3/5/2020 10:03:21,,,,,,,,,,,,
Owner:,lab1,,,,,,,,,,,,
Comments:,,,,,,,,,,,,,
Acquisition Date:,3/5/2020 10:03,,,,,,,,,,,,
&Instrument:,34970A,Address:,ASRL11::INSTR,Modules:,1,Slot3:,34901A,,,,,,
Total Channels:,4,,,,,,,,,,,,
Channel,Name,Function,Range,Resolution,AdvSettings,Scale,Gain,Offset,Label,Test,Low,High,HWAlarm
316,PCB_CTR,Temp (Type K),None,C,Temp (Type K)#1#0.016#Auto#0.001#C#Internal#0#false,FALSE,1,0,C,High Only,0,105,Alarm 1
317,Q24,Temp (Type K),None,C,Temp (Type K)#1#0.016#Auto#0.001#C#Internal#0#false,FALSE,1,0,C,High Only,0,105,Alarm 1
318,Q25,Temp (Type K),None,C,Temp (Type K)#1#0.016#Auto#0.001#C#Internal#0#false,FALSE,1,0,C,High Only,0,105,Alarm 1
319,Q18,Temp (Type K),None,C,Temp (Type K)#1#0.016#Auto#0.001#C#Internal#0#false,FALSE,1,0,C,High Only,0,105,Alarm 1
Scan Control:,Start Action:,Immediately,Stop Action:,User Terminated,,,,,,,,,
Scan,Time,316 <PCB_CTR> (C),Alarm 316,317 <Q24> (C),Alarm 317,318 <Q25> (C),Alarm 318,319 <Q18> (C),Alarm 319,,,,
1,3/5/2020 10:03:21:164,46.334,0,43.755,0,45.706,0,49.129,0,,,,
2,3/5/2020 10:03:22:149,46.997,0,44.262,0,46.35,0,49.773,0,,,,
3,3/5/2020 10:03:23:149,47.615,0,44.671,0,46.974,0,50.402,0,,,,
4,3/5/2020 10:03:24:149,48.267,0,45.229,0,47.628,0,50.879,0,,,,
5,3/5/2020 10:03:25:149,48.861,0,45.711,0,48.164,0,51.495,0,,,,
6,3/5/2020 10:03:26:149,49.455,0,46.323,0,48.783,0,51.9,0,,,,
7,3/5/2020 10:03:27:149,50.014,0,46.796,0,49.351,0,52.334,0,,,,
file 3
Name:,Data Instr INSTR 3/5/2020 13:41:06,,,,,,,,,,,,
Owner:,lab1,,,,,,,,,,,,
Comments:,,,,,,,,,,,,,
Acquisition Date:,3/5/2020 13:41,,,,,,,,,,,,
&Instrument:,34970A,Address:,ASRL11::INSTR,Modules:,1,Slot3:,34901A,,,,,,
Total Channels:,4,,,,,,,,,,,,
Channel,Name,Function,Range,Resolution,AdvSettings,Scale,Gain,Offset,Label,Test,Low,High,HWAlarm
316,PCB_CTR,Temp (Type K),None,C,Temp (Type K)#1#0.016#Auto#0.001#C#Internal#0#false,FALSE,1,0,C,High Only,0,105,Alarm 1
317,Q24,Temp (Type K),None,C,Temp (Type K)#1#0.016#Auto#0.001#C#Internal#0#false,FALSE,1,0,C,High Only,0,105,Alarm 1
318,Q25,Temp (Type K),None,C,Temp (Type K)#1#0.016#Auto#0.001#C#Internal#0#false,FALSE,1,0,C,High Only,0,105,Alarm 1
319,Q18,Temp (Type K),None,C,Temp (Type K)#1#0.016#Auto#0.001#C#Internal#0#false,FALSE,1,0,C,High Only,0,105,Alarm 1
Scan Control:,Start Action:,Immediately,Stop Action:,User Terminated,,,,,,,,,
Scan,Time,316 <PCB_CTR> (C),Alarm 316,317 <Q24> (C),Alarm 317,318 <Q25> (C),Alarm 318,319 <Q18> (C),Alarm 319,,,,
1,3/5/2020 13:41:06:162,28.121,0,26.882,0,28.785,0,31.061,0,,,,
2,3/5/2020 13:41:08:147,30.582,0,27.873,0,30.691,0,33.024,0,,,,
3,3/5/2020 13:41:10:147,31.782,0,28.935,0,32.578,0,34.876,0,,,,
4,3/5/2020 13:41:12:147,34.003,0,30.094,0,34.247,0,36.652,0,,,,
5,3/5/2020 13:41:14:147,35.097,0,31.199,0,35.975,0,38.142,0,,,,
6,3/5/2020 13:41:16:147,36.708,0,32.334,0,37.504,0,39.721,0,,,,
7,3/5/2020 13:41:18:147,38.274,0,33.508,0,39.048,0,41.198,0,,,,
Thank you for the help.
EDIT
With the help from @ilke444's input, I got closer to what I want, but I still have issues:
for i in range(len(filename)):
l=plt.plot(sc.decimate(x[i]['PCB'],5),'-+'+colors[i],label="PCB")
lines=np.append(lines,l[0].get_label())
l=plt.plot(sc.decimate(x[i]['FET1'],5),'-v'+colors[i],label='FET1')
lines=np.append(lines,l[0].get_label())
l=plt.plot(sc.decimate(x[i]['FET2'],5),'-x'+colors[i],label='FET2')
lines=np.append(lines,l[0].get_label())
l=plt.plot(sc.decimate(x[i]['FET3'],5),'-o'+colors[i],label='FET3')
lines=np.append(lines,l[0].get_label())
linesclr=np.append(linesclr, l) # save color info
names = np.append(names, filename[i][0:7])
fig.legend(lines, loc=1)
fig.legend(linesclr, labels=names, loc=2)
plt.show()
As can be seen below, the second legend I'm trying to add does not show the right colors, ie, 1 color per file read (top left corner):
I don't understand why the legend on the left doesn't show the right colors, since the color information is in each of the elements in the lineslr array:
linesclr[0].get_color()
Out[4]: 'r'
linesclr[0].get_color()
Out[5]: 'r'
linesclr[1].get_color()
Out[6]: 'b'
linesclr[2].get_color()
Out[7]: 'g'
Furthermore, I don't understand why the marker is not always the circle ('o') for all the keys in that legend.
Solution I'm looking for:
It occurs to me that the best way to convey the info on the plots is with the 2 legends, but with the legend on the right just showing the markers for each line for each device type (black line with corresponding marker), and the legend on the left showing the filenames and the colors (line with no markers) used for all the temperatures readings from that file.
So, I'd like to make the legend on the right just show the 4 thermocouple locations with their markers:
+ PCB
v FET1
x FET2
o FET3
and the legend on the left to show: ACI50#5 in red, ACI50# in blue, ACI50#6 in green, an KDE5515 in cyan (or however many files I read in, each with its corresponding plot color).
I've tried reading and literature on matplotlib on legends and composing custom legends, and have looked for examples on the internet, but I'm not having much success understanding what I'm reading!