Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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 matplotlib画布未重新调整大小_Python_Canvas_Matplotlib_Bar Chart - Fatal编程技术网

python matplotlib画布未重新调整大小

python matplotlib画布未重新调整大小,python,canvas,matplotlib,bar-chart,Python,Canvas,Matplotlib,Bar Chart,我是python新手,所以请耐心听我说。 我的python脚本读取一个文件,然后将其解析为一个数组。 然后使用matplotlib库绘制条形图。 由于“datadump.txt”文件中的数据不断变化,我在脚本中添加了一个无限循环来读取数据,解析数据,然后绘制数据。 一切正常。问题是,当我尝试用鼠标重新调整窗口大小时,画布或绘图保持不变,不会重新调整到新的窗口大小 如何使画布重新调整为新的窗口大小 以下是我的python脚本: #! /usr/bin/python2.7 #! /usr/lib/p

我是python新手,所以请耐心听我说。 我的python脚本读取一个文件,然后将其解析为一个数组。 然后使用matplotlib库绘制条形图。 由于“datadump.txt”文件中的数据不断变化,我在脚本中添加了一个无限循环来读取数据,解析数据,然后绘制数据。 一切正常。问题是,当我尝试用鼠标重新调整窗口大小时,画布或绘图保持不变,不会重新调整到新的窗口大小

如何使画布重新调整为新的窗口大小

以下是我的python脚本:

#! /usr/bin/python2.7
#! /usr/lib/pymodules/python2.7

import time
import matplotlib.pyplot as plt

# Global variables
fig = plt.figure()


def animated_graph():
    printonce = True
    while (True):
        # Make sure that the file is not currently being access by another program
        # If it is, then let the user know.
        try:
            file = open('datadump.txt', 'r')
        except IOError:
            if (printonce == True):
                print "The file is empty or does not exist."
                printonce = False

        # If the file is accessible, then execute the code below
        with file:
            file.seek(0)
            first_char = file.read(1)
            if first_char:
                # Extract all data and create a matrix containing string values
                table = [row.strip().split('\t') for row in file]
                # Close the file once the data has been extracted
                file.close()

                # If the table is not empty, then continue with the execution
                if table:
                    numrow = len(table)
                    numcol = len(table[0])

                    #print ("num of rows: " + str(numrow))
                    #print ("num of cols: " +  str(numcol))

                    tcp = 0
                    udp = 0
                    http = 0
                    dns = 0
                    icmp = 0

                    # Go thru each row and combine the total count of each protocol of for all IPs
                    for r in range(1, numrow):
                        for c in range(1, numcol):
                            if c==1:
                                tcp = tcp + int(table[r][c])
                            elif c==2:
                                udp = udp + int(table[r][c])
                            elif c==3:
                                http = http + int(table[r][c])
                            elif c==4:
                                dns = dns + int(table[r][c])
                            elif c==5:
                                icmp = icmp + int(table[r][c])


                    '''
                    print "tcp:  " + str(tcp)
                    print "udp:  " + str(udp)
                    print "http: " + str(http)
                    print "dns:  " + str(dns)
                    print "icmp: " + str(icmp)
                    '''
                    gridnumber = range(1,6)
                    labels = ["tcp", "udp", "http", "dns", "icmp"]
                    #plt.bar(gridnumber, [tcp, udp, http, dns, icmp], color="red", width=0.4, label="Total # of connections", align="center")
                    plt.clf()
                    plt.bar(1, tcp, color="red", width=0.4, label="tcp " + str(tcp), align="center")
                    plt.bar(2, udp, color="green", width=0.4, label="udp " + str(udp), align="center")
                    plt.bar(3, http, color="blue", width=0.4, label="http " + str(http), align="center")
                    plt.bar(4, dns, color="brown", width=0.4, label="dns " + str(dns), align="center")
                    plt.bar(5, icmp, color="gold", width=0.4, label="icmp " + str(icmp), align="center")
                    plt.xlim([0,8])
                    plt.xticks(gridnumber, labels)
                    plt.xlabel("Protocols")
                    plt.ylabel("Total # of packets")
                    plt.title("Number of packets in a time window of 5secs")
                    plt.legend()
                    fig.canvas.draw()
                    #time.sleep(1)

def main():
    length = 0
    print "\nOpening file 'datadump.txt' "
    printonce = True
    while (True):
        try:
            length = len(open("datadump.txt").readlines())
            break
        except IOError:
            if (printonce == True):
                print "The file is empty or does not exist."
                printonce = False

    #print "%d lines in your choosen file" % length
    win = fig.canvas.manager.window
    win.after(1, animated_graph)
    plt.figure(fig.number)
    plt.show()

main()

您需要让GUI事件循环屏住呼吸,调用
time.sleep()
并不能完全做到这一点,尽管这几乎是要做的事情

在我的代码中,我通过调用
plt.waitforbuttonpress(.1)
来解决这个问题,其中.1是等待的超时时间(以秒为单位),然后允许调用返回并继续运行您正在进行的任何其他操作


我怀疑这将允许gui捕获调整大小事件并正确处理它们。

您需要让gui事件循环屏住呼吸,调用
time.sleep()
并不能完全做到这一点,尽管这几乎是需要做的事情

在我的代码中,我通过调用
plt.waitforbuttonpress(.1)
来解决这个问题,其中.1是等待的超时时间(以秒为单位),然后允许调用返回并继续运行您正在进行的任何其他操作


我怀疑这将允许gui捕获调整大小事件并正确处理它们。

您是否可以将此示例简化为只包含导致问题的代码(大多数代码看起来像是数据垃圾代码),并让其他人可以运行它(我们没有您的数据文件->我们无法测试任何内容)?您使用的是哪种后端?您是否有理由尝试复制其事件循环的工作?您是否可以将此示例简化为只包含导致问题的代码(其中大多数看起来像是数据吞噬代码),并使其可由其他人运行(我们没有您的数据文件->我们无法测试任何内容)? 您使用的是哪个后端,您是否有理由尝试复制它的事件循环的工作?