Skip to content

Commit c3f715e

Browse files
authored
Update matplotlib-box-plots.md
1 parent c54f612 commit c3f715e

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

contrib/plotting-visualization/matplotlib-box-plots.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -40,60 +40,97 @@ Syntax - matplotlib.pyplot.boxplot(data,notch=none,vert=none,patch_artist,widths
4040
## Implementation of Box Plot in Python
4141

4242
### Import libraries
43+
4344
import matplotlib.pyplot as plt
45+
4446
import numpy as np
4547

4648
### Creating dataset
49+
4750
np.random.seed(10)
51+
4852
data = np.random.normal(100, 20, 200)
53+
4954
fig = plt.figure(figsize =(10, 7))
5055

5156
### Creating plot
57+
5258
plt.boxplot(data)
5359

5460
### show plot
61+
5562
plt.show()
5663

5764
### Implementation of Multiple Box Plot in Python
65+
5866
import matplotlib.pyplot as plt
67+
5968
import numpy as np
69+
6070
np.random.seed(10)
71+
6172
dataSet1 = np.random.normal(100, 10, 220)
62-
dataSet2 = np.random.normal(80, 20, 200)
73+
74+
dataSet2 = np.random.normal(80, 20, 200)
75+
6376
dataSet3 = np.random.normal(60, 35, 220)
77+
6478
dataSet4 = np.random.normal(50, 40, 200)
79+
6580
dataSet = [dataSet1, dataSet2, dataSet3, dataSet4]
81+
6682
figure = plt.figure(figsize =(10, 7))
83+
6784
ax = figure.add_axes([0, 0, 1, 1])
85+
6886
bp = ax.boxplot(dataSet)
87+
6988
plt.show()
7089

7190
### Implementation of Box Plot with Outliers (visual representation of the sales distribution for each product, and the outliers highlight months with exceptionally high or low sales)
91+
7292
import matplotlib.pyplot as plt
93+
7394
import numpy as np
7495

7596
### Data for monthly sales
97+
7698
product_A_sales = [100, 110, 95, 105, 115, 90, 120, 130, 80, 125, 150, 200]
99+
77100
product_B_sales = [90, 105, 100, 98, 102, 105, 110, 95, 112, 88, 115, 250]
101+
78102
product_C_sales = [80, 85, 90, 78, 82, 85, 88, 92, 75, 85, 200, 95]
79103

80104
### Introducing outliers
105+
81106
product_A_sales.extend([300, 80])
107+
82108
product_B_sales.extend([50, 300])
109+
83110
product_C_sales.extend([70, 250])
84111

85112
### Creating a box plot with outliers
113+
86114
plt.boxplot([product_A_sales, product_B_sales, product_C_sales], sym='o')
115+
87116
plt.title('Monthly Sales Performance by Product with Outliers')
117+
88118
plt.xlabel('Products')
119+
89120
plt.ylabel('Sales')
121+
90122
plt.show()
91123

92124
### Implementation of Grouped Box Plot (to compare the exam scores of students from three different classes (A, B, and C))
125+
93126
import matplotlib.pyplot as plt
127+
94128
import numpy as np
129+
95130
class_A_scores = [75, 80, 85, 90, 95]
131+
96132
class_B_scores = [70, 75, 80, 85, 90]
133+
97134
class_C_scores = [65, 70, 75, 80, 85]
98135

99136
### Creating a grouped box plot

0 commit comments

Comments
 (0)