2021年1月7日 星期四

[ 常見問題 ] Pandas Plot: scatter plot with index

 Source From Here

Question
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:
  1. df:  
  2. group people value  
  3.    1    5    100  
  4.    2    2    90  
  5.    1    10   80  
  6.    2    20   40  
  7.    1    7    10  
I want to create a scatter plot with index on x axis, only using pandas datframe:
  1. 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

how to perfom this plot with pandas dataframe

HowTo
You can try and use:
  1. import pandas as pd  
  2.   
  3. df = pd.DataFrame(list(range(7)), columns=['value'])  
  4. df.reset_index().plot.scatter(x='index', y='value')  
or use matplotlib directly:
  1. import matplotlib.pyplot as plt  
  2.   
  3. plt.scatter(df.index, df.value)  
  4. plt.xlabel("index")  
  5. plt.ylabel("value")  
  6. plt.show()  


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...