如何在 matplotlib 图表中循环遍历颜色和线型?


要循环遍历 matplotlib 图表中的颜色和线型,我们可以采取以下步骤。

步骤

  • 设置图表大小,并调整子图表之间、周围的间距。

  • 使用颜色线型,设置当前的rcParams

  • 使用 plot() 方法绘制数据点。

  • 要显示图表,请使用 Show() 方法。

示例

import matplotlib.pyplot as plt
from cycler import cycler

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Set the rcParams with color or linestyle
plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b', 'y']) +
   cycler('linestyle', [':', '-.', '-', '--'])))

# Plot the data points
plt.plot([0, 5, 2, 1])
plt.plot([2, 6, 3, 1])
plt.plot([3, 8, 5, 1])
plt.plot([4, 9, 0, 3])

plt.show()

输出

它将生成以下输出 −

更新日期: 19-10-2021

1K+ 浏览

开启你的 职业生涯

学完课程,获得认证

开始学习
广告
© . All rights reserved.