How to plot matplotlib contour?


To plot matplotlib contour, we can take the following steps −

  • Create data points for x, y and h using numpy.

  • Using the countourf() method, create a colored 3D (alike) plot.

  • Using the set_over() method, set the color for high out-of-range values when "norm.clip = False".

  • Using the set_under() method, set the color for low out-of-range values when "norm.clip = False".

  • Using the changed() method, call this whenever the mappable is changed to notify all the callbackSM listeners to the "changed" signal.

  • Use the show() method to display the figure.

Example

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.arange(1, 10)
y = x.reshape(-1, 1)
h = x * y
cs = plt.contourf(h, levels=[10, 30, 50], colors=['yellow', 'green', 'purple'], extend='both', alpha=.90)
cs.cmap.set_over('red')
cs.cmap.set_under('blue')
cs.changed()
plt.show()

Output

Updated on: 10-Apr-2021

256 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements