python - 如何为每个图表打开一个新窗口以便它们保持显示?
问题描述
我有一个工作正常的程序,可以从提供的数据文件中绘制图表。该程序打开一个 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
解决方案
推荐阅读
- java - HSQL 数据库用户缺少权限或找不到对象错误尝试将数据插入数据库
- regex - Bash 脚本中的 grep -ioP '(?<=Arguments=\")(.*)(?=":Language=)'
- android-layout - 如何让一个布局占据其他布局未使用的所有空间
- c# - 带有异步 MemberData 的 xUnit 理论
- instance - 我如何知道消息使用了哪个 BizTalk 实例?
- django - 如何在 Django 中为多个网站设置会话数据?
- xaml - 导航页上的 UWP 内存泄漏
- maven - 什么是满足 Maven 传递依赖的好策略
- python - 在单元测试中使用全局变量
- wordpress - Woocommerce 3.3 中的发布缩略图选项