0

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:

enter image description here

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.

Community
  • 1
  • 1
JAG2024
  • 3,987
  • 7
  • 29
  • 58

1 Answers1

1

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()

enter image description here

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)

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Is there a way to just manually input the names in the legend instead of "red Band 0", etc. but to define a vector with the strings I want to use? @ImportanceOfBeingErnest – JAG2024 Apr 19 '17 at 02:08