如何在 Matplotlib 中绘制图中的轴线?
matplotlibpythondata visualization
要在 Matplotlib 中绘制图中的轴线,我们可以采取以下步骤 −
- 设置图形大小并调整子图之间和周围的填充。
- 创建一个新图形或激活现有图形。
- 使用 numpy 创建 x 个数据点。
- 将 'ax' 添加到图形作为子图排列的一部分。
- 使用 plot() 方法绘制 x 和 x**x 数据点。
- 将左侧和底部位置设置为 0,而右侧和顶部脊柱的颜色为无。
- 要显示图形,请使用 show() 方法。
示例
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() x = np.linspace(-5, 5, 100) ax = fig.add_subplot(111) ax.plot(x, x*x) ax.spines['left'].set_position('zero') ax.spines['right'].set_color('none') ax.spines['bottom'].set_position('zero') ax.spines['top'].set_color('none') plt.show()
输出
它将产生以下输出