1

Given the following data frame:

import pandas as pd
df = pd.DataFrame({'X':[1,2,3],
                   'Y':[4,5,6],
                   'Site':['foo','bar','baz']
                   })
df

    Site    X   Y
0   foo     1   4
1   bar     2   5
2   baz     3   6

I want to iterate through rows in the data frame to produce 3 scatter plots (in this case, though a general solution for n rows is needed):

One in which the dot for "foo" is red and the rest are blue,

another in which the dot for "bar" is red and the rest are blue,

and a third in which the dot for "baz" is red and the rest are blue.

Here's a sample for "foo" done manually:

import matplotlib.pyplot as plt
%matplotlib inline
color=['r','b','b']
x=df['X']
y=df['Y']
plt.scatter(x, y, c=color, alpha=1,s=234)
plt.show()

Thanks in advance!

1 Answer 1

1

You have two options:

  1. Create two "views" from the data, one with the to-be-red elements, and other with the remaining elements. You would apply conditional slicing for that, for example. Then, you plot one set in red, and the other in blue. That would be two scatter commands in the same figure. Repeat for each set.

  2. Quite conveniently, the default jet colormap is blue and red in its extremes. You can then just call scatter once for all the data, but have the c argument of scatter set to a boolean array taken from the original data via conditional slicing. That would make the colors for the desired items be mapped to "1", while the other, false items would be "0", and colormapped accordingly.

Note: when I talk about conditional slicing, its like:

interesting_items = array[array == interesting_value]

or some equivalent in Pandas.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.