Adjust the width of box in boxplot in Python Matplotlib

MatplotlibPythonData Visualization

To adjust the width of box in boxplot in Python matplotlib, we can use width in the boxplot() method.

Steps

  • Set the figure size and adjust the padding between and around the subplots.
  • Make a Pandas dataframe, i.e., two-dimensional, size-mutable, potentially heterogeneous tabular data.
  • Make a box and whisker plot, using boxplot() method with width tuple to adjust the box in boxplot.
  • To display the figure, use show() method.

Example

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

data = pd.DataFrame({"Box1": np.random.rand(10), "Box2": np.random.rand(10)})
ax = plt.boxplot(data, widths=(0.25, 0.5))

plt.show()

Output

raja
Published on 03-Aug-2021 12:14:07
Advertisements