Professional Documents
Culture Documents
summary of the set of data values having properties like minimum, first
quartile, median, third quartile and maximum. In the box plot, a box is
created from the first quartile to the third quartile, a vertical line is also
there which goes through the box at the median. Here x-axis denotes the
data to be plotted while the y-axis shows the frequency distribution.
Parameters:
Attribute Value
positions optional parameter accepts array and sets the position of boxes
Attribute Value
widths optional parameter accepts array and sets the width of boxes
The data values given to the ax.boxplot() method can be a Numpy array or
Python list or Tuple of arrays. Let us create the box plot by using
numpy.random.normal() to create some random data, it takes mean,
standard deviation, and the desired number of values as arguments.
Example:
• Python3
# Import libraries
import numpy as np
# Creating dataset
np.random.seed(10)
plt.boxplot(data)
# show plot
plt.show()
Output:
• Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
np.random.seed(10)
ax = fig.add_axes([0, 0, 1, 1])
# Creating plot
bp = ax.boxplot(data)
# show plot
plt.show()
Output:
Example 2: Let’s try to modify the above plot with some of the
customizations:
• Python3
# Import libraries
import numpy as np
# Creating dataset
np.random.seed(10)
'#FFFF00', '#FF00FF']
patch.set_facecolor(color)
# whiskers
whisker.set(color ='#8B008B',
linewidth = 1.5,
linestyle =":")
# caps
cap.set(color ='#8B008B',
linewidth = 2)
# medians
median.set(color ='red',
linewidth = 3)
# changing style of fliers
flier.set(marker ='D',
color ='#e7298a',
alpha = 0.5)
# x-axis labels
ax.set_yticklabels(['data_1', 'data_2',
'data_3', 'data_4'])
# Adding title
# ticks
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
# show plot
plt.show()
Output: