Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scatter plot with multiple X features and single Y in Python

Data in form:

       x1     x2
data= 2104,    3
      1600,    3
      2400,    3
      1416,    2
      3000,    4
      1985,    4

y= 399900
   329900
   369000
   232000
   539900
   299900

I want to plot scatter plot which have got 2 X feature {x1 and x2} and single Y, but when I try

y=data.loc[:'y']
px=data.loc[:,['x1','x2']]
plt.scatter(px,y)

I get:

'ValueError: x and y must be the same size'.

So I tried this:

data=pd.read_csv('ex1data2.txt',names=['x1','x2','y'])
px=data.loc[:,['x1','x2']]
x1=px['x1']
x2=px['x2']
y=data.loc[:'y']
plt.scatter(x1,x2,y)

This time I got blank graph with full blue color painted inside. I will be great full if i get some guide

like image 598
GAURAV Sharma Avatar asked Sep 17 '25 07:09

GAURAV Sharma


1 Answers

You can only plot with one x and several y's. You could plot the different x's in a twiny axis:

fig, ax = plt.subplots()
ay = ax.twiny()

ax.scatter(df['x1'], df['y'])
ay.scatter(df['x2'], df['y'], color='r')
plt.show()

Output:

enter image description here

like image 172
Quang Hoang Avatar answered Sep 18 '25 22:09

Quang Hoang