【发布时间】:2019-02-20 20:24:38
【问题描述】:
我正在尝试制作一个简单的绘图,它读取存储在文件中的 x、y 值,并为每行使用不同的颜色绘制它们。以下是我的尝试。
## to run ctrl+shift+b
#print("hello world")
import csv
import itertools
import pylab
import numpy as np
import matplotlib.pyplot as plt
f = open('2016-09-09-22_25_A_initial-Hysteresis.txt', 'r')
f2 = open('2016-09-09-22_25_F_initial-Hysteresis.txt', 'r')
f3 = open('2016-09-09-22_25_K_initial-Hysteresis.txt', 'r')
f4 = open('2016-09-09-22_25_P_initial-Hysteresis.txt', 'r')
f5 = open('2016-09-09-22_25_U_initial-Hysteresis.txt', 'r')
f6 = open('2016-09-09-22_26_A_initial-Hysteresis.txt', 'r')
f7 = open('2016-09-09-22_26_F_initial-Hysteresis.txt', 'r')
f8 = open('2016-09-09-22_26_K_initial-Hysteresis.txt', 'r')
f9 = open('2016-09-09-22_26_P_initial-Hysteresis.txt', 'r')
f10 = open('2016-09-09-22_26_U_initial-Hysteresis.txt', 'r')
x = f.readlines()
x2 = f2.readlines()
x3 = f3.readlines()
x4 = f4.readlines()
x5 = f5.readlines()
x6 = f6.readlines()
x7 = f7.readlines()
x8 = f8.readlines()
x9 = f9.readlines()
x10 = f10.readlines()
vars = [x, x2, x3, x4, x5, x6, x7, x8, x9, x10]
colors = ['b', 'r', 'g', 'c']
cc = itertools.cycle(colors)
datatable = []
i = 0
for content in vars:
for line_num, line_content in enumerate(content):
data = line_content.split()
row = [[], [], [], []]
for index, num in enumerate(data, start = 0):
isappendable = False
try:
data_attempt = float(num)
index = int(index)
row[index] = data_attempt
if(line_num > 15):
isappendable = True
except ValueError:
data_attempt = 0
if(isappendable):
datatable.append(row)
i = i + 1
c = next(cc)
index = []
input = []
output = []
for n,x in enumerate(datatable):
input.append(x[0])
output.append(x[1])
num_str = str(i)
name = "DUT" + num_str
plt.plot(input, output, label = name)
plt.legend(loc='upper left')
plt.show()
这看起来非常接近我想要的(有 10 条线彼此明显重叠)但我遇到的问题是图例中的颜色似乎与图中的颜色不匹配。图例显示了我希望绘图的颜色,但线条都是相同的(即使我放大它们)。我将如何解决这个问题?谢谢
【问题讨论】:
-
看起来您正在绘制相同的数据 10 次,因此您看到的只是最后一个绿色数据。重新检查您的代码或数据。尝试仅绘制到 DUT9 并查看是否仅获得与 DUT9 对应的颜色。
-
plt.plot(输入,输出,标签=名称,颜色=颜色[line_num])
标签: python matplotlib plot colors