I am working on a script that plots the longitude and latitude coordinates of an airplane in real time with a scatter plot. I also wish to include the temperature at each point by adding a colorbar. My data set is large (tens of thousands of points), so instead of clearing the figure and drawing it again after each point is added, I keep the figure plotted and just add one more point, using the function provided here: https://stackoverflow.com/a/53631526/12001023 My issue is that I need to set the face color of each point to correspond to a color on the colorbar. I am a novice in matplotlib but from what I understand get_facecolors() and set_facecolors() take RGB values. What I would like is for each iteration of my graph, I take my temperature array and assign each value to a color that fits the colorbar, where my max and min temperatures are the highest and lowest colors, respectively. I would then update the face colors of each point to align to my colorbar.
I'm running Python 3.7 using PyCharm Community Edition 2019.2.1 x64. Because I can't get the colors to work, I've been setting each color to black by setting c='k', and it works fine.
Here is all of my relevant code:
def addPoint(scat, new_point, c='c'):
old_off = scat.get_offsets()
new_off = np.concatenate([old_off,np.array(new_point, ndmin=2)])
old_c = scat.get_array()
new_c = np.concatenate([old_c, np.array(c, ndmin=1)])
scat.set_offsets(new_off)
scat.set_facecolor(new_c)
scat.axes.figure.canvas.draw_idle()
cm = plt.cm.get_cmap('RdYlBu')
gridsize = (5, 3)
fig, ax1 = plt.figure()
sc = ax1.scatter(long, lat, c=temp, cmap=cm)
fig.canvas.draw()
div = make_axes_locatable(ax1)
x = div.append_axes('right', '5%', '5%')
cb = fig.colorbar(sc, cax=cax)
newdata = True
while newdata:
get_GPS_data() # This updates my longitude and latitude array and adds more point to each
get_temp_data() # This updates my temperature array and adds one point
addPoint(sc, [longitude[-1],latitude[-1]], c='k')
For an example, let's say my arrays are:
longitude = [0 1 2 3 4 5]
latitude = [1 3 5 7 9 11]
temperature = [17.7 18 17.9 22.3 21.9 20]
If someone could help me figure out a way to turn the temperature array into a an array of colors, that would be great. I've also considered setting the max and min temperatures to be out of range of my temperatures (e.g. min = 15, max = 25) and normalizing everything based off that. Any help would be great.
Here is an image of my graph, showing my desired colobar on the right, and how my temperature changes over time on the bottom