首页 > 解决方案 > 什么时候需要 plt.show() 来显示绘图,什么时候不需要?

问题描述

由于下面的代码将显示一个没有 的情节plt.show(),那有什么意义plt.show()呢?

请告诉我什么时候plt.show()需要,因为这可以让我更好地理解 matplotlib 的复杂性。

注意:我在 Spyder (Anaconda) 中使用它

import matplotlib.pyplot as plt
plt.subplot(211)             # the first subplot in the first figure
plt.plot([1, 2, 3])

标签: pythonmatplotlib

解决方案


需要或不需要,具体取决于您的脚本所在的位置。

有2个上下文。

  1. Matplotlib 用于终端或脚本中,plt.show() 是必须的。

  2. Matplotlib 用于 IPython shell 或笔记本(例如:Kaggle),不需要 plt.show()。


推荐阅读