Adding extra contour lines using Matplotlib 2D contour plotting

To add extra contour lines using Matplotlib 2D contour plotting, we can control the density and positioning of contour lines by specifying custom levels. This allows us to highlight specific values or create more detailed visualizations.

Steps to Add Extra Contour Lines

  • Set the figure size and adjust the padding between and around the subplots.

  • Create a function f(x, y) to get the z data points from x and y.

  • Create x and y data points using NumPy.

  • Make a list of levels using NumPy to define where contour lines appear.

  • Make a contour plot using contour() method with custom levels.

  • Label the contour plot and set the title of the plot.

  • To display the figure, use show() method.

Example

Here's how to create a contour plot with extra contour lines by defining custom levels ?

import matplotlib.pyplot as plt
import numpy as np

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

def f(x, y):
    return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)

x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 40)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)
levels = np.arange(-1.0, 1.5, 0.25)
CS = plt.contour(X, Y, Z, levels=levels)

plt.clabel(CS, inline=1, fontsize=10)
plt.title('levels = {}'.format(levels.tolist()))
plt.show()

Adding More Contour Lines

To add extra contour lines, we can create a denser array of levels or add specific values ?

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [8.00, 4.00]
plt.rcParams["figure.autolayout"] = True

def f(x, y):
    return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)

x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 40)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

# More dense levels for extra contour lines
extra_levels = np.arange(-1.0, 1.5, 0.1)
CS = plt.contour(X, Y, Z, levels=extra_levels)

plt.clabel(CS, inline=1, fontsize=8)
plt.title('Extra Contour Lines with {} levels'.format(len(extra_levels)))
plt.show()

Custom Level Selection

You can also specify exact values where you want contour lines to appear ?

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [8.00, 4.00]
plt.rcParams["figure.autolayout"] = True

def f(x, y):
    return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)

x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 40)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

# Custom levels for specific contour lines
custom_levels = [-0.8, -0.5, -0.2, 0, 0.2, 0.5, 0.8, 1.0]
CS = plt.contour(X, Y, Z, levels=custom_levels)

plt.clabel(CS, inline=1, fontsize=9)
plt.title('Custom Contour Levels: {}'.format(custom_levels))
plt.show()

Key Points

  • Use np.arange() with smaller step sizes to create more contour lines

  • Custom level arrays allow precise control over contour line placement

  • The levels parameter accepts both arrays and integers (number of levels)

  • More contour lines provide better detail but can make plots cluttered

Conclusion

Adding extra contour lines in Matplotlib is achieved by customizing the levels parameter in the contour() function. Use denser level arrays for more detail or specify exact values for precise control over contour line placement.

Updated on: 2026-03-26T00:24:15+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements