我该如何绘制一个带有不同线条颜色的pandas数据框?

3

问题标题已经解释了一切。我想绘制一个数据框,例如7行3列。我希望用随机颜色绘制每一列。我该怎么做?


这个回答解决了你的问题吗?Pandas Dataframe: plot colors by column name - Anurag Dabas
@AnuragDabas 我正在寻找一种自动化的方法,它可以将每个特定颜色写入一个字典中。 - Muh
1个回答

2

既然您没有大型数据集,您可以创建一个名为color_dict的字典,并在绘图时从中查找颜色。

import pandas as pd

data = {
    'time0': [41, 28, 33, 34, 38, 31, 37],
    'time1': [48, 26, 39, 33, 58, 41, 43],
    'time2': [53, 30, 51, 37, 48, 49, 53]
}

df = pd.DataFrame(data=data)

import random

color_dict = {}
for idx in range(df.shape[1]):
    r = random.random()
    b = random.random()
    g = random.random()
    color = (r, g, b)
    color_dict[idx] = color 
    
colors = [color_dict.get(x) for x in range(df.shape[1])]


import matplotlib.pyplot as plt

for idx in range(df.shape[1]):
    plt.plot(df.iloc[:,idx], color=colors[idx])

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接