简体   繁体   中英

How to plot a single column based on its sorted values

Lets assume I have a dataframe with one column. I sorted it and when I tried to plot the sorted values, the plot is getting plotted as per indices not as per the sorted values.

How to achieve a plot which is plotted based on the sorted values?

I want the plot to be curve from top declining towards to bottom.

Ex code:

import pandas as pd
import matplotlib.pyplot as plt
a=pd.DataFrame()
a['col']=(4,5,8,10,1,0,15,20)
a_sorted=a.sort_values(by='col',ascending=False)
plt.plot(a_s)

I believe you need default index by Series.reset_index and drop=True parameter:

a_sorted=a.sort_values(by='col',ascending=False).reset_index(drop=True)

Then also working Series.plot :

a_sorted.plot()

Another solution is ploting numpy 1d array by Series.values :

a_sorted=a.sort_values(by='col',ascending=False)
plt.plot(a_sorted.values)

Or use sorting in descending order :

plt.plot(-np.sort(-a['col']))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM