gpt4 book ai didi

python - 是否可以忽略 Matplotlib 的第一个默认绘图颜色?

转载 作者:太空狗 更新时间:2023-10-30 01:47:44 25 4
gpt4 key购买 nike

Matplotlib 绘制了我的矩阵 a 的每一列,其中 4 列分别为蓝色、黄色、绿色、红色。 enter image description here

然后,我仅绘制矩阵 a[:,1:4] 的第二、三、四列。是否可以让 Matplotlib 默认忽略蓝色并从黄色开始(这样我的每一行都与之前的颜色相同)? enter image description here

a = np.cumsum(np.cumsum(np.random.randn(7,4), axis=0), axis=1)

lab = np.array(["A","B","C","E"])

fig, ax = plt.subplots()
ax.plot(a)
ax.legend(labels=lab )
# plt.show()
fig, ax = plt.subplots()
ax.plot(a[:,1:4])
ax.legend(labels=lab[1:4])
plt.show()

最佳答案

用于连续线条的颜色来自颜色循环仪。为了跳过这个颜色循环中的颜色,你可以调用

ax._get_lines.prop_cycler.next()  # python 2
next(ax._get_lines.prop_cycler) # python 2 or 3

完整的示例如下所示:

import numpy as np
import matplotlib.pyplot as plt

a = np.cumsum(np.cumsum(np.random.randn(7,4), axis=0), axis=1)
lab = np.array(["A","B","C","E"])

fig, ax = plt.subplots()
ax.plot(a)
ax.legend(labels=lab )

fig, ax = plt.subplots()
# skip first color
next(ax._get_lines.prop_cycler)
ax.plot(a[:,1:4])
ax.legend(labels=lab[1:4])
plt.show()

关于python - 是否可以忽略 Matplotlib 的第一个默认绘图颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46670710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com