python种颜色循环,Python绘制颜色和颜色未知行数的标签没有循环

Researching this, I have found answers which involved using an iterator and a loop to plot n sets of data.

Is there a way to do it without using a loop?

I have an array odata which contains n columns. The first column is the abscissa. I want to plot the rest.

I have a line of code like this:

plt.plot(odata[:,0],odata[:,1:],'-')

This does the plot, But I want to add my own colors and labels. How?

解决方案

You can access the Line2D objects that are created using the return value of plt.plot, and set the legend directly based on that list:

labels = ['a', 'b', 'c']

lines = plt.plot(odata[:, 0], odata[:, 1:], '-')

plt.legend(lines, labels)

The number of labels and lines does not necessarily have to match. If there are fewer lines, some of the labels will be unused. If there are fewer labels, some of the lines will be unlabeled. Here is the legend guide in the documentation.

To seamlessly change the order of the colors, you would need to set the cycler in the global configuration via matplotlib.rc as in this example from the docs, or use the object-oriented API to do your plotting. Then you can use set_prop_cycle on your individual axes without messing around with the global settings.

Here are three approaches for setting the color cycler in order of increasing personal preference. Note that I am only showing how to set the color here, but you can also control the sequence of line styles and probably other attributes as well:

Set the global configuration:

import matplotlib as mpl

from matplotlib import cycler

from matplotlib import pyplot as plt

labels = ['a', 'b', 'c']

colors = ['r', 'g', 'b']

mpl.rc('axes', prop_cycle=cycler('color', ['r', 'g', 'b', 'y']))

lines = plt.plot(odata[:, 0], odata[:, 1:], '-')

plt.legend(lines, labels)

Set the global configuration, but using the rc_context context manager to make the changes effectively local to your plot:

import matplotlib as mpl

from matplotlib import cycler,rc_context

from matplotlib import pyplot as plt

labels = ['a', 'b', 'c']

colors = ['r', 'g', 'b']

with rc_context(rc={'axes.prop_cycle': cycler('color', ['r', 'g', 'b', 'y'])}):

lines = plt.plot(odata[:, 0], odata[:, 1:], '-')

plt.legend(lines, labels)

Set up the plot using the object-oriented API to begin with, and apply changes only to the Axes that you actually want to modify:

from matplotlib import pyplot as plt

labels = ['a', 'b', 'c']

colors = ['r', 'g', 'b']

fig, ax = plt.subplots()

ax.set_prop_cycle('color', colors)

ax.plot(odata[:, 0], odata[:, 1:], '-')

ax.legend(labels)

I would recommend the object-oriented API as a general rule, especially within standalone scripts because it offers much greater flexibility, and also clarity in terms of knowing exactly what objects will be operated on.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以使用matplotlib库中的colormap来实现Python中的颜色循环变化。下面是一个简单的示例代码: ``` import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2*np.pi, 100) colors = plt.cm.viridis(np.linspace(0, 1, len(x))) for i in range(len(x)): plt.plot(x[i], np.sin(x[i]), 'o', color=colors[i]) plt.show() ``` 在这个示例中,我们生成了一个包含100个据点的x轴组,然后使用viridis colormap将颜色映射到这个组的每个据点上。最后,我们使用这些颜色绘制了一条正弦曲线,并使用颜色循环据点进行了着色。 您可以更改colors = plt.cm.viridis(np.linspace(0, 1, len(x)))中的colormap来使用不同的颜色映射,以实现不同的颜色循环变化。 ### 回答2: 在Python中,我们可以使用turtle库来实现颜色循环变化。turtle库是Python的一个图形库,它可以用来绘制各种形状和图案。 首先,我们需要导入turtle库,并创建一个turtle对象,例如使用以下命令创建一个t对象: ``` import turtle t = turtle.Turtle() ``` 接下来,我们可以使用循环语句来实现颜色循环变化。turtle库中提供了一个colors列表,其中包含了一些常见的颜色名称,我们可以利用这个列表来实现循环变化。例如,我们可以使用以下命令来实现颜色的变化: ``` colors = ["red", "blue", "green", "yellow"] for i in range(4): t.pencolor(colors[i]) t.forward(100) t.right(90) ``` 在这个例子中,我们定义了一个包含四种颜色的列表colors。然后,使用循环语句for来遍历这个列表。在每次循环中,我们使用t.pencolor()函将画笔的颜色设置为列表中的一个颜色,然后绘制一个正方形。 如果我们想要实现更多种颜色循环变化,可以在colors列表中添加更多的颜色名称。 通过以上的方法,我们可以实现在Python中使用turtle库来实现颜色循环变化。希望对你有帮助! ### 回答3: 在Python中实现颜色循环变化可以通过使用turtle模块中的colormode()函和color()函实现。 首先,我们需要设置颜色模式,即颜色范围。turtle模块默认的颜色范围为0-255,通过调用colormode(255)函颜色模式设置为255。 接下来,我们可以定义一个循环,用来改变颜色。可以通过调用color()函来设置绘制图形时要使用的颜色。color()函接受一个RGB值作为参,我们可以将RGB值通过循环改变来实现颜色循环变化。 以下是一个简单的例子: ```python import turtle # 设置颜色模式为255 turtle.colormode(255) # 定义颜色列表 colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"] # 循环变化颜色 for i in range(100): # 设置颜色为当前循环对应的颜色 turtle.color(colors[i % len(colors)]) # 绘制图形 turtle.forward(100) turtle.right(90) # 结束绘画 turtle.done() ``` 在上面的例子中,我们定义了一个颜色列表colors,包含了几种颜色。然后在循环中,使用了取余操作符%来获取当前循环对应的颜色,从而实现颜色循环变化。通过调用turtle.forward()和turtle.right()函绘制了一个正方形,重复100次。最后调用turtle.done()结束绘图。 这样就实现了在Python中使用turtle模块实现颜色循环变化的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值