Y轴不在0处? (Matplotlib 条形图)

问题描述 投票:0回答:1

Y 轴不是从 0 开始,而是从最大值开始 这是证据

我真的希望 y 轴从 0 开始,否则它真的很混乱。

这是有关条形图的所有代码:

# gets the values for the x-axis (names) and y-axis (values)

def parties_values():
    global names, values
    names = []
    values = []
    with open(f"raw_stats/{day} {month} Stats.txt", "r", encoding="UTF-8") as stats:
        content = stats.read().splitlines()
        lines = len(content)
    
    for i in range(1,lines-1):
        if i % 2 == 0:
            names.append(content[i])
        else:
            values.append(content[i])
# actually gets the graph and saves it as an image

def get_graph():
    fig, ax = plt.subplots()

    plt.xlabel("Parteien")
    plt.ylabel("Anzahl der Wahlen in Prozent")

    ax.bar(names, values)
    plt.savefig(f'image_folder/{day} {month}.png')

是的,我还看了其他几篇文章,但到目前为止没有一个对我有帮助,我使用的是 matplotlib 版本 3.8.2。

python matplotlib bar-chart
1个回答
0
投票

从图像上看,你的价值观似乎在下降,这有点奇怪。但无论如何,您可以为生成的图设置 x 限制和 y 限制。你可以这样做,before

plt.savefig(...)

ax.set_ylim(0, 60)
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.