python - 让多个 matplotlib 轴使用相同的颜色循环器
问题描述
我想知道是否有一种简单的方法可以让 matplotlib 对子图中的所有轴使用相同的颜色循环,而不是在绘制到新轴时“重新启动”它。
MWE:
import matplotlib
import numpy as np
matplotlib.rcParams['axes.prop_cycle'] = matplotlib.cycler(
color=['red', 'blue', 'black']
)
X = np.linspace(0, 100, 100)
fig, (ax1, ax2) = matplotlib.pyplot.subplots(nrows=2)
ax1.plot(X, 1.5*X) # plots red line
ax1.plot(X, 0.9*X) # plots blue line
ax2.plot(X, X) # plots red line again, rather than black line
解决方案
As a prerequisite, the first color is used sequentially in other graphs. If you want to draw in your own color, use C2, just as you would use the defaults from C0 to C9. You can also make a list of your own colors and use them. See here.
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['axes.prop_cycle'] = plt.cycler(
color=['red', 'blue', 'black']
)
X = np.linspace(0, 100, 100)
fig, (ax1, ax2) = plt.subplots(nrows=2)
ax1.plot(X, 1.5*X) # plots red line
ax1.plot(X, 0.9*X) # plots blue line
ax2.plot(X, X, color='C{}'.format(2)) # plots red line again, rather than black line
plt.show()
Put another way.
cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']
ax2.plot(X, X, color= cycle[2])
推荐阅读
- regex - 在 VScode 中查找和删除组标签
- python - 检查每个字母
- uwp - 为什么 Header 比 Title 大,什么时候使用?
- vulkan - Vulkan 队列提交同步 - vkWaitForFences vs vkQueueWaitIdle
- javascript - 无法在基于反应的应用程序上单击带有 selenium webdriver 的按钮
- python - session.add() 和会话方法不存在
- ios - SwiftUI:我想每天显示一个随机元素,即使在重新启动应用程序之后也是如此
- typescript - 类型“{}”上不存在属性“onMessage”。& “{}”类型的参数不能分配给“null”类型的参数
- mysql - 如果 Id,如何获取名称
- python - 计算列表列表中的词频