简体   繁体   English

对于 for 循环的每次迭代,如何在 plot 中循环通过 colors

[英]How to cycle through colors in a plot for each iteration of a for loop

I am trying to run a for loop that cycles through colours for each iteration of a for loop.我正在尝试运行一个 for 循环,该循环为 for 循环的每次迭代循环颜色。 I am finding similar questions that cycle through colours but not one that is dependent on a particular for loop.我发现类似的问题在颜色之间循环,但不是一个依赖于特定 for 循环的问题。 I provide some links below:我在下面提供了一些链接:

How to pick a new color for each plotted line within a figure in matplotlib? 如何为 matplotlib 中的图形中的每条绘制线选择新颜色?

matplotlib.cycler

Color cycler demo 彩色循环仪演示

My code is for a simple random walk我的代码是一个简单的随机游走

# Parameters
ntraj=10
n=20
p=0.4

# Initialize holder for trajectories
xtraj=np.zeros(n+1,float)

# Simulation
for j in range(ntraj):
    for i in range(n):
        xtraj[i+1]=xtraj[i]+2.0*np.random.binomial(1,p)-1.0

    plt.plot(range(n+1),xtraj,'b-',alpha=0.2)
plt.title("Simple Random Walk")  

I would like to create a line with a different colour for each j .我想为每个j创建一条具有不同颜色的线。 I apologize if the answer is obvious.如果答案很明显,我很抱歉。 I am a novice at python.我是 python 的新手。

As it is now, new colour will be taken for each line.就像现在一样,将为每一行采用新颜色。 If you want to limit choices and loop through a list you can use itertools.cycle :如果你想限制选择并遍历一个列表,你可以使用itertools.cycle

from itertools import cycle

colours = cycle(['red', 'green', 'blue'])


# Simulation
for j in range(ntraj):
    for i in range(n):
        xtraj[i+1]=xtraj[i]+2.0*np.random.binomial(1,p)-1.0

    plt.plot(range(n+1),xtraj,'b-',alpha=0.2, color=colours.next())

I added a list of colors.我添加了 colors 的列表。 I'm pretty sure they can be RGB or Hex.我很确定它们可以是 RGB 或 Hex。 Then inside the j loop the color will switch to the next index.然后在 j 循环内,颜色将切换到下一个索引。

colors = ['b','g','r','c','m','y']
# Parameters

# Simulation
for j in range(ntraj):
    color = colors[j % len(colors)]
    for i in range(n):
        xtraj[i+1]=xtraj[i]+2.0*np.random.binomial(1,p)-1.0

    plt.plot(range(n+1),xtraj,"{}-".format(color),alpha=0.2)
plt.title("Simple Random Walk")  

Choose any colour palette you like from matplotlib.cmmatplotlib.cm选择您喜欢的任何调色板

Try:尝试:

# Parameters
ntraj=10
n=20
p=0.4

colors = plt.cm.jet(np.linspace(0,1,ntraj))# Initialize holder for trajectories
xtraj=np.zeros(n+1,float)


# Simulation
for j in range(ntraj):
    for i in range(n):
        xtraj[i+1]=xtraj[i]+2.0*np.random.binomial(1,p)-1.0

    plt.plot(range(n+1),xtraj,'b-',alpha=0.2, color=colors[j])

plt.title("Simple Random Walk")

在此处输入图像描述

You have several options using matplotlib.pylplot.您有几个使用 matplotlib.pylplot 的选项。

Besides the already provided solutions, you can define your color directly and change the values depending on your for loop:除了已经提供的解决方案之外,您还可以直接定义颜色并根据 for 循环更改值:

 # Parameters
ntraj=10
n=20
p=0.4

xtraj=np.zeros(n+1,float)


# Simulation
for j in range(ntraj):
    for i in range(n):
        xtraj[i+1]=xtraj[i]+2.0*np.random.binomial(1,p)-1.0

    ctemp = 0.1+(j-1)/ntraj
    plt.plot(range(n+1),xtraj,'b-',alpha=0.2, color=(ctemp, ctemp, ctemp))

plt.title("Simple Random Walk")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM