如何为每个图形打开一个新窗口,使它们保持显示状态?

问题描述 投票:0回答:2

我有一个可以正常运行并从提供的数据文件中绘制图形的程序。该程序将打开一个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,名称81,2,3,4,红色,6,7,82,5,6,7,蓝色,6,7,83,5,7,7,蓝色,6,7,84,5,8,7,蓝色,6,7,85,2,4,4,红色,6,7,86,2,5,4,红色,6,7,87,8,10,10,绿色,6,7,88,8,11,10,绿色,6,7,89,8,12,10,绿色,6,7,8

python matplotlib plot
2个回答
0
投票

一次显示所有地块

plt.show()从迭代中移出(向左移一层)for i in range(3):

要保持交互模式,请使用plt.ion()个数据here


0
投票

如果要在每次迭代中一一显示图,则可以执行以下操作:

    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
© www.soinside.com 2019 - 2024. All rights reserved.