How to make a circular matplotlib.pyplot.contourf?

To create a circular matplotlib.pyplot.contourf plot, you need to generate data in a circular pattern and use contourf() with proper aspect ratio settings. This technique is useful for visualizing radial data patterns or creating circular heatmaps.

Steps to Create Circular Contour Plot

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

  • Create x, y, a, b and c data points using NumPy

  • Create a figure and a set of subplots

  • Make a contour plot using contourf() method

  • Set the aspect ratios to maintain circular shape

  • Display the figure using show() method

Example

Here's how to create a circular contour plot using a radial distance function ?

import numpy as np
import matplotlib.pyplot as plt

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

x = np.linspace(-0.5, 0.5, 100)
y = np.linspace(-0.5, 0.5, 100)

a, b = np.meshgrid(x, y)
c = a ** 2 + b ** 2 - 0.2

figure, axes = plt.subplots()

axes.contourf(a, b, c)

axes.set_aspect(1)

plt.show()

Creating Multiple Circular Patterns

You can create more complex circular patterns by modifying the function ?

import numpy as np
import matplotlib.pyplot as plt

# Create coordinate arrays
x = np.linspace(-2, 2, 200)
y = np.linspace(-2, 2, 200)
X, Y = np.meshgrid(x, y)

# Create circular patterns
Z1 = np.sqrt(X**2 + Y**2)  # Distance from center
Z2 = np.sin(np.sqrt(X**2 + Y**2) * 3)  # Ripple pattern

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))

# Simple circular gradient
contour1 = ax1.contourf(X, Y, Z1, levels=20, cmap='viridis')
ax1.set_aspect('equal')
ax1.set_title('Circular Gradient')

# Circular ripple pattern
contour2 = ax2.contourf(X, Y, Z2, levels=20, cmap='plasma')
ax2.set_aspect('equal')
ax2.set_title('Circular Ripple Pattern')

plt.tight_layout()
plt.show()

Key Points

  • set_aspect('equal') or set_aspect(1) ensures the plot maintains circular shape

  • Use np.meshgrid() to create coordinate arrays for the contour function

  • The function a**2 + b**2 creates concentric circles based on distance from origin

  • Adjust the levels parameter in contourf() to control the number of contour bands

Conclusion

Creating circular contour plots requires generating radial data patterns and setting equal aspect ratios. Use np.meshgrid() for coordinates and set_aspect('equal') to maintain the circular shape in your visualization.

---
Updated on: 2026-03-26T00:20:53+05:30

897 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements