0

The title might seems simple, but I don't know what is the best way to achieve my goal.

So I got data stored in list:

  • T, a common time series
  • Data1
  • Data2
  • And so on...

Which gives me this plot with X and Y shared.

My plot

As you can see, I displayed 3 periods of my signal (60 Hz). There is 4 pulse phases, and a resting phase.

  • Pulse1: t = [0:2]
  • Pulse2: t = [2:4]
  • Pulse3: t = [4:6]
  • Pulse4: t = [6:8]
  • Rest: t = [8:16.67]

I would like for each phase to have a different color. Moreover, if during one phase, a channel don't have a pulse, he shall take the color of the rest state. For instance, channel 2 (from the top !) on the plot above don't have a pulse until the 4th phase. So between t = [0:6] the curve should have the same color as between t = [8:16.67].

Additionally I'm looking for a solution to fix the yticks to avoid the overlapping between +5 and -5.

Thanks for the help!

EDIT:

Sample data + code:

Time = [3000.0, 3000.0, 3000.3, 3000.3, 3000.35, 3000.35, 3001.55, 3001.55, 3002.0, 3002.0, 3002.3, 3002.3, 3002.35, 3002.35, 3003.55, 3003.55, 3004.0, 3004.0, 3004.3, 3004.3, 3004.35, 3004.35, 3005.55, 3005.55, 3006.0, 3006.0, 3006.3, 3006.3, 3006.35, 3006.35, 3007.55, 3007.55, 3008.0, 3016.67, 3016.67, 3016.97, 3016.97, 3017.02, 3017.02, 3018.22, 3018.22, 3018.67, 3018.67, 3018.97, 3018.97, 3019.02, 3019.02, 3020.22, 3020.22, 3020.67, 3020.67, 3020.97, 3020.97, 3021.02, 3021.02, 3022.22, 3022.22, 3022.67, 3022.67, 3022.97, 3022.97, 3023.02, 3023.02, 3024.22, 3024.22, 3024.67, 3033.34, 3033.34, 3033.64, 3033.64, 3033.69, 3033.69, 3034.89, 3034.89, 3035.34, 3035.34, 3035.64, 3035.64, 3035.69, 3035.69, 3036.89, 3036.89, 3037.34, 3037.34, 3037.64, 3037.64, 3037.69, 3037.69, 3038.89, 3038.89, 3039.34, 3039.34, 3039.64, 3039.64, 3039.69, 3039.69, 3040.89, 3040.89, 3041.34]
Sig1 = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 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, 2.7, 2.7, 0.0, 0.0, -0.675, -0.675, 0.0, 0.0, 1.2, 1.2, 0.0, 0.0, -0.3, -0.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.6, 4.6, 0.0, 0.0, -1.15, -1.15, 0.0, 0.0, 2.7, 2.7, 0.0, 0.0, -0.675, -0.675, 0.0, 0.0, 1.2, 1.2, 0.0, 0.0, -0.3, -0.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.6, 4.6, 0.0, 0.0, -1.15, -1.15, 0.0, 0.0, 2.7, 2.7, 0.0, 0.0, -0.675, -0.675, 0.0, 0.0, 1.2, 1.2, 0.0, 0.0, -0.3, -0.3, 0.0, 0.0]

f, ax = plt.subplots(7, sharex=True, sharey=True, figsize=(20,10))
ax[0].set_title("My title")
f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)

ax[0].plot(Time, Sig1, label = "signal 1")
ax[1].plot(Time, Sig2, label = "signal 2")
ax[2].plot(Time, Sig3, label = "signal 3")
ax[3].plot(Time, Sig4, label = "signal 4")
ax[4].plot(Time, Sig5, label = "signal 5")
ax[5].plot(Time, Sig6, label = "signal 6")
ax[6].plot(Time, Sig7, label = "signal 7")

f.text(0.5, 0.04, 'Time (ms)', ha='center')
f.text(0.04, 0.5, 'my Y axes label', va='center', rotation='vertical')

plt.show()

This sample will give you the same figure that I added (with one or 2 more subplots maybe). I just changed the names, thus the stupid naming :p One more thing I like to add, each subplot should have a legend with the labbel (i.e. a color line with the name signal i written nearby); and there should be also a color legend placed on the side of the graph like that:

  • color line Phase 1
  • color line...
  • color lineResting phase
Mathieu
  • 5,410
  • 6
  • 28
  • 55
  • Can you please update your question to show the code to produce the plot you show together with some sample input data? – Thomas Kühn Feb 13 '18 at 11:26
  • @ThomasKühn Please find a plotting code with data in the EDIT. Thanks for your help. – Mathieu Feb 13 '18 at 12:27
  • @ImportanceOfBeingErnest I kinda see the similarities between the 2 posts, and yet I can't manage to adapt it to my problem. I'm sure it must be obvious for you, but sadly I don't used matplotlib enough. – Mathieu Feb 13 '18 at 12:53
  • You can cut your data into segments and plot these parts separately. Then, if you generate `numpy.ndarrays` from your data, it is also simple to check whether a segment has zero signal or not and then assign a color accordingly. – Thomas Kühn Feb 13 '18 at 13:04
  • Ok well that solved one part of the problem, although my solution is far from perfect, and mostly manual. Anyway, any idea how to fix the yticks / labels and how to add a global legend on the right of the picture (thus reducing the windows size i guess ?) – Mathieu Feb 13 '18 at 13:21
  • A simple way to not have the ylabels overlap is to increase the plot range, e.g. `for a in ax: a.set_ylim([-6,6])`. For the common legend, see [this](https://stackoverflow.com/q/9834452/2454357). – Thomas Kühn Feb 13 '18 at 13:23
  • 1
    Please note that this is not your personal help desk. Each of those problems has a solution somewhere on this page already. – ImportanceOfBeingErnest Feb 13 '18 at 13:27
  • @ImportanceOfBeingErnest I understand that of course, but the goal is to progress and to learn. ie. i don't have the knowledge and skills to do that in the first place. If I don't understand the solution, well I don't improve :/ Thanks for the replies. – Mathieu Feb 13 '18 at 14:10
  • I guess it's totally fine not to understand some solution. In that case you need to ask about that solution, apply it as far as you do understand it, find out at which exact point you have problems, isolate the problem, creating [mcve]s. Then try to find answers for that isolated problem. In case you don't find any, ask about it here, but make sure to stick to [ask]. – ImportanceOfBeingErnest Feb 14 '18 at 17:17

0 Answers0