Skip to content Skip to sidebar Skip to footer

Colormap Lines Showing Up As The Same Color.

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 col

Solution 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 inrange(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 inenumerate(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

Post a Comment for "Colormap Lines Showing Up As The Same Color."