问题
I have the following variables, that I want to plot on a map:
Lons
, a list of longitudes, Lats
, a list of latitudes and Vals
, a list of values for each location.
To plot the locations, I do a simple
x,y=Mymap(Lons,Lats)
Mymap.scatter(x,y,marker='o',color='red')
Q: how do i best turn my Vals
into a list of colors (heatmap) to be passed intoscatter
, so that every x,y
pair gets its values matching color?
The basemap docs are rather lacking, and none of the examples fits my purposes.
I could probably loop through my whole Lats
, Lons
and Vals
but given how slow basemap is, I'd rather avoid that. I already have to draw about 800 maps, and increasing that to about 1 Million will probably take years.
回答1:
The basemap scatter documentation simply refers to the pyplot.scatter documentation, where you can find the parameter c (emphasis mine):
c : color or sequence of color, optional, default
c can be a single color format string, or a sequence of color specifications of length N, or a sequence of N numbers to be mapped to colors using the cmap and norm specified via kwargs (see below). Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. c can be a 2-D array in which the rows are RGB or RGBA, however.
So if you simply pass vals
to c, matplotlib should convert the values to colors on the standard colormap or a chosen colormap. To show which color is mapped to what value you can use the colorbar.
Example code:
import matplotlib.pyplot as plt
lons = range(50)
lats = range(25, 75)
vals = range(50,100)
plt.scatter(lons, lats, c=vals, cmap='afmhot')
plt.colorbar()
plt.show()
来源:https://stackoverflow.com/questions/29146662/plotting-color-coded-markers-in-matplotlib-basemap