I would like to plot one ore more signals into one plot.
For each signal, a individual color, linewidth and linestyle may be specified. If multiple signals have to be plotted, a legend should be provided as well.
So far, I use the following code which allows me to plot up to three signals.
import matplotlib
fig = matplotlib.figure.Figure(figsize=(8,6))
subplot = fig.add_axes([0.1, 0.2, 0.8, 0.75])
Signal2, Signal3, legend, t = None, None, None, None
Signal1, = subplot.plot(xDataSignal1, yDataSignal1, color=LineColor[0], linewidth=LineWidth[0],linestyle=LineStyle[0])
if (yDataSignal2 != [] and yDataSignal3 != []):
Signal2, = subplot.plot(xDataSignal2, yDataSignal2, color=LineColor[1], linewidth=LineWidth[1],linestyle=LineStyle[1])
Signal3, = subplot.plot(xDataSignal3, yDataSignal3, color=LineColor[2], linewidth=LineWidth[2],linestyle=LineStyle[2])
legend = subplot.legend([Signal1, Signal2, Signal3], [yLabel[0], yLabel[1], yLabel[2]],LegendPosition,labelspacing=0.1, borderpad=0.1)
legend.get_frame().set_linewidth(0.5)
for t in legend.get_texts():
t.set_fontsize(10)
elif (yDataSignal2 != []):
Signal2, = subplot.plot(xDataSignal2, yDataSignal2, color=LineColor[1], linewidth=LineWidth[1],linestyle=LineStyle[1])
legend = subplot.legend([Signal1, Signal2], [yLabel[0], yLabel[1]], LegendPosition,labelspacing=0.1, borderpad=0.1)
legend.get_frame().set_linewidth(0.5)
for t in legend.get_texts():
t.set_fontsize(10)
Is it possible to generalize that code such that it is more Pythonic and supports up to n signals by still making use of matplotlib and subplot?
Any suggestions are highly appreciated.