【问题标题】:How to open a new window for each graph plot so they stay displayed?如何为每个图表打开一个新窗口以便它们保持显示?
【发布时间】:2020-07-14 13:10:06
【问题描述】:

我有一个运行正常的程序,可以从提供的数据文件中绘制图表。该程序打开一个 csv 文件,过滤第 5 列中的数据以获取唯一值,并为每个唯一值以及原始文件中的数据写入一个 csv 文件,然后我绘制它以比较每列中的不同值。 所以对于这个例子,我应该在第 5 列中有 3 个唯一值,这会创建 3 个唯一的 csv 文件,前缀为蓝色、绿色和红色。然后我为每个蓝绿色红色文件绘制第 2 列并在图中进行比较 - 所以我将遍历蓝色、绿色、红色 csv 文件中的所有列并在图中比较它们。

我想在查看下一个绘图时保持上一个绘图打开,而不是关闭绘图窗口。我曾尝试使用 plt.figure() 但同时打开三个图表 - 为我刚开始学习 Python 的糟糕编程道歉。感谢任何帮助/建议。

从程序的第 40 行开始是我遇到问题的绘图代码

from matplotlib import style
from matplotlib import pyplot as plt
import numpy as np
import csv
# import random used for changing line colors in chart
import random
from itertools import cycle

# opens a the input file and reads in the data
with open('Test_colours_in.csv', 'r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
# prints list of unique values in column 5 of csv of input file
    my_list = set()
    for line in csv_reader:
        my_list.add(line['Name5'])
    print(my_list)

# takes these unique values and creates files associated with each unique value
    for item in my_list:
        with open(item + '_'+'Test.csv', 'w', newline='') as new_file:
            fieldnames = ['Name1', 'Name2', 'Name3', 'Name4', 'Name5', 'Name6', 'Name7', 'Name8']
            csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames)
            csv_writer.writeheader()

# filters the original file for each item in the list of unique values and writes them to respective file
            csv_file.seek(0)  # Reposition to front of file
            filtered = filter(lambda r: r['Name5'] == item, csv_reader)
            for row in filtered:
                csv_writer.writerow(row)
# Section of code below plots data from each of the filtered files
# reads in columns 1 for time (x) and 3,4 for y1 and y2

# to test for data type in column 0,1,2
# loops up to 3 (not including 3 )
#
# list of color for plots https://matplotlib.org/3.1.0/gallery/color/named_colors.html
# color values choice below
# b , g, r, c, m, y, tab:blue, tab:orange, tab:purple, tab:gray
#
# this is the bit of code I am have trouble with I need to run through a list of columns and
# have previous plots visible when a look at the next graph. At the moment I have just picked the first
# three column and loop through these
    for i in range(3):
        # apples, pears and oranges really Name1,Name2 and Name3 will change these later
        user_input = input('enter Name1, Name2 or Name3, q to quit ?')
        sample_string = user_input
        if sample_string[:1] == 'q':
            break
        if sample_string == 'Name1':
            user = int(0)
        if sample_string == 'Name2':
            user = int(1)
        if sample_string == 'Name3':
            user = int(2)

        for item in my_list:
            print(user)

            # Chooses a random number between 1 and 16777215
            random_number = random.randint(1, 16777215)
            # convert the random number to a hex number then to string
            hex_number = str(hex(random_number))
            # [2;] removes the 0x from the hexadecimal and add #
            hex_number = '#' + hex_number[2:]
            print(random_number)
            x, y = np.loadtxt(item + '_'+'Test.csv', skiprows=1, usecols=[0, user], unpack=True, delimiter=',')
            plt.plot(x, y, hex_number, label=item, linewidth=5)

            style.use('ggplot')

        plt.title('Data v Time')
        plt.ylabel('Data')
        plt.xlabel('Time seconds')

        plt.legend()
        plt.grid(True, color='k')
        plt.show()

下面的csv文件

姓名1,姓名2,姓名3,姓名4,姓名5,姓名6,姓名7,姓名8 1,2,3,4,红色,6,7,8 2,5,6,7,蓝色,6,7,8 3,5,7,7,蓝色,6,7,8 4,5,8,7,蓝色,6,7,8 5,2,4,4,红色,6,7,8 6,2,5,4,红色,6,7,8 7,8,10,10,绿色,6,7,8 8,8,11,10,绿色,6,7,8 9,8,12,10,绿色,6,7,8

【问题讨论】:

  • 嗨 - 感谢您的回复。正如你所说,我确实在一张图表中获得了所有信息。但我想做的是让每个情节在我移动到下一个选择时保留在屏幕上,以便我进行比较。
  • 我更新了答案,请看一下
  • 从第二个答案中查看 plt.ion()

标签: python matplotlib plot


【解决方案1】:

一次显示所有图

plt.show() 移出迭代(向左移动一级)for i in range(3):

要保持交互模式,请使用 plt.ion()(在第一次使用 plt 之前调用它)更多数据 here

【讨论】:

    【解决方案2】:

    如果你想在每次迭代中一张一张地显示图,那么你可以这样做:

        for i in range(3):
            # ...
    
            plt.figure()
    
            plt.title('Data v Time')
            plt.ylabel('Data')
            plt.xlabel('Time seconds')
            plt.legend()
            plt.grid(True, color='k')
    
            plt.show(block=False)
            plt.pause(0.01)
        plt.show()  # Prevents termination after the loop
    

    【讨论】:

    • 您好,谢谢。我可能不清楚,当我运行我的程序时,我得到了第一个图,但是需要关闭它才能返回程序运行第二个图。
    • 所以我的目标是让第一个图在我返回程序运行它以查看第二个图时仍然可见。抱歉我不够清楚
    • 如果我理解正确,我相信代码可以满足您的需求。当您绘制下一个图表时,图表将保持可见。
    猜你喜欢
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多