Colormap lines are displayed in the same color.

I am expanding this question to figure out how to make each of the lines a different shade of red or black. This may require the creation of a custom color map or some customization of the "RdGy" color map.

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 red. How is this possible?

It would also be great to customize the line names in the legend like "BCS Band 1" etc., but not sure how.

0


source to share


1 answer


Basically @Robbies answers a related question, gives you all the tools you need to create lines of whatever 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 the string list as legend entries. Either by providing 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

using an argument labels

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

+1


source







All Articles