Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot several curves with an offset on the same graph

I read a waveform from an oscilloscope. The waveform is divided into 10 segments as a function of time. I want to plot the complete waveform, one segment above (or under) another, 'with a vertical offset', so to speak. Additionally, a color map is necessary to show the signal intensity. I've only been able to get the following plot:

enter image description here

As you can see, all the curves are superimposed, which is unacceptable. One could add an offset to the y data but this is not how I would like to do it. Surely there is a much neater way of plotting my data? I've tried a few things to solve this issue using pylab but I am not even sure how to proceed and if this is the right way to go.

Any help will be appreciated.

import readTrc #helps read binary data from an oscilloscope
import matplotlib.pyplot as plt

fName = r"...trc"
datX, datY, m = readTrc.readTrc(fName)

segments = m['SUBARRAY_COUNT'] #number of segments

x, y = [], []

for i in range(segments+1):
    x.append(datX[segments*i:segments*(i+1)])
    y.append(datY[segments*i:segments*(i+1)])

plt.plot(x,y)
plt.show()
like image 827
DenGor Avatar asked Jan 28 '26 04:01

DenGor


1 Answers

A plot with a vertical offset sounds like a frequency trail.
kde_joyplot example from seaborn

Here's one approach that does just adjust the y value.

Frequency Trail in MatPlotLib

The same plot has also been coined a joyplot/ridgeline plot. Seaborn has an implementation that creates a series of plots (FacetGrid), and then adjusts the offset between them for a similar effect.

https://seaborn.pydata.org/examples/kde_joyplot.html

An example using a line plot might look like:

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

segments = 10
points_per_segment = 100

#your data preparation will vary
x = np.tile(np.arange(points_per_segment), segments)
z = np.floor(np.arange(points_per_segment * segments)/points_per_segment)
y = np.sin(x * (1 + z))
        
    
df = pd.DataFrame({'x': x, 'y': y, 'z': z})

pal = sns.color_palette()
g = sns.FacetGrid(df, row="z", hue="z", aspect=15, height=.5, palette=pal)
g.map(plt.plot, 'x', 'y')
g.map(plt.axhline, y=0, lw=2, clip_on=False)
# Set the subplots to overlap
g.fig.subplots_adjust(hspace=-.00)
g.set_titles("")
g.set(yticks=[])
g.despine(bottom=True, left=True)

plt.show()

Out: enter image description here

like image 143
Jamil R Khan Avatar answered Jan 29 '26 18:01

Jamil R Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!