如何在 matplotlib 图形上循环显示颜色和线条样式?

要在 matplotlib 图形上循环显示颜色和线条样式,我们可以采取以下步骤。

步骤

  • 设置图形大小并调整子图之间和周围的填充。

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

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

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

示例

importmatplotlib.pyplotas 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()
输出结果

它将产生以下输出 -