0

I am trying to make something along the lines of this answer with matplotlib. This works fine, but I have not enough data and want too high variation in line width for it to look good. What I get is like the following, where there are not many values to plot, and there are white spaces where the plot curves a lot.

example plot

For this example I simply took the code out of gg349's answer and replaced x = np.linspace(0,4*np.pi,10000) with x = np.linspace(0,4*np.pi,100)

I am looking for a way to make this look like one line which changes in size, and not many small ones which are back to back with each other (which they technically are, but I'd like to hide that)

I am struggling to properly ask my question since English is not my first language, please excuse any mistakes.

1 Answers1

1

You can use a spline interpolation to create more segments along the curve.

enter image description here

import numpy as np
from matplotlib.collections import LineCollection
import matplotlib.pyplot as plt
from scipy.interpolate import splev, splrep

x = np.linspace(0, 4*np.pi, 40)
y = np.cos(x)

spl = splrep(x, y)
x2 = np.linspace(0, 4*np.pi, 3000)
y2 = splev(x2, spl)

fig,a = plt.subplots(1, 2, figsize=(20,8))
for i, xy in enumerate([(x, y), (x2, y2)]):
    x3, y3 = xy
    lwidths=1+x3[:-1]
    points = np.array([x3, y3]).T.reshape(-1, 1, 2)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)
    lc = LineCollection(segments, linewidths=lwidths,color='blue')
    a[i].add_collection(lc)
    a[i].set_xlim(0,4*np.pi)
    a[i].set_ylim(-1.1,1.1)
tom10
  • 67,082
  • 10
  • 127
  • 137