English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matplotlib 3D contour plot

ax.contour3The D() function creates a 3D contour plot. It requires all input data to be in the form of two-dimensional regular grids and to evaluate Z data at each point. Here, a 3D sine function contour plot will be demonstrated.

Example code

# Filename: example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
#! /usr/bin/env python
 #coding=utf-8
 import matplotlib.pyplot as plt
 import numpy as np
 import math
 import seaborn as sns
 plt.rcParams['font.sans-serif'] = ['SimHei']  # Step one (replace sans-serif font)
 plt.rcParams['axes.unicode_minus'] = False  # Original text from [Lidi Huo], commercial转载 please contact the author for authorization, non-commercial please retain the original link:
 from mpl_toolkits import mplot3d
 def f(x, y):
    return np.sin(np.sqrt(x) ** 2 + y ** 2))
 x = np.linspace(-6, 6, 30)
 y = np.linspace(-6, 6, 30)
 X, Y = np.meshgrid(x, y)
 Z = f(X, Y)
 fig = plt.figure()
 ax = plt.axes(projection='3d')
 ax.contour3D(X, Y, Z, 50, cmap='binary')
 ax.set_xlabel('x')
 ax.set_ylabel('y')
 ax.set_zlabel('z')
 ax.set_title('3D contour
 plt.show()

Execute the above example code to get the following results -