matplotlib - Python plot Legend Key Format -
i trying plot set of data in python. i'm using matplotlib.pyplot library this.
i got working, have 1 issue last key in legend comes out weird format others. tried different settings fixed no luck far... can me fix ch4 (a) show in same format other keys?
the format looks in plot (i cannot post picture until 10 reputations
ch1 (a)
ch2 (a)
ch3 (a)
ch4
(a
)
here code set legend.
legends = data[16] legends = legends.split(",") if numchannels == 1: plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")") elif numchannels == 2: plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")") plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")") elif numchannels == 3: plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")") plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")") plt.plot(raw[:,0],raw[:,3], label=legends[3] + " (" + units[3] + ")") elif numchannels == 4: plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")") plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")") plt.plot(raw[:,0],raw[:,3], label=legends[3] + " (" + units[3] + ")") plt.plot(raw[:,0],raw[:,4], label=legends[4] + " (" + units[4] + ")") plt.legend(loc=0) plt.xlabel(legends[0]) plt.ylabel("data") plt.title(filestr) plt.show() legends data reading in .csv file. i'm not sure why happens. happens last legend. e.g. if have 4 data set , plot 3, come out fine, if plot 4 or have 1 data set , plot, key formatted differently shown above.
i think units[4] data may contain newline characters (\n) causing strange legend format. if setup legend , units strings manually, looks correct formatting,
import numpy np import matplotlib.pyplot plt raw = np.random.random((20,5)) legends = [" ", "ch1 ","ch2 ","ch3 ","ch4 "] units = [" ", "a","a","a","a"] numchannels = 4 if numchannels == 1: plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")") elif numchannels == 2: plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")") plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")") elif numchannels == 3: plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")") plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")") plt.plot(raw[:,0],raw[:,3], label=legends[3] + " (" + units[3] + ")") elif numchannels == 4: plt.plot(raw[:,0],raw[:,1], label=legends[1] + " (" + units[1] + ")") plt.plot(raw[:,0],raw[:,2], label=legends[2] + " (" + units[2] + ")") plt.plot(raw[:,0],raw[:,3], label=legends[3] + " (" + units[3] + ")") plt.plot(raw[:,0],raw[:,4], label=legends[4] + " (" + units[4] + ")") plt.legend(loc=0) plt.xlabel(legends[0]) plt.ylabel("data") plt.show() which gives, 
Comments
Post a Comment