Focus on IT Recommend

Home > python - Plotting color coded markers in matplotlib-basemap

python - Plotting color coded markers in matplotlib-basemap



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


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.

python matplotlib matplotlib-basemap
  this question
asked Mar 19 '15 at 14:02 JC_CL 322 2 14


1 Answers


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

Recommend:python - Plotting a map with matplotlib basemap - horribly slow

e (around 10 seconds!). This is just unreal! Setting of resolution is only "l" (low). Here is the very simple code: import matplotlibmatplotlib.use('Agg')import matplotlib.pyplot as pltfrom mpl_toolkits.basemap import Basemapm = Basem

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')

  this answer
answered Mar 19 '15 at 15:15 fhdrsdg 4,001 2 13 30      Thanks, that works. I was only looking round in the main documentation, since i thought the api part was internal stuff, only useful when you want to really change stuff under the hood. –  JC_CL Mar 19 '15 at 19:04


Recommend:python - Matplotlib Basemap Plotting Lat/Lons

Matplotlib basemap plot, using the animation function. I have tried implementing this example code into my script, but all my points plot at once. It may have something to do with my function and the for loop within the function. Any thoug

------splitte line----------------------------