颜色图线显示为相同的颜色. [英] Colormap lines showing up as the same color.
问题描述
我正在扩展
但是我希望'bcs'线为黑色,而'red'线为红色.这怎么可能?
自定义图例中线条的名称也很棒,例如BCS Band 1"等.但也不知道如何执行此操作.
原则上,@ Robbies对链接问题的回答为您提供了创建所需颜色和标签的线条所需的所有工具.
将 numpy 导入为 np将熊猫作为pd导入导入matplotlib.pyplot作为pltdf = pd.DataFrame()nam = [红色",bcs"]对于我在范围(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}无花果,ax = plt.subplots()对于i,s中的enumerate(df.columns):df [s] .plot(种类='密度',color = nam [s.split("_")[0]]((i%4 + 1)/4.),label=" Band ".join(s.split("_")))plt.legend()plt.show()
当然,您也可以仅使用字符串列表作为图例条目.通过将它们提供给绘图函数的标签参数,
labels =这是带有一些自定义条目的图例" .split()对于 i, s in enumerate(df.columns):df[s].plot(kind='密度', color=nam[s.split("_")[0]]((i%4+1)/4.),标签=标签[i])
或
通过使用 legend
的 labels
参数,
labels = "这是一个带有一些自定义条目的图例".split()对于 i, s in enumerate(df.columns):df[s].plot(kind='密度', color=nam[s.split("_")[0]]((i%4+1)/4.))plt.legend(labels =标签)
I am extending this question to figure out how to make each of the lines a different shade of red or black. This might require making a custom color map or somehow tweaking the colormap "RdGy".
Using the data and packages from the last question, this is what I have so far:
df0.groupby([df0.ROI.str.split('_').str[0],'Band']).Mean.plot.kde(colormap='RdGy')
plt.legend()
plt.show()
And the figure looks like this:
But I want the 'bcs' lines to be shades of black and the 'red' lines to be shades of red. How is this possible?
It would also be great to customize the names of the lines in the legend, such as "BCS Band 1", etc.. but not sure how to do this either.
In principle @Robbies answer to the linked question gives you all the tools needed to create lines of any color and label you want.
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()
Of course you can also just use a list of strings as legend entries. Either by supplying them to the label argument of the plotting function,
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])
or
by using the labels
argument of the legend
,
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.) )
plt.legend(labels=labels)
这篇关于颜色图线显示为相同的颜色.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!