Matplotlib - plot with a different color for specific data points

My question is similar to this question . I draw latitude and longitude. If the value in the variable is 0, I want the lat / long value to be marked in a different color. How to do it?

This is my attempt so far. Here x holds latitude and y has longitude. timeDiff is a list containing float values, and if the value is 0.0, I want this color to be different.

Since matplotlib complained that it could not use float, I first converted the values ​​to int.

timeDiffInt=[int(i) for i in timeDiff]

Then I used list comprehension:

plt.scatter(x,y,c=[timeDiffInt[a] for a in timeDiffInt],marker='<')

But I get this error:

IndexError: list index out of range

So, I checked the lengths of x, y and timeDiffInt. They are all the same. Can someone please help me with this? Thanks.

+4
1

timeDiffInt , , , , .

, ? 0 ?

Numpy, :

timeDiffInt = np.where(np.array(timeDiffInt) == 0, 0, 1)

Scatter .

fig, ax = plt.subplots(figsize=(5,5))

ax.scatter(x,y,c=timeDiffInt, s=150, marker='<', edgecolor='none')

enter image description here

:

, :

fig, ax = plt.subplots(figsize=(5,5))

colors = ['red', 'blue']
levels = [0, 1]

cmap, norm = mpl.colors.from_levels_and_colors(levels=levels, colors=colors, extend='max')

ax.scatter(x,y,c=timeDiffInt, s=150, marker='<', edgecolor='none', cmap=cmap, norm=norm)
+8

All Articles