Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make the linewidth of a single line change as a function of x in matplotlib?

Does anyone know how to make the linewidth of a single line change as a function of x in matplotlib? For example, how would you make a line thin for small values of x, and thick for large values of x?

like image 357
cmk24 Avatar asked Nov 17 '11 18:11

cmk24


People also ask

Which attribute of plot () function is used to set the width of line in line plot?

The linewidth and linestyle property can be used to change the width and the style of the line chart. Linewidth is specified in pixels.

Is it possible to change the line width of line chart?

Click On chart, then you will see a Values filled and category groups Box on right side of graph, then in Values filled, right-click on any values Go to "Series Properties" --> "Border " -- > "Line Width" and then set the size.


Video Answer


1 Answers

Basically, you need to use a polygon instead of a line. As a quick example:

import numpy as np
import matplotlib.pyplot as plt

# Make the original line...
x = np.linspace(0, 10, 100)
y = 2 * x
thickness = 0.5 * np.abs(np.sin(x) * np.cos(x))

plt.fill_between(x, y - thickness, y + thickness, color='blue')
plt.show()

enter image description here

Or if you want something closer to your description:

import numpy as np
import matplotlib.pyplot as plt

# Make the original line...
x = np.linspace(0, 10, 100)
y = np.cos(x)
thickness = 0.01 * x

plt.fill_between(x, y - thickness, y + thickness, color='blue')
plt.show()

enter image description here

like image 62
Joe Kington Avatar answered Oct 14 '22 03:10

Joe Kington