Python Matplotlib plt.show() 不显示图形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21688409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib plt.show() isn't showing graph
提问by Jamie Twells
My plotting code doesn't seem to be showing the graph (lines 12 to 59 are probably not breaking it, but I included them just in case - I had data that took a while to put into a sorted list).
我的绘图代码似乎没有显示图形(第 12 行到第 59 行可能没有破坏它,但我将它们包括在内以防万一 - 我的数据需要一段时间才能放入排序列表)。
I've messed around with it and tried different things but I think the main problem is that I don't understand what figure(), plt.show(), import matplotlib.pyplot as plt, from pylab import *and some other lines actually mean. I don't know when I need to use them or why I need to use them.
我搞砸与它周围和尝试不同的东西,但我认为主要的问题是,我不明白是什么figure(),plt.show(),import matplotlib.pyplot as plt, from pylab import *和其他一些线路的实际含义。我不知道什么时候需要使用它们,也不知道为什么需要使用它们。
Could someone help me and explain how to draw an .svg of two lists and have it show at the end with details about why each line is included and when I put plt.in front and when I put ax.in front and when I don't put anything in front, etc? Sorry this will take so long to answer, but I really don't understand matplotlib or any of the examples on their website.
有人可以帮我解释一下如何绘制两个列表的 .svg 并在最后显示有关为什么包含每行以及何时放在plt.前面以及何时放在ax.前面以及何时不放置任何内容的详细信息在前面等?抱歉,这需要很长时间才能回答,但我真的不了解 matplotlib 或他们网站上的任何示例。
import matplotlib
matplotlib.use('SVG')
import matplotlib.pyplot as plt
import string, math
from pylab import *
from decimal import *
name = raw_input("Enter the filename:\n")
myfile = open("datafiles/"+name+".data", 'r')
xData = []
yData = []
plots = [name]
mydata = myfile.readlines()
i = 0
N = len(mydata)
while (i < N):
string = mydata[i]
data = [str(x) for x in string.split(" ")]
data=filter(lambda x: len(x)>0, data)
xData.append(data[1])
yData.append(data[2])
i = i + 1
i = 0
while (i < N):
if (float(xData[i]) <= 0):
xData[i] = ""
yData[i] = ""
if (xData[i] == "nan" or xData[i] == "-nan"):
xData[i] = ""
yData[i] = ""
i = i + 1
xData=filter(lambda x: len(x)>0, xData)
yData=filter(lambda x: len(x)>0, yData)
N = len(xData)
i = 0
while (i < N):
xData[i] = float(xData[i])
yData[i] = float(yData[i])
i = i + 1
j = 0
while (j < N):
i = 0
while (i < (N-j-1)):
if (xData[i]>xData[i+1]):
a, b = xData[i], xData[i+1]
xData[i+1], xData[i] = a, b
a, b = yData[i], yData[i+1]
yData[i+1], yData[i] = a, b
i = i + 1
j = j + 1
plot = plt.figure(1)
plt.plot(xData, yData)
plt.show(1)
plt.savefig(name)
采纳答案by pseudocubic
You are attempting to use a backend that will not produce graphics with plt.show(). Instead you need to use another backend such as WXAgg or QT4agg, the selection of which will depend on your system. See this informationon Matplotlib's backends. Instead, you should use onlyplt.savefig('filename.svg')if you desire to have a file in the svg format. The resulting file will be in your working directory, you only need to open it after your script has finished.
您正在尝试使用不会生成图形的后端plt.show()。相反,您需要使用另一个后端,例如 WXAgg 或 QT4agg,其选择取决于您的系统。请参阅有关 Matplotlib 后端的此信息。相反,仅plt.savefig('filename.svg')当您希望拥有 svg 格式的文件时才应使用。生成的文件将在您的工作目录中,您只需在脚本完成后打开它。
To elaborate a bit to answer some of your other questions about not understanding what individual lines mean:
详细说明一下,以回答您关于不理解单行含义的其他一些问题:
plt.show()will produce an interactive plot on your screen, assuming you are using a backend (renderer) that supports plotting to your user interface.
plt.show()将在您的屏幕上生成一个交互式绘图,假设您使用的是支持绘制到您的用户界面的后端(渲染器)。
import matplotlib.pyplot as pltsimply imports the functions and classes from the pyplot library from the matplotlib package, and the as plt part is sort of like making a nickname to access those functions easier. For example, plt.show(), plt.figure, etc. instead of having to type out matplotlib.pyplot.show()every time. On the other hand, from pylab import *imports all of the functions without the prefix. In general, I would avoid using import *because it can be confusing to read back your code later. Also, pylab probably isn't something you need for the code you've shown here.
import matplotlib.pyplot as plt只需从 matplotlib 包中的 pyplot 库中导入函数和类,而 as plt 部分有点像使访问这些函数的昵称更容易。例如,plt.show(),plt.figure等,而不必matplotlib.pyplot.show()每次都输入。另一方面,from pylab import *导入所有没有前缀的函数。通常,我会避免使用,import *因为稍后读回您的代码可能会令人困惑。此外,pylab 可能不是您在此处显示的代码所需要的。
plt.figure()is the command that initializes your figure. In this case, since you used plot = plt.figure, you can then type plot.plot(xData, yData), because your variable plot now is part of the figure class. You would use axfor example if you had some additional axes, subplots or color bars on which you needed to perform some action.
plt.figure()是初始化图形的命令。在这种情况下,由于您使用了plot = plt.figure,您可以输入plot.plot(xData, yData),因为您的变量 plot 现在是 figure 类的一部分。你可以使用ax,如果您对您需要执行一些操作一些附加轴,次要情节或彩条的例子。
I would really recommend going through the pyplot tutorialon the matplotlib website to give you a more thorough, but still relatively brief and simple introduction to using matplotlib.
我真的建议您阅读matplotlib 网站上的pyplot 教程,为您提供更全面但仍然相对简短和简单的使用 matplotlib 的介绍。
回答by Roman
If you did pip install matplotlibin a virtualenv with --no-site-packages, and plt.show()isn't showing up your plot:
如果您pip install matplotlib在 virtualenv 中使用--no-site-packages, 并且plt.show()没有显示您的情节:
1) Either apt-getinstall matplotlib, then virtualenv --system-site-packages FOLDERNAME
1)要么apt-get安装matplotlib,然后virtualenv --system-site-packages FOLDERNAME
2) Or, from this guide:
2)或者,从本指南:
pip uninstall matplotlib
sudo apt-get install python-gtk2-dev
ln -sf /usr/lib/python2.7/dist-packages/{glib,gobject,cairo,gtk-2.0,pygtk.py,pygtk.pth} $VIRTUAL_ENV/lib/python2.7/site-packages
pip install matplotlib
There is still another step in the guide, but that wasn't neccessary for me (set the backend to GTKAggin ~/.config/matplotlib/matplotlibrc)
指南中还有另一个步骤,但这对我来说不是必需的(将后端设置为GTKAggin ~/.config/matplotlib/matplotlibrc)
回答by JacobTheKnitter
Im my case I resolved the problem by installing python 3.7 from python.org and then simply changed interpreter on my IDE (PyCharm CE) + of course installed all the necessary packages again. There was no need to use another backend and I kept TkAgg in my project.
我的情况是,我通过从 python.org 安装 python 3.7 解决了这个问题,然后简单地更改了我的 IDE (PyCharm CE) 上的解释器 + 当然再次安装了所有必要的包。不需要使用另一个后端,我在我的项目中保留了 TkAgg。

