Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib - plot with a different color for certain data points

My question is similar to this question. I am plotting latitude vs longitude. If the value in a variable is 0, I want that lat/long value to be marked with a different color. How do I do that?

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

Since, matplotlib complained that it cannot use floats, 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. All of them are the same. Can someone please help me with this? Thanks.

like image 704
Sarvavyapi Avatar asked Oct 11 '13 06:10

Sarvavyapi


People also ask

How do I specify colors on a Matplotlib plot?

The usual way to set the line color in matplotlib is to specify it in the plot command. This can either be done by a string after the data, e.g. "r-" for a red line, or by explicitely stating the color argument.

How do I shade part of a graph in Matplotlib?

Plot x and y data points, with color=red and linewidth=2. To shade an area parallel to X-axis, initialize two variables, y1 and y2. To add horizontal span across the axes, use axhspan() method with y1, y2, green as shade color,and alpha for transprency of the shade. To display the figure, use show() method.

How do I change the marker color in Matplotlib?

All of the line properties can be controlled by keyword arguments. For example, you can set the color, marker, linestyle, and markercolor with: plot(x, y, color='green', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=12).

How do I highlight data points in Matplotlib?

Matplotlib has a function named annotate() to add text in a specific location in a plot. We need to specify annotate() function the text we want to annotate the plot with and the x and y co-ordinates for the location of the text.


1 Answers

You are indexing your timeDiffInt list with items from that list, if those are integers larger then the length of the list, it will show this error.

Do you want your scatter to contain two colors? One colors for values of 0 and another colors for other values?

You can use Numpy to change your list to zeros and ones:

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

Scatter will then use different colors for both values.

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

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

enter image description here

edit:

You can create colors for specific values by making a colormap yourself:

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)
like image 84
Rutger Kassies Avatar answered Sep 22 '22 02:09

Rutger Kassies