Code中未出现Matplotlib图像

Code中未出现Matplotlib图像

本文介绍了Mac上的Visual Studio Code中未出现Matplotlib图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MacOSX上的Visual Studio代码编辑器中运行一些基本代码:

I'm running some basic code in the Visual Studio Code editor on MacOSX:

    import matplotlib.pyplot as plt
    import numpy as np
    x = np.linspace(0, 20, 100)
    plt.plot(x, np.sin(x))
    plt.show()

...并且运行此命令后似乎无法显示png/svg文件图像.这也不会停止执行,我必须手动终止该过程.但是,如果直接在终端(每行代码行)中运行此命令,则会得到结果图像.一种解决方法是仅保存文件(plt.savefig('foo.png')).这似乎可行-图像保存在指定的文件位置.但是,最好是在运行代码后才看到图像.

...and can't seem to get the png/svg file image to come up after running this. This also doesn't stop executing and I have to manually terminate the process. However, if I run this directly in the Terminal (each line of code line for line) I get the resulting image. One work-around is to just save the file (plt.savefig('foo.png')). This seems to work - the image is saved in the specified file location. However, it would be good to just see the image come up after running the code.

推荐答案

从终端运行matplotlib代码时,将图像保存到文件后,我遇到了相同类型的应用程序挂起.在这种情况下,对我一直有效的一种解决方法"是关闭阻止功能.基本上以这种方式更改代码:

When running matplotlib codes from the terminal, I experience the same kind of hanging of the application after saving the image to a file. In this case, one 'workaround' that has always worked for me is to turn off blocking. Basically alter your code in this way:

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show(block=False)
input('press <ENTER> to continue')

这不是完美的,但是在终端中按ENTER后,图像会正确保存并且应用程序停止.希望这会有所帮助.

It's not perfect, but the image is saved correctly and the application stops after you hit ENTER in the terminal. Hope this helps.

这篇关于Mac上的Visual Studio Code中未出现Matplotlib图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 04:51