  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # matplotlib: default color cycle from style  » python » matplotlib: default color cycle from style

By : Alexandr Krol
Date : November 21 2020, 09:01 AM
it helps some times Is there any way to recover the default color cycle used for plotting lines, which is somehow defined by the matplotlib stylesheet. , You can get the color cycle from the rc-params. code :
``````>>> from matplotlib import pyplot as plt
>>> color_cycle = plt.rcParams['axes.color_cycle']
>>> color_cycle
[u'b', u'g', u'r', u'c', u'm', u'y', u'k']
`````` ## How to set the default color cycle for all subplots with matplotlib?

By : tquest
Date : March 29 2020, 07:55 AM
This might help you 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:
code :
``````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.plot(x, i * (x - 10)**2)

for i in range(10):
axes.plot(x, i * np.cos(x))

plt.show()
`````` ## Get matplotlib color cycle state

By : RG Prins
Date : March 29 2020, 07:55 AM
I hope this helps . Accessing the color cycle iterator
There's no "user-facing" (a.k.a. "public") method to access the underlying iterator, but you can access it through "private" (by convention) methods. However, you'd can't get the state of an iterator without changing it.
code :
``````import matplotlib.pyplot as plt

fig, ax = plt.subplots()
color_cycle = ax._get_lines.color_cycle
# or ax._get_lines.prop_cycler on version >= 1.5
# Note that prop_cycler cycles over dicts, so you'll want next(cycle)['color']
``````
``````import matplotlib.pyplot as plt
import numpy as np

def custom_plot(x, y, **kwargs):
ax = kwargs.pop('ax', plt.gca())
base_line, = ax.plot(x, y, **kwargs)
ax.fill_between(x, 0.9*y, 1.1*y, facecolor=base_line.get_color(), alpha=0.5)

x = np.linspace(0, 1, 10)
custom_plot(x, x)
custom_plot(x, 2*x)
custom_plot(x, -x, color='yellow', lw=3)

plt.show()
`````` ## Increment matplotlib color cycle

By : Sagar Rao
Date : March 29 2020, 07:55 AM
This might help you Is there a simple way to increment the matplotlib color cycle without digging into axes internals? , You could call
code :
``````ax2._get_lines.get_next_color()
``````
``````ax2.plot([], [])
``````
``````import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
y1 = np.random.randint(10, size=10)
y2 = np.random.randint(10, size=10)*100
fig, ax = plt.subplots()
ax.plot(x, y1, label='first')
ax2 = ax.twinx()
ax2._get_lines.get_next_color()
# ax2.plot([], [])
ax2.plot(x,y2, label='second')

handles1, labels1 = ax.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()
ax.legend(handles1+handles2, labels1+labels2, loc='best')

plt.show()
`````` ## Why does color bars change the color in cycle drawing? (matplotlib)

By : Hongkang Tian
Date : March 29 2020, 07:55 AM
I hope this helps you . The problem is that you are plotting 10 different bars, all on the same axes. Your bars are being plotted at the same x coordinate and so are plotted on top of each other. This still means that your other bars are still there and so matplotlib cycles the colours of the "new" bars being plotted.
There are 2 things you can do. Firstly you can call plt.cla() which clears the current axes. This however will reset you y axis limits (which makes it look like nothing much is happening). This means you will have to set the y axis limits using ax4.set_ylim(0,20).
code :
``````import matplotlib.pyplot as plt
from matplotlib import mlab

fig = plt.figure()

ax4.set_xlim(-10,80)
ax4.set_ylim(0,30)

def view(ylist):

xmin = 0
xmax = 70.0
dx = 10
xlist = mlab.frange (xmin, xmax, dx)

plt.cla()
ax4.set_ylim(0,20)
ax4.bar(xlist, ylist, dx)  # can add color="blue" if you don't want to use plt.cla()
plt.pause(0.5)
plt.draw()

ylist = [0 for p in range(8)]
for i in range(10):
view(ylist)
ylist +=1
ylist +=2

plt.close()
`````` ## hist and axvline do not follow color cycle / matplotlib style

By : user1437480
Date : March 29 2020, 07:55 AM
may help you . I'm trying to plot a distribution and it's mean: , You need to get the next color in the cycle by using
code :
``````next(ax._get_lines.prop_cycler)['color']
``````
``````plt.style.use('ggplot')

numbers = np.random.rand(100)
fig, ax = plt.subplots()
ax.hist(numbers)

ax.axvline(numbers.mean(), color = next(ax._get_lines.prop_cycler)['color'])

plt.show()
`````` 