首页 > 解决方案 > 如何为每个图表打开一个新窗口以便它们保持显示?

问题描述

我有一个工作正常的程序,可以从提供的数据文件中绘制图表。该程序打开一个 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文件

Name1,Name2,Name3,Name4,Name5,Name6,Name7,Name8 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

标签: pythonmatplotlibplot

解决方案


一次显示所有图

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

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


推荐阅读