how to color area under a curve matplot lib python in python

To color the area under a curve in Matplotlib, the fill_between function can be used. Here is an example code snippet that demonstrates how to use the fill_between function:

main.py
import matplotlib.pyplot as plt
import numpy as np

# Generate some data for the curve
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Create the plot with the curve
fig, ax = plt.subplots()
ax.plot(x, y)

# Fill the area under the curve with a color
ax.fill_between(x, y, color='blue', alpha=0.5)

# Show the plot
plt.show()
323 chars
17 lines

In the fill_between function, the x and y arrays represent the points that define the curve. The color parameter sets the color of the fill, and the alpha parameter sets the transparency of the fill.

related categories

gistlibby LogSnag