I have two list as below:
我有两个列表如下:
latt=[42.0,41.978567980875397,41.96622693388357,41.963791391892457,...,41.972407378075879]lont=[-66.706920989908909,-66.703116557977069,-66.707351643324543,...-66.718218142021925]
now I want to plot this as a line, separate each 10 of those 'latt' and 'lont' records as a period and give it a unique color.what should I do?
现在我想把它画成一条线,把那些'latt'和'lont'记录中的每一个作为一个句点分开并给它一个独特的颜色。我该怎么办?
4 个解决方案
#1
20
There are several different ways to do this. The "best" approach will depend mostly on how many line segments you want to plot.
有几种不同的方法可以做到这一点。 “最佳”方法主要取决于您要绘制的线段数。
If you're just going to be plotting a handful (e.g. 10) line segments, then just do something like:
如果您只是想绘制一些(例如10个)线段,那么只需执行以下操作:
import numpy as npimport matplotlib.pyplot as pltdef 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()
If you're plotting something with a million line segments, though, this will be terribly slow to draw. In that case, use a LineCollection
. E.g.
但是,如果您正在绘制具有一百万个线段的内容,那么绘制的速度将非常慢。在这种情况下,请使用LineCollection。例如。
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.collections import LineCollectionxy = (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()
For both of these cases, we're just drawing random colors from the "gist_ncar" coloramp. Have a look at the colormaps here (gist_ncar is about 2/3 of the way down): http://matplotlib.org/examples/color/colormaps_reference.html
对于这两种情况,我们只是从“gist_ncar”coloramp中绘制随机颜色。看看这里的色彩图(gist_ncar大概是下降的2/3):http://matplotlib.org/examples/color/colormaps_reference.html
#2
3
Copied from this example:
复制自这个例子:
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.collections import LineCollectionfrom matplotlib.colors import ListedColormap, BoundaryNormx = 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 greencmap = 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()
#3
2
See the answer here to generate the "periods" and then use the matplotlib scatter function as @tcaswell mentioned. Using the plot.hold function you can plot each period, colors will increment automatically.
请参阅此处的答案以生成“句点”,然后使用matplotlib分散函数作为@tcaswell提到。使用plot.hold函数可以绘制每个周期,颜色会自动增加。
#4
2
Cribbing the color choice off of @JoeKington,
克服@JoeKington的颜色选择,
import numpy as npimport matplotlib.pyplot as pltdef 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)))
You can do this with scatter
.
你可以用散点图来做到这一点。
#1
20
There are several different ways to do this. The "best" approach will depend mostly on how many line segments you want to plot.
有几种不同的方法可以做到这一点。 “最佳”方法主要取决于您要绘制的线段数。
If you're just going to be plotting a handful (e.g. 10) line segments, then just do something like:
如果您只是想绘制一些(例如10个)线段,那么只需执行以下操作:
import numpy as npimport matplotlib.pyplot as pltdef 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()
If you're plotting something with a million line segments, though, this will be terribly slow to draw. In that case, use a LineCollection
. E.g.
但是,如果您正在绘制具有一百万个线段的内容,那么绘制的速度将非常慢。在这种情况下,请使用LineCollection。例如。
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.collections import LineCollectionxy = (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()
For both of these cases, we're just drawing random colors from the "gist_ncar" coloramp. Have a look at the colormaps here (gist_ncar is about 2/3 of the way down): http://matplotlib.org/examples/color/colormaps_reference.html
对于这两种情况,我们只是从“gist_ncar”coloramp中绘制随机颜色。看看这里的色彩图(gist_ncar大概是下降的2/3):http://matplotlib.org/examples/color/colormaps_reference.html
#2
3
Copied from this example:
复制自这个例子:
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.collections import LineCollectionfrom matplotlib.colors import ListedColormap, BoundaryNormx = 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 greencmap = 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()
#3
2
See the answer here to generate the "periods" and then use the matplotlib scatter function as @tcaswell mentioned. Using the plot.hold function you can plot each period, colors will increment automatically.
请参阅此处的答案以生成“句点”,然后使用matplotlib分散函数作为@tcaswell提到。使用plot.hold函数可以绘制每个周期,颜色会自动增加。
#4
2
Cribbing the color choice off of @JoeKington,
克服@JoeKington的颜色选择,
import numpy as npimport matplotlib.pyplot as pltdef 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)))
You can do this with scatter
.
你可以用散点图来做到这一点。