如何在 IPython 笔记本中隐藏 <matplotlib.lines.Line2D>?

matplotlibserver side programmingprogramming更新于 2025/5/5 22:07:17

要在调用 plot() 方法时隐藏 matplotlib.lines.Line2D 实例,我们可以采取以下步骤 −

  • 将 numpy 导入为 np。

  • 从 ma​​tplotlib 导入 pyplot 作为 plt。

  • 为 x 创建点,即 np.linspace(1, 10, 1000)

  • 现在使用 plot() 方法绘制线条。

  • 要隐藏实例,请使用 plt.plot(x); (带分号)

  • 或者,使用 _ = plt.plot(x)。

示例

In [1]: import numpy as np
In [2]: from matplotlib import pyplot as plt
In [3]: x = np.linspace(1, 10, 1000)
In [4]: plt.plot(x)
Out[4]: []
In [5]: plt.plot(x);
In [6]: _ = plt.plot(x)
In [7]:

输出

Out[4]: [<matplotlib.lines.Line2D at 0x7faa3852fac8>]

相关文章