简体   繁体   English

如何使用matplotlib和python更改多线图上的颜色?

[英]How to change colors on multilines plotting using matplotlib and python?

I am using a dictionary to plot lines with python and matplotlib i can't figure out why the colors of my lines are not changing 我正在使用字典用python和matplotlib绘制线条我不知道为什么我的线条颜色没有改变

from datetime import datetime
import matplotlib.pyplot as plt


dico =  {'A01': [(u'11/10/12-08:00:01', 2.0), (u'11/10/12-08:10:00', 10.0), \
                 (u'11/10/12-08:20:01', 5.0), (u'11/10/12-08:30:01', 15.0), \
                 (u'11/10/12-08:40:00', 7.0), (u'11/10/12-08:50:01', 45.0)],
         'A02': [(u'11/10/12-08:00:01', 10.0), (u'11/10/12-08:10:00', 12.0), \
                 (u'11/10/12-08:20:01', 15.0), (u'11/10/12-08:30:01', 10.0), \
                 (u'11/10/12-08:40:00', 17.0), (u'11/10/12-08:50:01', 14.0)]}

x = []
y = []
lstPlot = []
plt.gca().set_color_cycle(["b", "g", "r", "c", "m", "y", "k"])
for key, values in dico.iteritems():
    for i in  sorted(values):
        # date sting to date obj
        dateObj = datetime.strptime(i[0], "%d/%m/%y-%H:%M:%S")
        line = dateObj, i[1]
        lstPlot.append(line)
    for i in sorted(lstPlot):
        x.append(i[0])
        y.append(i[1])
    plt.plot(x, y, label=key)



# plotting

plt.legend(loc='upper right')
plt.xlabel('Dates')
plt.ylabel("titre")
plt.title("Modbus")
plt.show()

Note that I have different colors in the legend but not in the plot. 请注意,图例中有不同的颜色,但情节中没有。

They are changing, but you're overplotting one with the other. 它们正在发生变化,但是您正在彼此过度绘图。 These lines 这些线

x = []
y = []
lstPlot = []

need to be inside the loop. 必须在循环 Otherwise lstPlot will just grow. 否则, lstPlot将会增长。 For example, adding print lstPlot inside the loop gives: 例如,在循环内添加print lstPlot可以得到:

[(datetime.datetime(2012, 10, 11, 8, 0, 1), 10.0), (datetime.datetime(2012, 10, 11, 8, 10), 12.0), (datetime.datetime(2012, 10, 11, 8, 20, 1), 15.0), (datetime.datetime(2012, 10, 11, 8, 30, 1), 10.0), (datetime.datetime(2012, 10, 11, 8, 40), 17.0), (datetime.datetime(2012, 10, 11, 8, 50, 1), 14.0)]
[(datetime.datetime(2012, 10, 11, 8, 0, 1), 10.0), (datetime.datetime(2012, 10, 11, 8, 10), 12.0), (datetime.datetime(2012, 10, 11, 8, 20, 1), 15.0), (datetime.datetime(2012, 10, 11, 8, 30, 1), 10.0), (datetime.datetime(2012, 10, 11, 8, 40), 17.0), (datetime.datetime(2012, 10, 11, 8, 50, 1), 14.0), (datetime.datetime(2012, 10, 11, 8, 0, 1), 2.0), (datetime.datetime(2012, 10, 11, 8, 10), 10.0), (datetime.datetime(2012, 10, 11, 8, 20, 1), 5.0), (datetime.datetime(2012, 10, 11, 8, 30, 1), 15.0), (datetime.datetime(2012, 10, 11, 8, 40), 7.0), (datetime.datetime(2012, 10, 11, 8, 50, 1), 45.0)]

(You'll probably have to scroll over to see that the second list is a lot longer than the first, but you should notice that the first value is the same in both.) (您可能需要滚动查看第二个列表比第一个列表长很多,但是您应该注意,第一个值在两个列表中都相同。)

So you can either clear the lists inside, or maybe you can simplify it a bit: 因此,您可以清除其中的列表,也可以稍微简化一下:

for key, values in dico.iteritems():
    points = [(datetime.strptime(i[0], "%d/%m/%y-%H:%M:%S"), i[1]) for i in values]
    points.sort()
    x, y = zip(*points)
    plt.plot(x, y, label=key)

That code, with the addition of @bmu's suggestion of 该代码,加上@bmu的建议

plt.gcf().autofmt_xdate()

to automatically make the x-axis look nice, produces 自动使x轴看起来不错,产生

固定影像

[Alternatively, you might want to use get_xticklabels() and methods like set_rotation for finer control.] [或者,您可能希望使用get_xticklabels()set_rotation方法set_rotation进行更好的控制。

Try : 尝试:

colors = ["b", "g", "r", "c", "m", "y", "k"]
for (key, values), c in zip(dico.iteritems(), colors):
    ...
    plt.plot(x, y, c, label=key)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM