Show signed changes around zero

How do I draw positive and negative changes around zero?

In Matplotlib, pass signed changes [-4,2,-1,5] directly to barh and add a vertical zero line.

On this page

Published and tested 22 November 2026.

Which code change produces this result?

Negative widths extend left and positive widths extend right. I keep the signs in the data and choose colors from the sign only as an additional cue. The first comparison deliberately uses absolute values, which removes direction.

This article uses simulated example data.

Original output: Absolute changes drawn as four positive bars, hiding their original directions.
Open full-size image
Original output. Absolute changes drawn as four positive bars, hiding their original directions.
Result output: Conditions A and C now extend left by four and one units; B and D extend right by two and five. The zero line makes direction readable without relying on color.
Open full-size image
Result output. Conditions A and C now extend left by four and one units; B and D extend right by two and five. The zero line makes direction readable without relying on color.

What changes in the actual output?

Conditions A and C now extend left by four and one units; B and D extend right by two and five. The zero line makes direction readable without relying on color.

conditionsigned_change
A-4
B2
C-1
D5

The table shows all input rows. (4)

What should I check before using this setting?

State the baseline and units of change in the caption, and draw lengths from the signed changes. Show absolute values separately when that is the comparison needed.

How can I reproduce the figures?

Run python example.py in a writable folder. Tested with Python 3.12.4, Matplotlib 3.8.4 and NumPy 1.26.4. The script writes data.csv, before.png, after.png and after.pdf beside itself.

These are the code, data and result files used in this article. Choose a file below to run the example or compare your results.

  • example.pyComplete example code to run on your computer.
  • data.csvThe input data used in the example; open it as a table.
  • before.pngThe figure before the change, for comparison.
  • after.pngThe figure after applying the settings in this article.
  • after.pdfThe result in PDF format, for viewing and page layout.
  • Usage license · LICENSE.txtTerms for reusing the code, data and figures, with source and font notices.
File verification
  • hashes.jsonChecksums for checking whether the downloaded files are complete.

Complete Python code

# Original Bopbun code/data: CC0-1.0. Font rights remain separate; see LICENSE.txt.
# Tested: Python 3.12.4, Matplotlib 3.8.4, NumPy 1.26.4.
from pathlib import Path
import csv
import json
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib import colors, ticker, dates
OUT=Path(__file__).resolve().parent
plt.rcdefaults()
plt.rcParams.update({'font.family':'DejaVu Sans','font.size':11,'figure.dpi':120,
                     'savefig.dpi':160,'pdf.fonttype':42})
def data_file(header,rows):
    with (OUT/'data.csv').open('w',newline='',encoding='utf-8') as f:
        w=csv.writer(f);w.writerow(header);w.writerows(rows)
def canvas(**kw):
    return plt.subplots(figsize=(7.2,4.5),layout='constrained',**kw)

def make(fixed):
    labels=['A','B','C','D'];delta=np.array([-4,2,-1,5]);data_file(['condition','signed_change'],zip(labels,delta));fig,ax=canvas()
    ax.barh(labels,delta if fixed else abs(delta),color=np.where(delta<0,'#b84632','#007c83') if fixed else '#007c83')
    if fixed:ax.axvline(0,color='black')
    ax.set(xlabel='Signed change (a.u.)' if fixed else 'Absolute change (a.u.)',ylabel='Condition',title='Retain direction around a zero baseline');return fig

if __name__=='__main__':
    before=make(False);after=make(True)
    before.savefig(OUT/'before.png');after.savefig(OUT/'after.png')
    after.savefig(OUT/'after.pdf',metadata={'CreationDate':None,'ModDate':None})
    after.canvas.draw()
    print(json.dumps({'axes':len(after.axes),'limits':[{'x':list(a.get_xlim()),'y':list(a.get_ylim())} for a in after.axes]}))
    plt.close('all')

Which sources and licenses apply?

Original code and data: CC0-1.0. Figures use DejaVu Sans; its font license remains separate. Install Matplotlib and NumPy locally to run the code.

Which related answer do I need?

Research figure questions