如何避免 matplotlib.pyplot 中颜色的重复?


要避免 matplotlib.pyplot 中颜色的重复,我们可以采取以下步骤 −

步骤

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

  • 使用 numpy 创建xy 数据点。

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

  • plot() 方法中,对color 属性使用唯一十六进制值,例如 color="#980ab5" 设置唯一颜色的图形。您还可以指定您选择的特定颜色,例如 color="green"

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

示例

import numpy as np
from matplotlib import pyplot as plt

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

# x and y data points
x = np.linspace(1, 100, 1000)
y = np.log(x)

# Plot the x and y data points with color attribute
plt.plot(x, y, color="#980ab5", lw=3)

# Display the plot
plt.show()

输出

它将产生如下输出 −

如果您在没有 color 属性的情况下运行上述代码,则图形将采用默认颜色。

更新于: 01-Feb-2022

945 次浏览

开启你的 职业生涯

完成课程认证

开始
广告
© . All rights reserved.