简体   繁体   中英

y axis has decreasing values instead of increasing ones for plt

I am trying to build a histogram and here is my code:

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()

Here is the histogram I obtain:

在此处输入图像描述

I would like the values in the y axis to be in an increasing order. This means that 1 should be first followed by 3, 5, 7, etc. How can I fix this?

They're not decreasing, they're in the order in which they are in the list, because the list items are strings. Try

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

to convert them to numbers before plotting.

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