Creating multiple boxplots on the same graph from a dictionary, using Matplotlib

PythonServer Side ProgrammingProgramming

To create multiple boxplots on the same graph from a dictionary, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Make a dictionary, dict, with two columns.
  • Create a figure and a set of subplots.
  • Make a box and whisker plot
  • Set the xtick labels using set_xticklabels() method
  • To display the figure, use show() method.

Example

from matplotlib import pyplot as plt

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

data = {'col1': [3, 5, 2, 9, 1], 'col2': [2, 6, 1, 3, 4]}
fig, ax = plt.subplots()

ax.boxplot(data.values())
ax.set_xticklabels(data.keys())

plt.show()

Output

raja
Published on 08-Jul-2021 11:12:01
Advertisements