2

I have a 500 row pandas dataframe, which has 2 columns x and y referring to coordinates in the table. However I want to be able to assign a different colour to points from 0 to 249, and then 250 to 499 i.e. half are red for example and half are blue. How would I do this?

The table of 500 rows, 2 columns

The code:

diagram = pos_table.plot.scatter('x', 'y', c = 'turquoise', s = 4)

result:

The code output

Yash Dwivedi
  • 71
  • 1
  • 5
  • 1
    Does this answer your question? [Scatter plots in Pandas/Pyplot: How to plot by category](https://stackoverflow.com/questions/21654635/scatter-plots-in-pandas-pyplot-how-to-plot-by-category) – KenHBS Jan 11 '20 at 17:30

2 Answers2

1

Select first and last 250 rows by DataFrame.iloc and then pass ax to second plot:

length = len(pos_table)
half = length //
ax = pos_table.iloc[:250].plot.scatter('x', 'y', c = 'red', s = 4)
pos_table.iloc[250:].plot.scatter('x', 'y', c = 'blue', s = 4, ax=ax)

Or dynamic count values:

length = len(pos_table)
half = length // 2
ax = pos_table.iloc[:half].plot.scatter('x', 'y', c = 'red', s = 4)
pos_table.iloc[half:].plot.scatter('x', 'y', c = 'blue', s = 4, ax=ax)

Sample: (changed s for better see)

pos_table = pd.DataFrame({'x':[2,3,4,6,2,4,6,8,5,7],
                          'y':[4,3,1,4,6,8,5,3,5,4]})

length = len(pos_table)
half = length // 2
ax = pos_table.iloc[:half].plot.scatter('x', 'y', c = 'red', s = 90)
pos_table.iloc[half:].plot.scatter('x', 'y', c = 'blue', s = 90, ax=ax)

graph

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

I think the following is a nice, clean solution

colors = ['red' if i < 250 else 'blue' for i in range(500)]
pos_table.plot.scatter('x','y',color=colors)

enter image description here

Student240
  • 88
  • 6