Given the following data frame and line graph:
import matplotlib.pyplot as plt
from cycler import cycler
import numpy as np
fig, ax=plt.subplots(1)
d=pd.DataFrame({'a':[1,2,3,4],
'b':[2,3,4,5],
'c':[3,4,5,6]})
colors=['r','g','b']
ax.set_prop_cycle(cycler('color', [colors]))
ax.plot(d[:3],'-ko',d[2:],'--ko')
plt.show()
You'll notice that I am trying to assign one color per line but it is not working. I also tried using the colors argument in ax.plot. It seems like this should be straight forward.
Thanks in advance for any help on this.
There are two problems in your code.
the 'k'
in '-ko'
and '--ko'
sets the colour to black, so we need to remove that
colors
is already a list, but you have put it inside square brackets again in the call to set_prop_cycle, and thus made it into a nested list: [['r','g','b']]
. Remove the square brackets there and it all works fine: ax.set_prop_cycle(cycler('color', colors))
So, your code will look like:
import matplotlib.pyplot as plt
import pandas as pd
from cycler import cycler
import numpy as np
fig, ax=plt.subplots(1)
d=pd.DataFrame({'a':[1,2,3,4],
'b':[2,3,4,5],
'c':[3,4,5,6]})
colors=['r','g','b']
ax.set_prop_cycle(cycler('color', colors))
ax.plot(d[:3],'-o',d[2:],'--o')
plt.show()