簡體   English   中英

如何在散點 plot 中對軸上的字符串值進行排序

[英]How to sort values with strings on the axis in scatter plot

如何在散點 plot 中按升序對 x 軸上的值進行排序?

fig, ax = plt.subplots()
fig.set_size_inches(18, 8)
ax.scatter(data=ipl,x='budget',y='player')

n = 4

for idx, label in enumerate(ax.xaxis.get_ticklabels()):
    if idx % n != 0:
        label.set_visible(False)

在圖像中,x軸上的值是隨機排列的。

和

您可以使用一個小的正則表達式來提取值,轉換為 int 並按 integer 值排序:

#dummy data
ipl = pd.DataFrame(
    {
        'budget': ['${}M'.format(a) for a in np.random.randint(0,200,10)],
        'player': np.random.randint(-10,10,10)
    }

)

ipl['values'] = ipl['budget'].str.extract('([0-9]+)').astype(int)
ipl.sort_values(by='values', inplace=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM