对于 plt,y 轴具有递减值而不是递增值

y axis has decreasing values instead of increasing ones for plt

我正在尝试构建直方图,这是我的代码:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

x = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','38','40','41','42','43','44','45','48','50','51','53','54','57','60','64','70','77','93','104','108','147'] #sample names
y =  ['164','189','288','444','311','216','122','111','92','54','45','31','31','30','18','15','15','10','4','15','2','8','6','4','7','5','3','3','1','10','3','3','3','2','4','2','1','1','1','2','2','1','1','1','1','1','2','1','2','2','2','1','1','2','1','1','1','1']
        
plt.bar(x, y)
        
plt.xlabel('Number of Methods')
plt.ylabel('Variables')
plt.show()

这是我得到的直方图:

我希望 y 轴上的值按升序排列。这意味着 1 应该先跟 3、5、7 等。我该如何解决这个问题?

它们并没有递减,而是按照它们在列表中的顺序排列,因为列表项是字符串。尝试

x = [int(i) for i in x]
y = [int(i) for i in y]

在绘图前将它们转换为数字。