简体   繁体   English

在 Seaborn Matplotlib 中将 Y 轴零点移动到中心

[英]Move Y-Axis Zero Point to the Center in Seaborn Matplotlib

I have a chart that also has negative values which means I have bars that are above the x-axis spine and below.我有一个图表也有负值,这意味着我的条形图位于 x 轴脊柱上方和下方。 I want to move the 0 point of the Y-Axis to the middle.我想将 Y 轴的 0 点移到中间。 How can I do this?我怎样才能做到这一点?

    plt.style.use('dark_background')
    self.figure = plt.figure(figsize=(12, 1), dpi=120)
    # plt.subplots_adjust(bottom=0.15)
    self.canvas = FigureCanvas(self.figure)
    self.figure.clear()

    self.figure.add_subplot(111)
    df = pd.DataFrame({'Year': years, 'Receivables': receivable, 'Sales': orders, 'Payments': payment})

    for i in df.index:
        word = df.loc[i, "Receivables"]
        y = df.loc[i, "Receivables"]
        plt.annotate(f'{word:,.0f}', (i, y), rotation=90, ha="left", va="bottom", fontsize=8)

    p = sns.barplot(x='Year', y='Receivables', data=df)
    p.axes.set_title("Receivables".format(year), fontsize=11)
    sns.lineplot(data=orders, sort=False, legend=True, label='Sales')
    sns.lineplot(data=payment, sort=False, legend=True, label='Payments')

在此处输入图像描述

I found an answer by trial and error.我通过反复试验找到了答案。

So, instead of repositioning it manually.因此,与其手动重新定位它。 I changed the axes Y-axis limit.我更改了轴 Y 轴限制。 Since I think matplotlib sets the limits too high in my chart:因为我认为 matplotlib 在我的图表中设置的限制太高:

p = sns.barplot(x='Year', y='Receivables', data=df)
p.axes.set_title("Receivables".format(year), fontsize=11)
sns.lineplot(data=orders, sort=False, legend=True, label='Sales')
sns.lineplot(data=payment, sort=False, legend=True, label='Payments')
p.set_xticklabels(years, fontsize=8, rotation=90, verticalalignment='top')

Added this: p.axes.set_ylim(-8000000, 8000000)添加了这个: p.axes.set_ylim(-8000000, 8000000)

So that the values aren't too small when plotted.以便绘制时值不会太小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM