如何在 Python Matplotlib 中绘制多变量函数?


要在 Python 中绘制多变量函数,我们可以执行以下步骤 -

步骤

  • 设置图片大小并调整子图之间的填充间距。

  • 使用 numpy 创建随机的xyz数据点。

  • 创建图片和一组子图。

  • 使用xyz数据点创建散点图。

  • 为 ScalarMappable 实例创建一个颜色条,s

  • 要显示图片,请使用show()方法。

示例

Open Compiler
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True def func(x, y): return 3 * x + 4 * y - 2 + np.random.randn(30) x, y = np.random.randn(2, 30) y *= 100 z = func(x, y) fig, ax = plt.subplots() s = ax.scatter(x, y, c=z, s=100, marker='*', cmap='plasma') fig.colorbar(s) plt.show()

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

输出

它将生成以下输出 -

更新于: 2022-02-01

6K+ 查看次数

开启您的 职业

通过完成本课程获得认证

开始
广告