I am trying to display some points using matplotlib, Although I can display them using print command but matplotlib gives error. The command that works is also there(commented).
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
data = np.array([[-1,-1,'C1'],[-2,-1,'C1'],[-3,-2,'C1'],[1,1,'C2'],[2,1,'C2'],[3,2,'C2']])
query=[-2.5,-1.5]
df=pd.DataFrame(data)
df.columns =['x','y','Cat']
df
for i in range(6):
if(df.ix[i]['Cat'] == 'C1'):
plt.scatter(df.iloc[i]['x'], df.iloc[i]['y'], s=150, c='r') #error line
#working linke below
#print(df.iloc[i]['x'],df.iloc[i]['y'])
else:
plt.scatter(df.iloc[i]['x'], df.iloc[i]['y'], s=150, c='b')
#working line below
#print(df.iloc[i]['x'],df.iloc[i]['y'])
Please help. Thanks in advance
Thanks @Haleemur Ali for your help I am able to run it now but still not fully functional. Not all points are showing not sure why?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
data = np.array([[-1,-1,'r'],[-2,-1,'r'],[-3,-2,'r'],[1,1,'b'],[2,1,'b'],[3,2,'b'],[-2.5,-1.5,'y']])
query=[-2.5,-1.5]
df=pd.DataFrame(data)
df.columns =['x','y','Cat']
print(df)
plt.scatter(df.x, df.y, s=150, c=df.Cat)
Graph generated




listnumpy arrays can only hold data of the same type. In your casedatais cast to a string type (<u21) because of the strings on the second index. Matplotlib does not know how to plot non-numeric data; that's the problem here.