gpt4 book ai didi

python - matplotlib 中每个绘图的唯一绘图标记

转载 作者:IT老高 更新时间:2023-10-28 22:13:33 24 4
gpt4 key购买 nike

我有一个循环,我在其中创建了一些图,并且我需要为每个图使用唯一的标记。我考虑创建一个返回随机符号的函数,并以这种方式在我的程序中使用它:

for i in xrange(len(y)):
plt.plot(x, y [i], randomMarker())

但我认为这种方式不好。我需要这个只是为了区分图例上的图,因为图不能与线相连,它们必须只是点集。

最佳答案

itertools.cycle 将无限期地迭代列表或元组。这比为您随机选择标记的功能更可取。

Python 2.x

import itertools
marker = itertools.cycle((',', '+', '.', 'o', '*'))
for n in y:
plt.plot(x,n, marker = marker.next(), linestyle='')

Python 3.x

import itertools
marker = itertools.cycle((',', '+', '.', 'o', '*'))
for n in y:
plt.plot(x,n, marker = next(marker), linestyle='')

您可以使用它来生成这样的图(Python 2.x):

import numpy as np
import matplotlib.pyplot as plt
import itertools

x = np.linspace(0,2,10)
y = np.sin(x)

marker = itertools.cycle((',', '+', '.', 'o', '*'))

fig = plt.figure()
ax = fig.add_subplot(111)

for q,p in zip(x,y):
ax.plot(q,p, linestyle = '', marker=marker.next())

plt.show()

Example plot

关于python - matplotlib 中每个绘图的唯一绘图标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13091649/

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