Matplotlib fill_between doesn't cycle colors

Most plotting methods, such as plot () and errorbar, automatically change to the next color in color_palette when you draw multiple objects on the same plot. For some reason this does not apply to fill_between (). I know I can do it tough, but it is done in a loop, which makes it annoying. Is there a good way around this?

import numpy as np
import matplotlib.pyplot as plt

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])

plt.fill_between(x, y-yerr, y+yerr,alpha=0.5)
plt.fill_between(y,x-xerr,x+xerr,alpha=0.5)

plt.show() 

Perhaps just to get the current position in the palette and repeat it until the next will be enough.

+4
source share
1 answer

, , ax._get_patches_for_fill.set_color_cycle(clist) ax.set_color_cycle(np.roll(clist, -1)) (. ). , ( ?). , , , 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() 
+2

All Articles