Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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实时更新x-tick中的范围?_Python_Python 2.7_Matplotlib - Fatal编程技术网

Python 如何使用matplotlib实时更新x-tick中的范围?

Python 如何使用matplotlib实时更新x-tick中的范围?,python,python-2.7,matplotlib,Python,Python 2.7,Matplotlib,我有一个日志文件,每10秒更新一次。这是30秒时的日志内容 Iteration 0, data = 1.0 Iteration 10, data = 4.0 Iteration 20, data = 5.0 Iteration 30, data = 8.0 我将在Python2.7中使用matplotlib来创建实时图形。它工作得很好,没有小问题。问题是x-tick的值表示迭代次数未清除以前的范围,导致x-tick中显示的范围重叠,而y-tick中显示相同的问题。这意味着如果我在log.txt

我有一个日志文件,每10秒更新一次。这是30秒时的日志内容

Iteration 0, data = 1.0
Iteration 10, data = 4.0
Iteration 20, data = 5.0
Iteration 30, data = 8.0
我将在Python2.7中使用matplotlib来创建实时图形。它工作得很好,没有小问题。问题是x-tick的值表示迭代次数未清除以前的范围,导致x-tick中显示的范围重叠,而y-tick中显示相同的问题。这意味着如果我在log.txt中再添加一行作为迭代40,data=5.0,那么x-tick值将在旧迭代[0,10,20,30]和[0,10,20,30,40]之间重叠。我只想展示最新的范围[0,10,20,30,40]。我怎样才能修好它

这是我的密码

import matplotlib.pyplot as plt
import re
from mpl_toolkits.axes_grid1 import host_subplot

plt.ion()
while True:
    f = open("./log.txt", 'r')
    iterations = []
    data = []
    for line in f:
        if 'Iteration ' in line and 'data = ' in line:
            arr = re.findall(r'\b\d+\b,', line)
            iterations.append(int(arr[0].strip(',')[0:]))
            data.append(float(line.strip().split(' ')[-1]))
    f.close()

    host = host_subplot(111)  # , axes_class=AA.Axes)
    plt.subplots_adjust(right=0.75)
    host.set_xlabel("Iterations")
    host.set_ylabel("Loss")
    p1, = host.plot(iterations, data, label="Data")
    plt.pause(3)

我想你有两个选择。在任何情况下,都需要在循环外部创建轴,否则最终会出现大量重叠轴

清除轴:

更新数据:


如果我用legend呢?host.legendloc=2 host.axis[左].label.set\u colorp1.get\u color。我该怎么把它搬到哪里去?我不明白你在问什么。没有图例,但是如果您想使用图例,这里有很多关于图例的问题。@ImportanceOfBegingErnest:您可以检查第二个选项吗。它不起作用
import matplotlib.pyplot as plt
import re
from mpl_toolkits.axes_grid1 import host_subplot

plt.ion()
host = host_subplot(111) 
plt.subplots_adjust(right=0.75)

while True:
    f = open("./log.txt", 'r')
    iterations = []
    data = []
    for line in f:
        if 'Iteration ' in line and 'data = ' in line:
            arr = re.findall(r'\b\d+\b,', line)
            iterations.append(int(arr[0].strip(',')[0:]))
            data.append(float(line.strip().split(' ')[-1]))
    f.close()

    host.clear()
    host.set_xlabel("Iterations")
    host.set_ylabel("Loss")
    p1, = host.plot(iterations, data, label="Data")
    plt.draw()
    plt.pause(3)
import matplotlib.pyplot as plt
import re
from mpl_toolkits.axes_grid1 import host_subplot

plt.ion()

host = host_subplot(111)
plt.subplots_adjust(right=0.75)
host.set_xlabel("Iterations")
host.set_ylabel("Loss")
p1, = host.plot([],[], label="Data")

while True:
    f = open("./log.txt", 'r')
    iterations = []
    data = []
    for line in f:
        if 'Iteration ' in line and 'data = ' in line:
            arr = re.findall(r'\b\d+\b,', line)
            iterations.append(int(arr[0].strip(',')[0:]))
            data.append(float(line.strip().split(' ')[-1]))
    f.close()
    p1.set_data(iterations, data)
    host.relim()
    host.autoscale_view()
    plt.draw()
    plt.pause(3)