简体   繁体   中英

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?

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()

在此输入图像描述

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()

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM