[英]How to sort values with strings on the axis in scatter plot
How to sort the values on x axis in an ascending order in a 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)
In the image,the values on x axis are randomly arranged.在图像中,x轴上的值是随机排列的。
you can use a small regex to extract the values, convert to int and sort by the integer values:您可以使用一个小的正则表达式来提取值,转换为 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.