I would like to be able to change width of a line according to a list of values. For example if I have the following list to plot:
a = [0.0, 1.0, 2.0, 3.0, 4.0]
could I use the following list to set the linewidth?
b = [1.0, 1.5, 3.0, 2.0, 1.0]
It doesn't seem to be supported, but they say "anything is possible" so thought I would ask people with more experience (noob here).
Thanks
Basically, you have two options.
LineCollection
. In this case, your line widths will be in points, and the line width will be constant for each segment.fill_between
, but for complex curves you may need to create it directly). In this case, your line widths will be in data units and will vary linearly between each segment in your line.Here are examples of both:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
np.random.seed(1977)
x = np.arange(10)
y = np.cos(x / np.pi)
width = 20 * np.random.random(x.shape)
# Create the line collection. Widths are in _points_! A line collection
# consists of a series of segments, so we need to reformat the data slightly.
coords = zip(x, y)
lines = [(start, end) for start, end in zip(coords[:-1], coords[1:])]
lines = LineCollection(lines, linewidths=width)
fig, ax = plt.subplots()
ax.add_collection(lines)
ax.autoscale()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1977)
x = np.arange(10)
y = np.cos(x / np.pi)
width = 0.5 * np.random.random(x.shape)
fig, ax = plt.subplots()
ax.fill_between(x, y - width/2, y + width/2)
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With