Skip to content Skip to sidebar Skip to footer

Matplotlib - Contour Plot With Single Value

I want to make a contour plot of some data, but it is possible that all values in the field at the same value. This causes an error in matplotlib, which makes sense since there re

Solution 1:

Well, contourf handles it perfectly, it's contour that chokes.

Why not just do this:

import numpy as np
import matplotlib.pyplot as plt

xi = np.array([0., 0.5, 1.0])
yi = np.array([0., 0.5, 1.0])
zi = np.ones((3,3))

try:
    CS = plt.contour(xi, yi, zi, 15, linewidths=0.5, colors='k')
except ValueError:
    pass
CS = plt.contourf(xi, yi, zi, 15, cmap=plt.cm.jet)

plt.colorbar()
plt.show()

This way, you'll get a filled (green, by default) box if there's a uniform field, and a filled contour plot with lines otherwise.

enter image description here

Post a Comment for "Matplotlib - Contour Plot With Single Value"