python - Matplotlib - 使用 plt.setp() 的不同颜色

标签 python matplotlib

matplotlib.pyplot教程有以下代码:

lines = plt.plot(x1, y1, x2, y2)
# use keyword args
plt.setp(lines, color='r', linewidth=2.0)

我想知道是否可以在此语句中为不同的行指定不同的颜色。

最佳答案

您也可以使用相同的 matplotlib.pyplot.setp() 方法指定每条线的属性:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 1.0, 0.01)
y1 = np.sin(2*np.pi*x) # function 1
y2 = np.sin(4*np.pi*x) # function 2
lines = plt.plot(x, y1, x, y2)

l1, l2 = lines # split lines     
plt.setp(l1, linewidth=1, color='r', linestyle='-') # set function 1 linestyle
plt.setp(l2, linewidth=1, color='g', linestyle='-') # set function 2 linestyle

plt.show()

输出:

enter image description here

关于python - Matplotlib - 使用 plt.setp() 的不同颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45699021/

相关文章:

python - 为什么 -1 和 -2 在 CPython 中都散列为 -2?

python - 试图将 csv 的一部分提取到 numpy 数组

python - 拆分和编辑 CSV 列并按字母顺序排列

python - 通过python程序访问网络文件夹

Python - 我不知道如何在这段代码中工作

python - 在 matplotlib 中组合多个热图

python - 如何绘制时间序列的倒数

python - 使用 python 绘制曲线而不是条形图

python - 禁用 matplotlib 的默认箭头键绑定(bind)

python - 散点图点重叠轴