PLOTTING WITH PYPLOT II – HISTOGRAM,
FREQUENCY DISTRIBUTION, BOXPLOTS
Chapter – 4 (PART -3)
Class – XII
Creating Box Plots:
The box plot has become the standard technique for
presenting the 5-number summary which consists of:
1. The minimum range value
2. The maximum range value
3. The upper quartile
4. The lower quartile
5. The median
A box plot is used to
show the range and middle half of ranked data. Ranked data is numerical data
such as numbers etc. The middle half of the data is represented by the box. The
highest and lowest scores are joined to the box by straight lines. The regions
above the upper quartile and below the lower quartile each contain 25% of the
data.
The five-number summary is shown in the diagram below.
Creating Boxplots with boxplot() of
Pyplot:
The syntax of boxplot is:
matplotlib.pyplot.boxplot(x,
notch = None, vert = None, meanline = None, showmeans = None,
showbox = None)
Parameters:
x |
Array or a sequence of vectors. The input data. |
notch |
bool, optional (False); If True, will produce a notched box plot.
Otherwise, a rectangular boxplot is produced. |
vert |
bool, optional (True); If True (default), makes the boxes vertical. If
False, everything is drawn horizontally. |
meanline |
bool, optional (False); If True (and showmeans is True), will try to
render the mean as a line spanning the full width of the box. |
showbox |
bool, optional (True); Show the central box. |
showmeans |
bool, optional (False); Show the arithmetic means. |
1. Draw the plain boxplot |
|
import numpy as np import matplotlib.pyplot as plt x = [5, 10, 15, 20, 30, 40, 65, 75, 90, 120, 150,
200, 250] plt.boxplot(x) plt.show() |
|
2. Draw the boxplot with mean shown |
|
import numpy as np import matplotlib.pyplot as plt x = [5, 10, 15, 20, 30, 40, 65, 75, 90, 120, 150,
200, 250] plt.boxplot(x, showmeans = True) plt.show()
|
|
3. Draw a notched boxplot |
|
import numpy as np import matplotlib.pyplot as plt x = [5, 10, 15, 20, 30, 40, 65, 75, 90, 120, 150,
200, 250] plt.boxplot(x, showmeans = True, notch = True) plt.show()
|
|
4.Draw the boxplot without the central box |
|
import numpy as np import matplotlib.pyplot as plt x = [5, 10, 15, 20, 30, 40, 65, 75, 90, 120, 150,
200, 250] plt.boxplot(x, showbox = False) plt.show()
|
|
Customizing/Adding Details to the Plots:
Use <matplotlib.pyplot>.title() to add title for
your plot.
Use <matplotlib.pyplot>.xticks()/yticks() for
setting xticks and yticks.
Use <matplotlib.pyplot>.xlim()/ylim() for
setting xlimit and ylimit.
Use <matplotlib.pyplot>.xlabel()/ylabel() for
setting x-axis label and y-axis label.
Use <matplotlib.pyplot>.legend() to add legends
to our plot.
Question:Create a
boxplot from the following set of data and (i) change the orientation to
horizontal, (ii) Add title as ‘Horizontal Boxplot’, (iii) Y-axis title as
‘Value Range x = [44, 54, 65, 85, 110, 56, 97, 74, 96, 54, 64, 76, 94, 82, 73, 76] |
|
Solution: import numpy as np import matplotlib.pyplot as plt x = [44, 54, 65, 85, 110, 56, 97, 74, 96, 54, 64,
76, 94, 82, 73, 76] plt.boxplot(x, vert = False, showmeans = True) plt.title("Horizontal Boxplot") plt.xlabel("Value Range") plt.show()
|
|
No comments:
Post a Comment
Please do not any spam in the comment box.