Jan*_*Jan 4 python plot matplotlib python-2.7
Matplotlib将我的矩阵的每列绘制a成4列蓝色,黄色,绿色,红色.

然后,我只绘制矩阵中的第二,第三,第四列a[:,1:4].是否有可能使Matplotlib从默认值中忽略蓝色并从黄色开始(所以我的每一行都得到与之前相同的颜色)?

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()
Run Code Online (Sandbox Code Playgroud)
用于连续线的颜色是来自颜色循环仪的颜色.为了跳过此颜色循环中的颜色,您可以拨打电话
ax._get_lines.prop_cycler.next() # python 2
next(ax._get_lines.prop_cycler) # python 2 or 3
Run Code Online (Sandbox Code Playgroud)
完整的示例如下所示:
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()
Run Code Online (Sandbox Code Playgroud)
为了跳过第一种颜色,我建议使用以下方法获取当前颜色的列表
plt.rcParams['axes.prop_cycle'].by_key()['color']
Run Code Online (Sandbox Code Playgroud)
如这个问题/答案所示。然后使用以下命令设置当前轴的颜色循环:
plt.gca().set_color_cycle()
Run Code Online (Sandbox Code Playgroud)
因此,您的完整示例将是:
a = np.cumsum(np.cumsum(np.random.randn(7,4), axis=0), axis=1)
lab = np.array(["A","B","C","E"])
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
fig, ax = plt.subplots()
ax.plot(a)
ax.legend(labels=lab )
fig1, ax1 = plt.subplots()
plt.gca().set_color_cycle(colors[1:4])
ax1.plot(a[:,1:4])
ax1.legend(labels=lab[1:4])
plt.show()
Run Code Online (Sandbox Code Playgroud)
这使:
| 归档时间: |
|
| 查看次数: |
1461 次 |
| 最近记录: |