Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:Tkinter与Matplotlib:1窗口在Ubuntu上,但在Windows中为2窗口_Python_Python 2.7_Matplotlib_Tkinter_Spyder - Fatal编程技术网

Python:Tkinter与Matplotlib:1窗口在Ubuntu上,但在Windows中为2窗口

Python:Tkinter与Matplotlib:1窗口在Ubuntu上,但在Windows中为2窗口,python,python-2.7,matplotlib,tkinter,spyder,Python,Python 2.7,Matplotlib,Tkinter,Spyder,我正在Ubuntu上用Python编写一个程序,它使用Matplotlib和Tkinter来显示一些图形。问题是,在Ubuntu中,它运行良好,但在Windows中,用户将使用Spyder运行它,它会生成带有绘图的Tkinter窗口,但也会生成带有相同绘图的另一个单独Matplotlib窗口。我不知道如何解决这个问题,因为我通常根本不处理Windows 以下是显示正在发生的情况的屏幕截图: Ubuntu: 窗户: 以下是生成图形的代码: # This method takes in a lot

我正在Ubuntu上用Python编写一个程序,它使用Matplotlib和Tkinter来显示一些图形。问题是,在Ubuntu中,它运行良好,但在Windows中,用户将使用Spyder运行它,它会生成带有绘图的Tkinter窗口,但也会生成带有相同绘图的另一个单独Matplotlib窗口。我不知道如何解决这个问题,因为我通常根本不处理Windows

以下是显示正在发生的情况的屏幕截图:

Ubuntu: 窗户: 以下是生成图形的代码:

# This method takes in a lot of data and creates a plot with all the data.  It takes in the x and y data points
# (for the scatter plot), the slope and intercept of the linear best-fit line, the coefficients of the logarithmic
# best-fit curve, the range of the axes to display in the plot, and the titles for the plot and the axes.
# It generates the plot displaying all of this, as well as the equations of the best-fit curves, and their
# R-squared values.  To get the R-squared values, it calls the method which calculates them.

def plotForCorrections(scatterXs, scatterYs, trendlineSlope, trendlineIntercept, logA, logB, axesRange, title, xTitle, yTitle):

    # The linear fit is a straight line, and can be plotted with just two points.
    # The x-coordinates will be the two ends of the plot (the leftmost and rightmost points displayed).
    trendlineXs = numpy.array([axesRange[0], axesRange[1]])

    # We can apply the linear fit equation to the two x-coordinates to get the two y-coordinates.
    trendlineYs = numpy.multiply(trendlineSlope, trendlineXs) + trendlineIntercept

    # We need to clear the plot figure of anything that was on it before.
    plt.clf()

    # Plot the x and y data points
    plt.scatter(scatterXs, scatterYs, color='r')

    # For the x-coordinates for the logarithmic fit curve, we need much more than two, so we
    # create a set of 200 equally spaced points between the two ends of the visible plot.
    # The leftmost point will always be 0, which we can not take the logarithm of.  To remedy that,
    # we use 10^-300 instead of 0.
    logLineXs = numpy.linspace((1e-300 if axesRange[0] == 0 else axesRange[0]), axesRange[1],num=200)

    # We plot the logarithmic fit curve, calculating the y-values of the curve in the same line.
    plt.plot(logLineXs, logFitFun(logLineXs, logA, logB), color='g')

    # We plot the linear fit line.
    plt.plot(trendlineXs, trendlineYs, color='b')

    # We set the axis range of the plot.
    plt.axis(axesRange)

    # We calculate R squared values.
    linRSquared = numpy.round(calculateLinRSquared(scatterXs,scatterYs,trendlineSlope,trendlineIntercept),3)
    logRSquared = numpy.round(calculateLogRSquared(scatterXs,scatterYs,logA,logB),3)

    # We place the equations and R squared values on the plot.
    plt.annotate("Linear trendline (blue):  " + "y = " + str(numpy.round(trendlineSlope,3)) + "x + " +
                 str(numpy.round(trendlineIntercept,3)) + ";  R" + unichr(0x00b2) + " = " + str(linRSquared) + 
                 "\nLogarithmic fit line (green):  " + "y = " + str(numpy.round(logA,3)) + " * ln(x) + " + str(numpy.round(logB,3)) +
                 ";  R" + unichr(0x00b2)+ " = " + str(logRSquared),
                 xy=(0.05,0.90),
                 xycoords="axes fraction")

    # We place the titles on the plot.
    plt.title(title)
    plt.xlabel(xTitle)
    plt.ylabel(yTitle)

    # We display the plot in its window.
    plt.gcf().canvas.draw()
和窗口的代码:

# What follows are GUI-specific things for the plot window
fig = plt.figure()
graphCanvas = FigureCanvasTkAgg(fig,master=window)
questionLabel = Label(window,text="Use this correction?")
linCorrectionButton = Button(window,text="Use Linear Correction",command=clickLinCorrection)
logCorrectionButton = Button(window,text="Use Logarithmic Correction",command=clickLogCorrection)
noCorrectionButton = Button(window,text="Do Not Use a Correction",command=clickNoCorrection)
graphCanvas.get_tk_widget().grid(row=0,column=0,columnspan=3)
questionLabel.grid(row=1,column=0,columnspan=3)
linCorrectionButton.grid(row=2,column=0)
logCorrectionButton.grid(row=2,column=1)
noCorrectionButton.grid(row=2,column=2)
cancelButton = Button(window,text="Cancel",command=destroyAndReturn)
cancelButton.grid(row=3,column=0,columnspan=3)

如果您需要任何其他代码样本,请让我知道。非常感谢大家

如果从spyder之外的控制台运行脚本,windows上是否有两个窗口?我不知道如何在spyder之外运行它,但我相信如果我这样做,它会修复它。结果证明这是Spyder的问题,要解决它,我所要做的就是确保在Run->Configure或F6菜单中的一个新的专用Python解释器中选中Execute。这样做也使Spyder在所有情况下都更容易使用,因为以前,如果我尝试运行某个程序,终止它,然后再次运行它,它通常会给出一个关于需要解释器的错误。