Fill the Area Between Curves in Matplotlib

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y, label='sin(x)')
plt.fill_between(x, y, color='lightblue')

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Basic fill_between')
plt.legend()

plt.show()
import matplotlib.pyplot as plt
import numpy as np

# Sample data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x, y1, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.fill_between(x, y1, y2, color='lightgray')

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Filling Between Two Curves')
plt.legend()

plt.show()
import matplotlib.pyplot as plt
import numpy as np

# Sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y, label='sin(x)')
plt.fill_between(x, y, where=(y > 0), color='green', alpha=0.3, label='y > 0')
plt.fill_between(x, y, where=(y < 0), color='red', alpha=0.3, label='y < 0')

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Conditional fill_between')
plt.legend()

plt.show()
import matplotlib.pyplot as plt
import numpy as np

# Sample data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x, y1, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.fill_between(x, y1, y2, where=(y1 >= y2), facecolor='blue', alpha=0.3, label='sin(x) >= cos(x)')
plt.fill_between(x, y1, y2, where=(y1 < y2), facecolor='orange', alpha=0.3, label='sin(x) < cos(x)')

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Custom Colors and Transparency')
plt.legend()

plt.show()
import matplotlib.pyplot as plt
import numpy as np

# Sample data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x, y1, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.fill_between(x, y1, y2, where=(y1 >= y2), facecolor='blue', alpha=0.3, hatch='/', label='sin(x) >= cos(x)')
plt.fill_between(x, y1, y2, where=(y1 < y2), facecolor='orange', alpha=0.3, hatch='\\', label='sin(x) < cos(x)')

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Filling with Patterns')
plt.legend()

plt.show()
import matplotlib.pyplot as plt
import numpy as np

# Sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)
y_upper = y + 0.2
y_lower = y - 0.2

plt.plot(x, y, label='Mean (sin(x))', color='blue')
plt.fill_between(x, y_lower, y_upper, color='blue', alpha=0.2, label='Confidence Interval')

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Confidence Interval Visualization with fill_between')
plt.legend()

plt.show()