考虑这个绘制两个数字的例子:
from numpy import *
import matplotlib.pyplot as plt
plt.gca().set_color_cycle(['black','red'])
x = linspace(0.,10.,100)
y1 = sin(x)
y2 = cos(x)
plt.plot(x,y1,x,y2)
plt.show()
导致:
结果很好,但我收到了这个警告:
MatplotlibDeprecationWarning: The set_color_cycle attribute was deprecated in version 1.5. Use set_prop_cycle instead.
warnings.warn(message, mplDeprecation, stacklevel=1)
Here我试图研究一个例子,但我无法理解。
如何正确使用“set_prop_cycle”?我试过了:
plt.set_prop_cycle(['black','red'])
但它不起作用。我收到此错误消息:
AttributeError: module 'matplotlib.pyplot' has no attribute 'set_prop_cycle'
答案 0 :(得分:1)
根据color cycle文档,set_prop_cycle()
可以应用于plt.rc()
或matplotlib.axes.Axes
对象。
这是使用Axes对象执行此操作的一种方法:
from cycler import cycler
# separate the figure and axis elements of plt
f, ax = plt.subplots()
cy = cycler('color', ['black', 'red'])
ax.set_prop_cycle(cy)
ax.plot(x,y1,x,y2)
FWIW对于这样的事情你可能会发现Pandas读得更清楚:
data = {"x":x, "y1":y1, "y2":y2}
colors = ["black", "red"]
pd.DataFrame(data).set_index("x").plot(color=colors)