Colormap线显示为相同的颜色。

时间:2017-04-18 19:48:52

标签: python pandas matplotlib plot colormap

我正在扩展this问题,以弄清楚如何使每条线条成为不同的红色或黑色。这可能需要制作自定义颜色贴图或以某种方式调整颜色图“RdGy”。

使用上一个问题的数据和包,这是我到目前为止所做的:

df0.groupby([df0.ROI.str.split('_').str[0],'Band']).Mean.plot.kde(colormap='RdGy')
plt.legend()
plt.show()

这个数字看起来像这样:

enter image description here

但我希望'bcs'线条为黑色阴影,而'红色'线条为红色阴影。这怎么可能?

自定义图例中线条的名称也很棒,例如“BCS Band 1”等。但不知道如何做到这一点。

1 个答案:

答案 0 :(得分:1)

原则上@Robbies对链接问题的回答为您提供了创建任何颜色和标签所需的所有工具。

register

enter image description here

当然,您也可以使用字符串列表作为图例条目。通过将它们提供给绘图函数的label参数,

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame()
nam = ["red", "bcs"]
for i in range(8):
    df['{}_{}'.format(nam[i//4],i%4)] = np.random.normal(i, i%4+1, 100)

nam = {"red":plt.cm.Reds, "bcs": plt.cm.gray_r}
fig, ax = plt.subplots()
for i, s in enumerate(df.columns):
    df[s].plot(kind='density', color=nam[s.split("_")[0]]((i%4+1)/4.), 
                label=" Band ".join(s.split("_")))

plt.legend()
plt.show()

使用labels = "This is a legend with some custom entries".split() for i, s in enumerate(df.columns): df[s].plot(kind='density', color=nam[s.split("_")[0]]((i%4+1)/4.), label=labels[i]) 的{​​{1}}参数,

labels

enter image description here