如何在 Python Matplotlib 中绘制多元函数?
matplotlibpythondata visualization
要在 Python 中绘制多元函数,我们可以采取以下步骤 −
步骤
设置图形大小并调整子图之间和周围的填充。
使用 numpy 创建随机 x、y 和 z 数据点。
创建一个图形和一组子图。
使用 x、y 和 z 数据点创建散点图。
为 ScalarMappable 实例 s 创建颜色条。
要显示图,使用 show() 方法。
示例
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()
输出
它将产生以下输出 −