首页 > 解决方案 > 如何在 matplotlib.pyplot 中更改我的绘图图的 Y 比例

问题描述

with open('data/covid.csv', encoding="utf8") as file_obj:
    file_data = csv.DictReader(file_obj, skipinitialspace = True)
    #storing info of dose 1 in dictionary
    dicti = {}
    for row in file_data:
        key = row['State/UTs']
        if key in dicti:
            dicti[key] = row['Dose 1']
        else:
            key = row['State/UTs']
            dicti[key] = row['Dose 1']
    
    print(dicti)
    valuesL = list(dicti.values())
    for i in range(0, len(valuesL)):
        valuesL[i] = int(valuesL[i])
    plt.figure(figsize=(18,12))
    ax = plt.bar(list(dicti.keys()),valuesL, color = "green")
    
    plt.xlabel("State/uts", fontsize = 30)
    plt.ylabel("Number of people who have taken dose 1", fontsize = 30)
    plt.title('States VS people who have taken dose 1', fontsize = 30)
    plt.xticks(rotation=80)
    for patch in ax.patches:
        width = patch.get_width()
        height = patch.get_height()
        x = patch.get_x()
        y = patch.get_y()
    
        plt.text(x , y + height + 4, '{}'.format(height))
    
    plt.show()

这是我的代码,图表显示 y 轴的范围从 0.5 到 1,我希望它根据字典的值进行更改,范围从 10000 到 10000000

标签: pythonmatplotlib

解决方案


好像您正在寻找set_ylim

bottom = 10000
top = 10000000
ax.set_ylim(bottom, top)

或者

plt.ylim(bottom, top)

推荐阅读