matplotlib

Stacked bar plot in matplotlib

Create a stacked bar plot in matplotlib based on your data. You can plot only one stacked bar plot in one figure. You can select color, width, alignment and transparency (alpha).

Please check Advanced options to change plot style, set title, axis labels and control legend visibility and position.

matplotlibbarstacked

Required packages

You need below packages to use the code generated by recipe. All packages are automatically installed in MLJAR Studio.

matplotlib>=3.8.4

Interactive recipe

You can use below interactive recipe to generate code. This recipe is available in MLJAR Studio.

In the below recipe, we assume that you have following variables available in your notebook:

  • X (type DataFrame)

Python code

# Python code will be here

Code explanation

  1. Please select DataFrame.
  2. Please select columns for x axis.
  3. You can customize color, transparency(alpha), width and alignment of every bar at once.
  4. Please check Advanced options.
  5. You can set title and axis labels there.
  6. If you would like to control position of legend in your plot, you can do this in Advanced options. The default value is best, which means that matplotlib will search for position that do not overlay chart points.
  7. You can apply different style for stacked bar figure.

Matplotlib cookbook

Code recipes from Matplotlib cookbook.

« Previous
Bar plot