Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Plot: scatter plot with index [duplicate]

I am trying to create a scatter plot from pandas dataframe, and I dont want to use matplotlib plt for it. Following is the script

df:
group people value
   1    5    100
   2    2    90
   1    10   80
   2    20   40
   1    7    10

I want to create a scatter plot with index on x axis, only using pandas datframe

df.plot.scatter(x = df.index, y = df.value)

it gives me an error

Int64Index([0, 1, 2, 3, 4], dtype='int64') not in index

I dont want to use

plt.scatter(x = df.index, y = df.value)

how to perfom this plot with pandas dataframe

like image 692
Manu Sharma Avatar asked Mar 14 '19 18:03

Manu Sharma


2 Answers

You can try and use:

df.reset_index().plot.scatter(x = 'index', y = 'value')

enter image description here

like image 92
anky Avatar answered Oct 18 '22 14:10

anky


You are mixing two styles, matplotlib and the pandas interface to it. Either do it like @anky_91 suggested in their answer, or use matplotlib directly:

import matplotlib.pyplot as plt

plt.scatter(df.index, df.value)
plt.xlabel("index")
plt.ylabel("value")
plt.show()

enter image description here

like image 32
Graipher Avatar answered Oct 18 '22 14:10

Graipher