1

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?

2 Answers 2

1

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

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

2 Comments

Thank you. How would I modify the code if the data is read from a csv file and stored in arrays? ids,clump_thickness,single_ep_cell_size,benign_or_malignant=_loadData() #this is how you invoke the helper function.
Well the dataset looked like a Pandas dataframe to me. For plotting and grouping, I would recommend loading it into the dataframe first using pd.read_csv(yourfile)
1

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]) 

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.