The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Use matplotlib.pyplot.bar() with an explicit bottom for each series. For mixed positive and negative values, track two running totals per category: positives stack upward from zero, while negatives stack downward. A single running total will put some segments on the wrong side of the baseline.
Build the chart with separate positive and negative totals
This example stacks each value on the same side of zero as its sign. The bottom array selects the starting point for each category; the totals are then updated separately for the next series.
import matplotlib.pyplot as plt
import numpy as np
labels = ["Jan", "Feb", "Mar", "Apr"]
data = {
"Series A": np.array([12, -5, 8, -3]),
"Series B": np.array([4, -7, -2, 6]),
"Series C": np.array([-3, 2, 5, -4]),
}
fig, ax = plt.subplots()
pos_bottom = np.zeros(len(labels))
neg_bottom = np.zeros(len(labels))
for name, values in data.items():
bottom = np.where(values >= 0, pos_bottom, neg_bottom)
ax.bar(labels, values, bottom=bottom, label=name)
pos_bottom += np.clip(values, 0, None)
neg_bottom += np.clip(values, None, 0)
ax.axhline(0, color="black", linewidth=0.8)
ax.set_ylabel("Value")
ax.legend()
plt.show()
The loop applies the logic element by element, so one series can be positive in one category and negative in another. np.clip(values, 0, None) contributes only positive values to the upward total; np.clip(values, None, 0) contributes only negative values to the downward total.
Why bottom needs an explicit baseline
bottom tells Matplotlib where each vertical bar segment begins. It is not automatically accumulated across separate calls to bar(), so pass the appropriate baseline for every category and series. The official stacked-bar example illustrates cumulative bottoms for conventional positive stacks; the two-total approach extends that baseline behavior to mixed signs. The [Matplotlib 3.11.0 bar API reference](https://matplotlib.org/3.11.0/api/_as_gen/matplotlib.pyplot.bar.html) documents the bottom parameter.
#1 Best Overall
Avoid common stacking errors
- Do not use only the immediately previous series as the baseline. A stack needs the cumulative total of all earlier values on that same side of zero.
- Do not use one sign-blind running sum. It can place a later segment on the wrong side of zero or overlap the other stack.
- Do not change negative values to absolute values unless the chart is meant to show magnitudes rather than signed contributions; taking absolute values changes what the data means.
Make sure a stacked chart fits the comparison
A diverging stack is useful when the goal is to show signed contributions and how they accumulate above and below zero. Add a zero reference line, clear series labels, and units that identify what the values measure. If the priority is precise comparison of each individual series across categories, grouped bars may be easier to read: segments that do not start at zero are harder to compare directly.
This example uses vertical bar(). For horizontal bars, barh() has an analogous baseline parameter named left; check its API documentation before adapting the code.
Quick Recap
Best Value
Rank #2
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




