matplotlib fill_between does not cycle through colours
Strange, it seems that even calling ax._get_patches_for_fill.set_color_cycle(clist)
or ax.set_color_cycle(np.roll(clist, -1))
explicitly doesn't reinstate the color cycle (see this answer). This may be because fill between doesn't create a conventional patch or line object (or it
may be a bug?). Anyway, you could manually call a cycle on the colors like in the axis function ax.set_color_cycle
,
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
import itertools
clist = rcParams['axes.color_cycle']
cgen = itertools.cycle(clist)
x = np.asarray([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.asarray([1.0, 2.0, 3.0, 4.0, 5.0])
xerr = np.asarray([0.2, 0.4, 0.6, 0.8, 1.0])
yerr = np.asarray([0.1, 0.2, 0.3, 0.4, 0.5])
fig, ax = plt.subplots(1,1)
ax.fill_between(x, y-yerr, y+yerr,alpha=0.5, facecolor=cgen.next())
ax.fill_between(y,x-xerr,x+xerr,alpha=0.5, facecolor=cgen.next())
plt.show()
In matplotlib 3.3.3 following adjustment in the syntax proposed by Ed Smith is necessary:
clist = rcParams['axes.prop_cycle']