简体   繁体   中英

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. I want to move the 0 point of the Y-Axis to the middle. 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. Since I think matplotlib sets the limits too high in my chart:

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)

So that the values aren't too small when plotted.

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