Home > Net >  Plot curve with varying opacity with matplotlib
Plot curve with varying opacity with matplotlib

Time:10-21

I have a plot with several curves that looks like this: enter image description here

These curves start from the top right corner and finish around the point (0.86, 0.5).

I want to focus attention on the end point. If I zoom on this region, it is still not very easy to distinguish the different lines because they overlap several times.

My idea is then to add a gradient of opacity so that the curves would be transparent at their start point and then, the opacity would increasing as we get closer to the end point.

How would you do that with matplotlib? Currently, I just basically do for the three curves:

plt.plot( r, l )

with r, l being two arrays.

CodePudding user response:

You could always break down your x and y arrays into smaller arrays that you plot separately. This would give you the opportunity to modify alpha for each segment.

See example below:

import numpy as np
import matplotlib.pyplot as plt

N_samp=1000
x=np.arange(N_samp)
y=np.sin(2*np.pi*x/N_samp)
step=10
[plt.plot(x[step*i:step*(i 1)],y[step*i:step*(i 1)],alpha=np.min([0.1 0.01*i,1]),color='tab:blue',lw=1) for i in range(int(N_samp/step))]
plt.show()

enter image description here

  • Related