Fill the area under a curve in Matplotlib python on log scale

To fill the area under a curve in Matplotlib on a log scale, you can use fill_between() combined with xscale() and yscale() methods. This technique is useful for visualizing data that spans multiple orders of magnitude.

Steps to Fill Area on Log Scale

  • Set figure size and layout parameters
  • Create data points using NumPy
  • Plot the curves using plot() method
  • Fill the area between curves using fill_between()
  • Set logarithmic scale for axes
  • Add legend and display the plot

Example

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create positive data for log scale
x = np.linspace(0.1, 10, 100)
y1 = np.exp(x/3)  
y2 = np.exp(x/5)

plt.plot(x, y1, label="y=exp(x/3)")
plt.plot(x, y2, label="y=exp(x/5)")

plt.fill_between(x, y1, y2, color="red", label="Area", alpha=0.3)

plt.xscale('log')
plt.yscale('log')

plt.legend(loc='lower right')
plt.xlabel('X (log scale)')
plt.ylabel('Y (log scale)')

plt.show()

Key Points

  • Positive values only: Log scale requires positive values for both x and y axes
  • fill_between(): Creates filled area between two curves with specified color and transparency
  • alpha parameter: Controls transparency (0 = transparent, 1 = opaque)
  • Log scale: Useful for data spanning multiple orders of magnitude

Alternative with Single Curve

You can also fill the area under a single curve against a baseline −

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(1, 100, 50)
y = np.log(x) * x

plt.plot(x, y, 'b-', linewidth=2, label='y = x*log(x)')
plt.fill_between(x, 0, y, color='blue', alpha=0.2, label='Area under curve')

plt.xscale('log')
plt.yscale('log')

plt.xlabel('X (log scale)')
plt.ylabel('Y (log scale)')
plt.legend()
plt.grid(True, alpha=0.3)

plt.show()

Conclusion

Use fill_between() with xscale('log') and yscale('log') to visualize filled areas on logarithmic scales. Ensure your data contains only positive values since log scale cannot handle zero or negative numbers.

Updated on: 2026-03-25T22:09:16+05:30

723 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements