2013-06-21 36 views
10

我有两个列表如下:蟒蛇:如何绘制不同的颜色一行

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

现在我要绘制这样的线,分开的那些“LATT”和“隆特”记录各10作为一个时期,并给它一个独特的颜色。 我该怎么办?

+0

看看'scatter' http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter – tacaswell

回答

19

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

如果你只是要密谋极少数(如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

+1

我认为OP需要点标记,而不是线段。 – tacaswell

+0

啊,我以为他想要的是“现在我想把这个作为一条线”,但是在重新阅读时,你可能是对的。 –

3

this example复制:

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.collections import LineCollection 
from matplotlib.colors import ListedColormap, BoundaryNorm 

x = np.linspace(0, 3 * np.pi, 500) 
y = np.sin(x) 
z = np.cos(0.5 * (x[:-1] + x[1:])) # first derivative 

# Create a colormap for red, green and blue and a norm to color 
# f' < -0.5 red, f' > 0.5 blue, and the rest green 
cmap = ListedColormap(['r', 'g', 'b']) 
norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N) 

# Create a set of line segments so that we can color them individually 
# This creates the points as a N x 1 x 2 array so that we can stack points 
# together easily to get the segments. The segments array for line collection 
# needs to be numlines x points per line x 2 (x and y) 
points = np.array([x, y]).T.reshape(-1, 1, 2) 
segments = np.concatenate([points[:-1], points[1:]], axis=1) 

# Create the line collection object, setting the colormapping parameters. 
# Have to set the actual values used for colormapping separately. 
lc = LineCollection(segments, cmap=cmap, norm=norm) 
lc.set_array(z) 
lc.set_linewidth(3) 

fig1 = plt.figure() 
plt.gca().add_collection(lc) 
plt.xlim(x.min(), x.max()) 
plt.ylim(-1.1, 1.1) 

plt.show() 
2

恶癖的颜色选择关闭@JoeKington的,

import numpy as np 
import matplotlib.pyplot as plt 

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

plt.scatter(latt, lont, c=uniqueish_color(len(latt))) 

你可以用scatter做到这一点。