Aoitori
Aoitori

Reputation: 67

Python Scatterplot Index Error

I can't determine why I am getting an index error for my scatterplot.

fig,ax=plt.subplots(figsize=(12,8))
colors=['b','r']  
for i in [2,4]:
    indices=np.where(benign_or_malignant==i)
    ax.scatter(clump_thickness[indices],single_ep_cell_size[indices], 
s=55,c=colors[i])   

IndexError Traceback (most recent call last) in () 4 for i in [2,4]: 5 indices=np.where(benign_or_malignant==i) ----> 6 ax.scatter(clump_thickness[indices],single_ep_cell_size[indices], s=55,c=colors[i])

IndexError: list index out of range

The dataset is formated like so (ct=clump_thickness, secs=single_ep_cell_size, cl=clump benign(2) or malignant(4):

id     ct   secs    cl<br>
1000025 5   2   2<br>
1002945 5   7   2<br>
1015425 3   2   2<br>
1016277 6   3   2<br>
1017023 4   2   2<br>
1017122 8   7   4<br>
1018099 1   2   2<br>
1018561 2   2   2<br>
1033078 4   2   2<br>
1035283 1   1   2<br>
1036172 2   2   2<br>
1041801 5   2   4<br>

As I understand it, it should plot clump thickness vs single ep cell size and color the points differently based on whether i = 2 or 4. Can someone point me in the right direction?

Upvotes: 1

Views: 563

Answers (2)

Vaishali
Vaishali

Reputation: 38415

You can try this

fig,ax=plt.subplots(figsize=(8,6))
for name, group in df.groupby('cl'):
    ax.plot(group['ct'], group['secs'], marker='o', linestyle='', label=name, ms = 10)
ax.set(xlabel='clump thickness', ylabel='single ep cell size')
ax.legend()

enter image description here

Upvotes: 1

grand_chat
grand_chat

Reputation: 531

The list index error is referring to the colors list. colors[i] is not defined when i has value 4.

A possible fix:

colors = ['b','r']
b_or_m = [2, 4]  
for i in [0, 1]:
    indices=np.where(benign_or_malignant==b_or_m[i])
    ax.scatter(clump_thickness[indices], single_ep_cell_size[indices], s=55, c=colors[i]) 

Upvotes: 1

Related Questions