gpt4 book ai didi

python - 如何用不同颜色绘制一条线

转载 作者:太空狗 更新时间:2023-10-29 17:37:51 26 4
gpt4 key购买 nike

我有两个列表如下:

latt=[42.0,41.978567980875397,41.96622693388357,41.963791391892457,...,41.972407378075879]
lont=[-66.706920989908909,-66.703116557977069,-66.707351643324543,...-66.718218142021925]

现在我想将其绘制成一条线,将每 10 个“latt”和“lont”记录分隔为一个句点,并赋予其独特的颜色。我该怎么办?

最佳答案

有几种不同的方法可以做到这一点。 “最佳”方法主要取决于您要绘制多少条线段。

如果您只是要绘制少量(例如 10 个)线段,则只需执行以下操作:

import numpy as np
import matplotlib.pyplot as plt

def uniqueish_color():
"""There're better ways to generate unique colors, but this isn't awful."""
return plt.cm.gist_ncar(np.random.random())

xy = (np.random.random((10, 2)) - 0.5).cumsum(axis=0)

fig, ax = plt.subplots()
for start, stop in zip(xy[:-1], xy[1:]):
x, y = zip(start, stop)
ax.plot(x, y, color=uniqueish_color())
plt.show()

enter image description here

但是,如果您要绘制具有一百万条线段的东西,绘制起来会非常慢。在这种情况下,请使用 LineCollection。例如

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

xy = (np.random.random((1000, 2)) - 0.5).cumsum(axis=0)

# Reshape things so that we have a sequence of:
# [[(x0,y0),(x1,y1)],[(x0,y0),(x1,y1)],...]
xy = xy.reshape(-1, 1, 2)
segments = np.hstack([xy[:-1], xy[1:]])

fig, ax = plt.subplots()
coll = LineCollection(segments, cmap=plt.cm.gist_ncar)
coll.set_array(np.random.random(xy.shape[0]))

ax.add_collection(coll)
ax.autoscale_view()

plt.show()

enter image description here

对于这两种情况,我们只是从“gist_ncar”coloramp 中随机绘制颜色。看看这里的颜色图(gist_ncar 大约下降了 2/3):http://matplotlib.org/examples/color/colormaps_reference.html

关于python - 如何用不同颜色绘制一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17240694/

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