Python matplotlib是否只返回对象而不是打印?

2024-09-30 22:10:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为python使用vscdoe。当我这样做时:

from matplotlib import pyplot

fig, ax = pyplot.subplots()

它会给我一个空白的情节。然而,当我使用

ax.plot([1, 2, 3], [1, 4, 6])

在随后的街区,我得到了一个

<matplotlib.lines.Line2D at 0x7f9f118e68b0>

我的问题是:如何让vscode打印我的绘图? 谢谢


Tags: fromimportplotmatplotlibfigaxvscode空白
3条回答

您忘记添加^{}

from matplotlib import pyplot

fig, ax = pyplot.subplots()

ax.plot([1, 2, 3], [1, 4, 6])

pyplot.show()

输出:

enter image description here

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [1, 4, 6])

plt.show()

您需要立即使用plt.show(),因为您没有打印任何图形

示例:

import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 6])
plt.show() # this line displays your graph

自定义图形

现在您已经知道如何打印图形,可以添加标题和轴标签

plt.ylabel('some numbers') # y-axis title
plt.xlabel('other numbers') # x-axis title
plt.title('My Graph') # main title
plt.show() 

输出

enter image description here

相关问题 更多 >