python and matplotlib and changing marker color
-
29-06-2021 - |
Question
I am plotting things using matplotlib and Basemap (within a wxpython gui). Currently, my plot code look something like this:
self.map = Basemap(llcrnrlon=lon_L, llcrnrlat=lat_D, urcrnrlon=lon_R,
urcrnrlat=lat_U, projection='lcc', lat_0=map_lat1, lon_0=map_lon1,
resolution='i', area_thresh=10000,ax=self.axes, fix_aspect=False)
m = Basemap(llcrnrlon=lon_L, llcrnrlat=lat_D, urcrnrlon=lon_R,
urcrnrlat=lat_U, projection='lcc', lat_0=map_lat1, lon_0=map_lon1,
resolution='i', area_thresh=10000,ax=self.axes)
x,y=m(some_x_data,some_y_data)
plot_handle, = self.map.plot(x,y,'bo')
plot_handle.set_xdata(x)
plot_handle.set_ydata(y)
self.figure.canvas.draw()
This plots it just fine. Now what I want to do is take a single point (single x and single y within my data) and color it a different color. I still want to use the plot_handle because I am constantly updating the map/plot -- so i don't want to just reset my data. Any help?
Thanks!
Solution
Do a new plot_handle for the specific plot with a different marker:
plot_handle1, = self.map.plot([x[i]], [y[i]], 'ro')
You'll then have to update this every time you want to change that point's position. It's not possible to use only one plot_handle and have points showing with different markers.
OTHER TIPS
If you use scatter
(doc) you can set and update the color of each point.
import matplotlib.pylab as plt
x,y = m(some_x,some_y)
c = iterator_of_colors
plt_handle, = self.map.scatter(x,y,c=c)
# some code
c[j] = new_color # update you color list
plt_handle.set_array(c) # update the colors in the graph
plt.draw()
It looks a little strange to use set_array
but that is how matplotlib
deals with scatter plots internally (it looks like they use the same class that is used for displaying images, only just color in markers instead of squares in the grid).