Matplotlib plt.show()不显示图形 [英] Matplotlib plt.show() isn't showing graph

查看:1645
本文介绍了Matplotlib plt.show()不显示图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的绘图代码似乎没有显示图表(第12到59行可能没有打破它,但我将它们包括在内以防万一 - 我有数据需要一段时间才能放入排序列表中)。



我搞砸了,尝试了不同的东西,但我认为主要的问题是我不明白 figure() plt.show() import matplotlib.pyplot as plt from pylab import * 和其他一些行实际上意味着。我不知道什么时候需要使用它们,或者为什么我需要使用它们。

有人可以帮我解释一下如何绘制一个.svg的两个列表,并在末尾显示关于为什么包含每行的细节以及何时将 plt。在前面,当我把 ax。放在前面,并且当我没有放置任何东西时,等等。对不起,这将需要很长时间才能回答,但我真的不明白matplotlib或他们网站上的任何示例。

  import matplotlib 
matplotlib.use('SVG')
import matplotlib.pyplot as plt
导入字符串,数学
从pylab导入*
从十进制导入*

name = raw_input(输入文件名:\ n)

myfile = open(datafiles /+ name +。data,'r')

xData = []
yData = []
plots = [name]
mydata = myfile.readlines()

i = 0 $ b $ (n):
string = mydata [i]
data = [str(x)for string in string.split() ]
data = filter(lambda x:len(x)> 0,data)
xData.append(data [1])$ ​​b $ b yData.append(data [2])$ b (i if(float(xData [i])<= 0):

i = 0
b xData [i] =
yData [i] =
if(xData [i] ==nan或xData [i] ==-nan):
xDa ta [i] =
yData [i] =
i = i + 1

xData = filter(lambda x:len(x)> 0,xData )
y Data = filter(lambda x:len(x)> 0,yData)

N = len(xData)
i = 0

而(i< N):
xData [i] = float(xData [i])
yData [i] = float(yData [i])
i = i + 1
$ b $ (j i = 0
while(i <(Nj-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)


解决方案

您正试图使用​​不会产生图形的后端与 plt.show()。相反,您需要使用另一个后端,如WXAgg或QT4agg,其选择取决于您的系统。有关Matplotlib后端的此信息。相反,如果您希望使用svg格式的文件,则只能使用 plt.savefig('filename.svg')。生成的文件将在您的工作目录中,您只需在脚本完成后将其打开即可。



详细阐述一些其他问题了解每条线的意思:

<$ c假设您使用支持绘制到用户界面的后端(渲染器),$ c> plt.show() 将在您的屏幕上生成一个交互式图。



import matplotlib.pyplot as plt 只需从matplotlib包中的pyplot库中导入函数和类,并且as plt部分是有点像制作昵称以更容易地访问这些功能。例如, plt.show() plt.figure 等等,而不必输入 matplotlib.pyplot.show()每次。另一方面,来自pylab import的 * 会导入所有没有前缀的函数。一般情况下,我会避免使用 import * ,因为稍后可能会让您感到困惑。另外,pylab可能不是你在这里显示的代码所需要的东西。



plt.figure() 是初始化数字的命令。在这种情况下,由于您使用了 plot = plt.figure ,您可以输入 plot.plot(xData,yData),因为你的变量图现在是图形类的一部分。例如,如果您有一些额外的轴,子图或色条需要执行某些操作,则可以使用 ax



我真的推荐阅读matplotlib网站上的 pyplot教程,以便让您更彻底,但仍然使用matplotlib相对简单和简单的介绍。


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).

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.

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.

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)

解决方案

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 information on Matplotlib's backends. Instead, you should use only plt.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.

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.

import matplotlib.pyplot as plt simply 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.

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 ax for example if you had some additional axes, subplots or color bars on which you needed to perform some action.

I would really recommend going through the pyplot tutorial on the matplotlib website to give you a more thorough, but still relatively brief and simple introduction to using matplotlib.

这篇关于Matplotlib plt.show()不显示图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆