Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Adjust Width of Box in Boxplot using Python Matplotlib



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

Updated on: 2021-08-03T12:14:33+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements