In matplotlib versions >= 1.5, you can print the rcParam called axes.prop_cycle:
print(plt.rcParams['axes.prop_cycle'].by_key()['color'])
# [u'#1f77b4', u'#ff7f0e', u'#2ca02c', u'#d62728', u'#9467bd', u'#8c564b', u'#e377c2', u'#7f7f7f', u'#bcbd22', u'#17becf']
Or equivalently, in python2:
print plt.rcParams['axes.prop_cycle'].by_key()['color']
In versions < 1.5, this was called color_cycle:
print plt.rcParams['axes.color_cycle']
# [u'b', u'g', u'r', u'c', u'm', u'y', u'k']
Note that the default color cycle changed in version 2.0.0 http://matplotlib.org/users/dflt_style_changes.html#colors-in-default-property-cycle
Answer from tmdavison on Stack OverflowIn matplotlib versions >= 1.5, you can print the rcParam called axes.prop_cycle:
print(plt.rcParams['axes.prop_cycle'].by_key()['color'])
# [u'#1f77b4', u'#ff7f0e', u'#2ca02c', u'#d62728', u'#9467bd', u'#8c564b', u'#e377c2', u'#7f7f7f', u'#bcbd22', u'#17becf']
Or equivalently, in python2:
print plt.rcParams['axes.prop_cycle'].by_key()['color']
In versions < 1.5, this was called color_cycle:
print plt.rcParams['axes.color_cycle']
# [u'b', u'g', u'r', u'c', u'm', u'y', u'k']
Note that the default color cycle changed in version 2.0.0 http://matplotlib.org/users/dflt_style_changes.html#colors-in-default-property-cycle
Often, there is no need to get the default color cycle from anywhere, as it is the default one, so just using it is sufficient.
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
t = np.arange(5)
for i in range(4):
line, = ax.plot(t,i*(t+1), linestyle = '-')
ax.plot(t,i*(t+1)+.3,color = line.get_color(), linestyle = ':')
plt.show()

In case you want to use the default color cycle for something different, there are of course several options.
"tab10" colormap
First it should be mentionned that the "tab10" colormap comprises the colors from the default color cycle, you can get it via cmap = plt.get_cmap("tab10").
Equivalent to the above would hence be
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
t = np.arange(5)
cmap = plt.get_cmap("tab10")
for i in range(4):
ax.plot(t,i*(t+1), color=cmap(i), linestyle = '-')
ax.plot(t,i*(t+1)+.3,color=cmap(i), linestyle = ':')
plt.show()
Colors from color cycle
You can also use the color cycler directly, cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']. This gives list with the colors from the cycle, which you can use to iterate over.
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
t = np.arange(5)
cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']
for i in range(4):
ax.plot(t,i*(t+1), color=cycle[i], linestyle = '-')
ax.plot(t,i*(t+1)+.3,color=cycle[i], linestyle = ':')
plt.show()
The CN notation
Finally, the CN notation allows to get the Nth color of the color cycle, color="C{}".format(i). This however only works for the first 10 colors (N in [0,1,...9])
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
t = np.arange(5)
for i in range(4):
ax.plot(t,i*(t+1), color="C{}".format(i), linestyle = '-')
ax.plot(t,i*(t+1)+.3,color="C{}".format(i), linestyle = ':')
plt.show()
All codes presented here produce the same plot.
Default color cycler for plots should have more than 8 colors
Changing default colors of matplotlib for scatterplot
Change default color cycle
python - How to set the default color cycle for all subplots with matplotlib? - Stack Overflow
So I'm making a scatterplot where in we plot A vs B and then based on a third variable (which is either, 0, 1, 2, 3 or 4) the dot on the graph has a different color. But one of the colors is yellow... which sucks for white backgrounds.
So I want to change the default colors picked by matplotlib that are assigned to each value of the third variable. The only thing I could find so far is to change the colormap with cmap=cm.xxx but that doesn't really work for me, since most cmaps still have yellow in it, or have just generally ugly colors.
So how do I change the default colors?
plt.scatter(df['A'],df['B'], s=5, c=df['third_variable'], edgecolors=(1,1,1,0))
Sure! Either specify axes.color_cycle in your .matplotlibrc file or set it at runtime using matplotlib.rcParams or matplotlib.rc.
As an example of the latter:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
# Set the default color cycle
mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=["r", "k", "c"])
x = np.linspace(0, 20, 100)
fig, axes = plt.subplots(nrows=2)
for i in range(10):
axes[0].plot(x, i * (x - 10)**2)
for i in range(10):
axes[1].plot(x, i * np.cos(x))
plt.show()

Starting from matplotlib 1.5, mpl.rcParams['axes.color_cycle'] is deprecated. You should use axes.prop_cycle:
import matplotlib as mpl
mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=["r", "#e94cdc", "0.7"])
Hello everyone,
I've noticed something interesting when it comes to a scatter plot colors in Matplotlib.
Lets take a basic example
import matplotlib.pyplot as plt
file1=[1,2,3,4,5]
file2=[1,2,3,4,5]
fig,ax=plt.subplots()
def fun():
for x,y in zip(file1,file2):
ax.scatter(x,y)
plt.show()
def fun2():
ax.scatter(file1,file2)
plt.show()When you are plotting in a loop:
fun()
The colors come out randomly. Each dot has its own color. However, if you scatter plot a list instead.
fun2()
Then all the colors are the same.
Why is this the case? The only thing I can think of is matplotlib takes each value in a loop as an independent species (i.e. it treats it as an individual case), whereas in a list each value is part of a group (i.e. it treats the list as an individual case, and all values in the list are part of that case). And thus, the colors are assigned per case basis.
Finally, I actually like the random colors, is there any way to get those colors back when plotting the list instead of within a loop?