gpt4 book ai didi

Python Matplotlib : plot with 2-dimensional arguments : how to specify options?

转载 作者:太空狗 更新时间:2023-10-30 02:21:16 26 4
gpt4 key购买 nike

我正在绘制如下几条曲线:

import numpy as np
import matplotlib.pyplot as plt

plt.plot(x, y)

其中 xy 是二维的(在本例中假设为 N x 2)。

现在我想独立设置每条曲线的颜色。我试过这样的事情:

plot(x, y, color= colorArray)

例如colorArray= ['red', 'black'],但无济于事。其他选项(线型、标记等)也是如此。

我知道这可以通过 for 循环来完成。然而,由于此 plot 命令接受多维 x/y,我认为应该也可以通过这种方式指定绘图选项。

这可能吗?执行此操作的正确方法是什么? (我在搜索时发现的所有内容都有效地使用了循环)

最佳答案

你可以使用 ax.set_color_cycle :

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2013)
N = 10
x, y = np.random.random((2,N,2))
x.cumsum(axis=0, out=x)
y.cumsum(axis=0, out=y)
fig, ax = plt.subplots()
colors = ['red', 'black']
ax.set_color_cycle(colors)
ax.plot(x,y)
plt.show()

产量 enter image description here

关于Python Matplotlib : plot with 2-dimensional arguments : how to specify options?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17171122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com