FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vcs/_4.python/matplotlib/matplotlib5_hist.py at master · bunshue/vcs · GitHub
bunshue
/
vcs
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
3
Code
Issues
0
Pull requests
4
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
vcs
/
_4.python
/
matplotlib
/
matplotlib5_hist.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
886 lines (666 loc) · 23.1 KB
Breadcrumbs
vcs
/
_4.python
/
matplotlib
/
matplotlib5_hist.py
Copy path
File metadata and controls
886 lines (666 loc) · 23.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
"""
# hist 集合
np.random.normal
(1)
mu, sigma = 100, 15 # 平均值, 標準差
x = np.random.normal(mu, sigma, size=N) # 隨機數
(2)
若mu, sigma, 則是 平均值為 0,標準差為 1 的常態分配
生成 N 組標準常態分配(平均值為 0,標準差為 1 的常態分配)隨機變數
x = np.random.normal(size=N)
np.random.randn
(1)
mu, sigma = 100, 15 # 平均值, 標準差
x = mu + sigma * np.random.randn(N) # 隨機數
np.random.rand(
x = np.random.rand(N, 3) # 產生共3組,每組 N 個隨機數
np.random.uniform(size=N)
# 生成 N 組介於 0 與 1 之間均勻分配隨機變數
x = np.random.uniform(size=N)
# 給定範圍
x = np.random.uniform(0.0, 5.0, size=N) # 隨機數 #另外範圍
random.randint
"""
print
(
"------------------------------------------------------------"
)
# 60個
N
=
500
# 資料個數
num_bins
=
50
# 直方圖顯示時的束數
print
(
"------------------------------------------------------------"
)
# 60個
# 共同
import
os
import
sys
import
time
import
math
import
random
import
numpy
as
np
import
pandas
as
pd
import
matplotlib
.
pyplot
as
plt
font_filename
=
"D:/_git/vcs/_1.data/______test_files1/_font/msch.ttf"
# 設定中文字型及負號正確顯示
# 設定中文字型檔
plt
.
rcParams
[
"font.sans-serif"
]
=
"Microsoft JhengHei"
# 將字體換成 Microsoft JhengHei
# 設定負號
plt
.
rcParams
[
"axes.unicode_minus"
]
=
False
# 讓負號可正常顯示
plt
.
rcParams
[
"font.size"
]
=
12
# 設定字型大小
def
show
():
plt
.
show
()
pass
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
plt
.
figure
(
figsize
=
(
12
,
8
))
mu
,
sigma
=
100
,
15
# 平均值, 標準差
x
=
np
.
random
.
normal
(
mu
,
sigma
,
size
=
N
*
10
)
# 隨機數
print
(
"平均數:"
,
np
.
mean
(
x
))
print
(
"標準差:"
,
np
.
std
(
x
))
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
231
)
mu
,
sigma
=
100
,
15
x
=
np
.
linspace
(
mu
-
50
,
mu
+
50
,
3000
)
# 從N1到N2, 分成N個, 包含頭尾
y
=
(
1
/
(
sigma
*
np
.
sqrt
(
2
*
np
.
pi
)))
*
np
.
exp
(
-
((
x
-
mu
)
**
2
)
/
(
2
*
sigma
**
2
))
plt
.
plot
(
x
,
y
,
"--"
,
color
=
"r"
,
linewidth
=
2
)
plt
.
title
(
"使用plot"
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
232
)
N
=
10000
# 資料個數
num_bins
=
50
# 直方圖顯示時的束數
mu
,
sigma
=
100
,
15
# 平均值, 標準差
x
=
mu
+
sigma
*
np
.
random
.
randn
(
N
)
# 隨機數
n
,
bins
,
patches
=
plt
.
hist
(
x
,
bins
=
num_bins
,
density
=
True
,
color
=
"green"
,
rwidth
=
0.5
,
alpha
=
0.5
)
# 繪製曲線圖
x
=
bins
y
=
(
1
/
(
sigma
*
np
.
sqrt
(
2
*
np
.
pi
)))
*
np
.
exp
(
-
((
bins
-
mu
)
**
2
)
/
(
2
*
sigma
**
2
))
plt
.
plot
(
x
,
y
,
"--"
,
color
=
"r"
,
linewidth
=
2
)
plt
.
title
(
"使用plot + hist"
)
plt
.
ylabel
(
"機率"
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
233
)
N
=
10000
# 樣本數
x
=
np
.
random
.
randn
(
N
)
# 隨機數, 預設 平均值 = 0.0, 標準差 = 1
mu
=
100
# 均值
sigma
=
15
# 標準差
x
=
np
.
random
.
normal
(
mu
,
sigma
,
N
)
# 隨機數
print
(
"平均數:"
,
np
.
mean
(
x
))
print
(
"標準差:"
,
np
.
std
(
x
))
bins
=
50
# 束
plt
.
hist
(
x
,
bins
,
density
=
True
)
# 直方圖
plt
.
ylabel
(
"機率"
)
plt
.
title
(
"mu=100, sigma=15之常態分佈"
)
plt
.
text
(
120
,
0.02
,
r"$\mu=100,\ \sigma=15$"
)
# plt.grid(True)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
234
)
N1
=
10000
# 樣本數
mu1
=
50
# 均值
sigma1
=
5
# 標準差
N2
=
10000
# 樣本數
mu2
=
60
# 均值
sigma2
=
5
# 標準差
x1
=
np
.
random
.
normal
(
mu1
,
sigma1
,
N1
)
x2
=
np
.
random
.
normal
(
mu2
,
sigma2
,
N2
)
plt
.
hist
(
x1
,
range
=
(
30
,
80
),
bins
=
20
,
color
=
"r"
,
alpha
=
0.8
)
plt
.
hist
(
x2
,
range
=
(
30
,
80
),
bins
=
20
,
color
=
"g"
,
alpha
=
0.8
)
"""
#用density
plt.hist(x1, range=(30, 80), bins=20, color="r", alpha=0.8, density=True)
plt.hist(x2, range=(30, 80), bins=20, color="g", alpha=0.8, density=True)
"""
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
235
)
# 產生常態分佈的數據:平均數0, 標準差1, 1000個資料
x
=
np
.
random
.
normal
(
0
,
1
,
N
)
print
(
"平均數:"
,
np
.
mean
(
x
))
print
(
"標準差:"
,
np
.
std
(
x
))
bins
=
50
# 束
plt
.
hist
(
x
,
bins
=
bins
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
236
)
mu
=
0
sigma
=
1
variance
=
np
.
square
(
sigma
)
x
=
np
.
arange
(
-
5
,
5
,
0.01
)
y
=
np
.
exp
(
-
np
.
square
(
x
-
mu
)
/
2
*
variance
)
/
(
np
.
sqrt
(
2
*
np
.
pi
*
variance
))
plt
.
plot
(
x
,
y
)
show
()
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
plt
.
figure
(
figsize
=
(
12
,
8
))
plt
.
subplot
(
231
)
N
=
10000
# 樣本數
mu
=
0
# 平均值
sigma
=
1
# 標準差
x
=
np
.
random
.
randn
(
N
)
# 隨機數
bins
=
50
# 束
count
,
bins
,
ignored
=
plt
.
hist
(
x
,
bins
,
density
=
True
)
# 直方圖
# 繪製折線圖
plt
.
plot
(
bins
,
1
/
(
sigma
*
np
.
sqrt
(
2
*
np
.
pi
))
*
np
.
exp
(
-
((
bins
-
mu
)
**
2
)
/
(
2
*
sigma
**
2
)),
linewidth
=
2
,
color
=
"r"
,
)
plt
.
title
(
"常態分布 "
+
r"$\mu=0, \sigma=1$"
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
232
)
x3
=
np
.
random
.
randn
(
N
,
3
)
colors
=
[
"red"
,
"green"
,
"blue"
]
bins
=
50
# 直方圖顯示時的束數
plt
.
hist
(
x3
,
bins
,
density
=
True
,
color
=
colors
,
label
=
colors
)
plt
.
legend
()
plt
.
title
(
"3 組數據的常態分佈隨機數"
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
233
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
234
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
235
)
x
=
np
.
random
.
exponential
(
scale
=
2
,
size
=
N
)
plt
.
hist
(
x
,
bins
=
num_bins
,
label
=
"Exponential distribution"
)
plt
.
title
(
"np.random.exponential"
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
236
)
# 生成 N 組介於 0 與 1 之間均勻分配隨機變數
x
=
np
.
random
.
uniform
(
size
=
N
)
# x = np.random.uniform(0.0, 5.0, size=N) # 隨機數 #另外範圍
plt
.
hist
(
x
,
bins
=
num_bins
,
rwidth
=
0.8
)
plt
.
title
(
"np.random.uniform"
)
show
()
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
plt
.
figure
(
figsize
=
(
12
,
8
))
plt
.
suptitle
(
"皆為 np.random.normal
\t
"
+
r"$\mu = 100, \sigma=15$"
)
mu
,
sigma
=
100
,
15
# 平均值, 標準差
x
=
np
.
random
.
normal
(
mu
,
sigma
,
size
=
N
*
10
)
# 隨機數
print
(
"平均數:"
,
np
.
mean
(
x
))
print
(
"標準差:"
,
np
.
std
(
x
))
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
231
)
plt
.
hist
(
x
,
bins
=
num_bins
,
histtype
=
"bar"
,
facecolor
=
"r"
,
edgecolor
=
"g"
,
alpha
=
0.75
,
rwidth
=
0.8
,
label
=
"Normal distribution"
,
)
plt
.
title
(
"histtype 1 : bar"
)
plt
.
text
(
50
,
250
,
r"$\mu=100,\ \sigma=15$"
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
232
)
plt
.
hist
(
x
,
bins
=
num_bins
,
histtype
=
"barstacked"
,
facecolor
=
"g"
,
edgecolor
=
"b"
,
alpha
=
0.75
,
rwidth
=
0.8
,
label
=
"Normal distribution"
,
)
plt
.
title
(
"histtype 2 : barstacked"
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
233
)
plt
.
hist
(
x
,
bins
=
num_bins
,
histtype
=
"step"
,
facecolor
=
"c"
,
edgecolor
=
"r"
,
alpha
=
0.75
,
rwidth
=
0.8
,
label
=
"Normal distribution"
,
)
plt
.
title
(
"histtype 3 : step + 多筆資料"
)
mu
,
sigma
=
80
,
10
x2
=
np
.
random
.
normal
(
mu
,
sigma
,
size
=
N
*
10
)
# 隨機數
mu
,
sigma
=
120
,
10
x3
=
np
.
random
.
normal
(
mu
,
sigma
,
size
=
N
*
10
)
# 隨機數
plt
.
hist
(
x2
,
bins
=
num_bins
,
histtype
=
"step"
,
facecolor
=
"m"
,
edgecolor
=
"g"
,
alpha
=
0.75
,
rwidth
=
0.8
,
label
=
"Normal distribution"
,
)
plt
.
hist
(
x3
,
bins
=
num_bins
,
histtype
=
"step"
,
facecolor
=
"y"
,
edgecolor
=
"b"
,
alpha
=
0.75
,
rwidth
=
0.8
,
label
=
"Normal distribution"
,
)
# 合寫, 但效果不對
# plt.hist([x2, x3], bins=num_bins, histtype="step", facecolor="y", edgecolor="b", alpha=0.75, rwidth=0.8, label="Normal distribution")
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
234
)
# 縱軸不做正規化處理為數量,直條的間距填滿
plt
.
hist
(
x
,
bins
=
num_bins
,
histtype
=
"stepfilled"
,
facecolor
=
"c"
,
edgecolor
=
"m"
,
alpha
=
0.75
,
rwidth
=
0.8
,
label
=
"Normal distribution"
,
)
plt
.
title
(
"histtype 4 : stepfilled"
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
235
)
kwargs
=
dict
(
histtype
=
"stepfilled"
,
alpha
=
0.3
,
bins
=
num_bins
//
2
,
rwidth
=
0.8
)
plt
.
hist
(
x
,
**
kwargs
)
plt
.
title
(
"以字典傳送參數"
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
236
)
# 縱軸執行正規化處理表示為機率
# 指定bins
bins
=
[
50
,
70
,
85
,
95
,
100
,
105
,
115
,
130
,
150
]
# bins = range(0, 151, 10) #設定bin的範圍
plt
.
hist
(
x
,
bins
,
rwidth
=
0.8
)
plt
.
title
(
"unequal bins"
)
show
()
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
plt
.
figure
(
figsize
=
(
12
,
8
))
# 資料個數
N
=
10000
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
231
)
sides
=
6
# 骰子有幾面
dice
=
[]
# 建立擲骰子的串列
# 產生擲骰子的串列
for
i
in
range
(
N
):
ranNum
=
random
.
randint
(
1
,
sides
)
# 產生1-6隨機數
dice
.
append
(
ranNum
)
# 建立 N 個 1-6(含) 的整數隨機數 same
# dice = np.random.randint(1, sides + 1, size=N) # 建立隨機數
plt
.
hist
(
dice
,
bins
=
6
,
rwidth
=
0.5
,
cumulative
=
False
,
alpha
=
0.3
)
# 繪製hist圖
plt
.
ylabel
(
"次數"
)
plt
.
title
(
"測試 10000 次 不累積統計"
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
232
)
print
(
"資料同上, 改了 累積統計"
)
# plt.hist(dice, bins=6, rwidth=0.5, cumulative=True, alpha=0.3) # 繪製hist圖
plt
.
hist
(
dice
,
bins
=
6
,
rwidth
=
0.5
,
cumulative
=
True
,
alpha
=
0.3
)
# 繪製hist圖
plt
.
ylabel
(
"次數"
)
plt
.
title
(
"測試 10000 次, 累積統計"
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
233
)
d1
=
np
.
random
.
randint
(
1
,
6
+
1
,
N
)
# 不含尾
d2
=
np
.
random
.
randint
(
1
,
6
+
1
,
N
)
dsums
=
d1
+
d2
plt
.
hist
(
dsums
,
bins
=
11
,
rwidth
=
0.5
)
plt
.
xlabel
(
"兩個骰子和"
)
plt
.
ylabel
(
"次數"
)
plt
.
title
(
"擲兩個骰子 10000 次 看其分布"
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
234
)
sc1
=
[
90
,
72
,
45
,
18
,
13
,
81
,
65
,
68
,
73
,
84
,
75
,
79
,
58
,
78
,
96
,
100
,
98
,
64
,
43
,
2
,
63
,
71
,
27
,
35
,
45
,
65
,
]
sc2
=
[
90
,
72
,
45
,
18
,
13
,
81
,
65
,
68
,
73
,
84
,
75
,
79
,
58
,
78
,
96
,
100
,
98
,
64
,
43
,
2
,
63
,
71
,
27
,
35
,
45
,
65
,
]
# 指定束範圍
# plt.hist([sc1, sc2], 9)
plt
.
hist
(
[
sc1
,
sc2
],
bins
=
[
0
,
10
,
20
,
30
,
40
,
50
,
60
,
70
,
80
,
90
,
100
],
edgecolor
=
"r"
,
rwidth
=
0.5
,
)
plt
.
ylabel
(
"學生人數"
)
plt
.
xlabel
(
"分數"
)
plt
.
title
(
"成績表"
)
"""
N = 100
math = np.random.randint(10, 100, N) # 不含尾
chem = np.random.randint(10, 100, N) # 不含尾
bins = 9
labels = ["數學", "化學"]
plt.hist([math, chem], bins, label=labels)
plt.ylabel("學生人數")
plt.xlabel("分數")
plt.title("成績表")
plt.legend()
"""
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
235
)
x
=
np
.
random
.
rand
(
N
,
3
)
# 產生共3組,每組 N 個隨機數
plt
.
hist
(
x
,
bins
=
num_bins
//
10
)
plt
.
title
(
"產生共3組,每組 10000 個隨機數"
)
print
(
"------------------------------"
)
# 30個
plt
.
subplot
(
236
)
show
()
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"hist參數大合集"
)
plt
.
figure
(
figsize
=
(
12
,
8
))
mu
,
sigma
=
100
,
15
# 平均值, 標準差
x
=
np
.
random
.
normal
(
mu
,
sigma
,
size
=
N
*
10
)
# 隨機數
print
(
"平均數:"
,
np
.
mean
(
x
))
print
(
"標準差:"
,
np
.
std
(
x
))
print
(
"x : 需要製作直方圖的一維數組"
)
print
(
"bins : 直方圖的柱數,即要分的組數,默認為10"
)
print
(
"histtype : 直方圖類型,『bar』, 『barstacked』, 『step』, 『stepfilled』"
)
print
(
"histtype : bar 傳統的條形直方圖"
)
print
(
"histtype : barstacked 堆疊的條形直方圖"
)
print
(
"histtype : step 未填充的條形直方圖,只有外邊框"
)
print
(
"histtype : stepfilled 有填充的直方圖"
)
print
(
"color:顏色串列, color=['r','g','b','c','m'], 若有多組數據 依序顯示顏色"
)
print
(
"facecolor : 直方圖顏色"
)
print
(
"edgecolor : 直方圖邊框顏色"
)
print
(
"alpha : 透明度"
)
print
(
"rwidth : 柱子的寬度占bins寬的比例 0~1"
)
print
(
"orientation : 直方圖方向 'vertical'(垂直, 預設), 'horizontal'(水平)"
)
print
(
"density : 密度, 是否將得到的直方圖向量歸一化"
)
print
(
"density : 密度, 如果為true,則返回的元組的第一個參數n將為頻率而非默認的頻數"
)
print
(
"density : False : 頻數, True : 頻率"
)
print
(
"cumulative : 如果為True,則計算累計頻數;如果normed或density取值為True,則計算累計頻率"
)
print
(
"align : 此參數是可選參數,它控制如何繪制直方圖。 {‘left’,‘mid’,‘right’}"
)
print
(
"align : left 柱子的中心位於bins的左邊緣"
)
print
(
"align : mid 柱子位於bins左右邊緣之間"
)
print
(
"align : right 柱子的中心位於bins的右邊緣"
)
"""
#尚未使用過的參數 尚無使用範例
bottom : 數組,標量值或None;每個柱子底部相對於y=0的位置。如果是標量值,則每個柱子相對於y=0向上/向下的偏移量相同。如果是數組,則根據數組元素取值移動對應的柱子;即直方圖上下便宜距離;
bottom : 此參數是每個容器底部基線的位置。
range:元組(tuple)或None;剔除較大和較小的離群值,給出全局範圍;如果為None,則默認為(x.min(), x.max());即x軸的範圍;
range:此參數是可選參數,它是箱子的上下限。
weights : 與x形狀相同的權重數組;將x中的每個元素乘以對應權重值再計數;如果normed或density取值為True,則會對權重進行歸一化處理。這個參數可用於繪制已合并的數據的直方圖;
weights : 此參數是可選參數,并且是一個權重數組,與x的形狀相同。
log:此參數是可選參數,用於將直方圖軸設置為對數刻度
log:布爾值。如果取值為True,則坐標軸的刻度為對數刻度;
如果log為True且x是一維數組,則計數為0的取值將被剔除,
僅返回非空的(frequency, bins, patches);
stacked:布爾值。如果取值為True,則輸出的圖為多個數據集堆疊累計的結果;
如果取值為False且histtype=‘bar’或’step’,則多個數據集的柱子并排排列;
返回值(用參數接收返回值,便於設置數據標簽):
n:直方圖向量,即每個分組下的統計值,是否歸一化由參數normed設定。
當normed取默認值時,n即為直方圖各組內元素的數量(各組頻數);
bins: 返回各個bin的區間範圍;
patches:返回每個bin里面包含的數據,是一個list。
"""
n
,
bins
,
patches
=
plt
.
hist
(
x
,
bins
=
10
,
# bins = 'auto'
# bins = range(10, 101, 10), # 設定bin的範圍
histtype
=
"bar"
,
align
=
"right"
,
# color=['red','green','blue','cyan','magenta'], 若有多組數據 依序顯示顏色
facecolor
=
"red"
,
edgecolor
=
"green"
,
alpha
=
0.75
,
rwidth
=
0.9
,
orientation
=
"vertical"
,
cumulative
=
False
,
density
=
False
,
label
=
"常態分佈"
,
# 以便在圖例中顯示
)
print
(
"標示高度"
)
for
i
in
range
(
len
(
n
)):
print
(
"x = "
,
bins
[
i
]
+
10
,
", y = "
,
n
[
i
],
", text ="
,
int
(
n
[
i
]))
plt
.
text
(
bins
[
i
]
+
10
,
n
[
i
],
int
(
n
[
i
]),
ha
=
"center"
,
va
=
"bottom"
)
print
(
"返回值"
)
print
(
"每一柱的高度(y軸) : "
,
n
)
print
(
"每一柱的起訖(x軸) : "
,
bins
)
print
(
patches
)
for
p
in
patches
:
print
(
type
(
p
))
print
(
p
)
total_n
=
sum
(
n
)
print
(
"總樣本數 : "
,
total_n
)
print
(
"總柱數 : "
,
len
(
n
))
xx
=
[]
for
i
in
range
(
len
(
bins
)
-
1
):
xx
.
append
((
bins
[
i
]
+
bins
[
i
+
1
])
/
2
)
yy
=
n
plt
.
plot
(
xx
,
yy
,
"--"
,
color
=
"r"
,
linewidth
=
2
)
plt
.
title
(
"hist參數大合集"
)
plt
.
xlabel
(
""
)
plt
.
ylabel
(
"個數統計"
)
show
()
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"建立N筆成績資料 常態分佈 平均值 = 70, 標準差 = 15"
)
N
=
50000
# 資料個數
num_bins
=
100
# 直方圖顯示時的束數
mu
,
sigma
=
70
,
15
# 平均值, 標準差
plt
.
figure
(
figsize
=
(
12
,
8
))
# 理想值
x
=
np
.
linspace
(
mu
-
50
,
mu
+
50
,
N
)
# 從N1到N2, 分成N個, 包含頭尾
y
=
(
1
/
(
sigma
*
np
.
sqrt
(
2
*
np
.
pi
)))
*
np
.
exp
(
-
((
x
-
mu
)
**
2
)
/
(
2
*
sigma
**
2
))
*
N
plt
.
plot
(
x
,
y
,
"--"
,
color
=
"lime"
,
linewidth
=
3
)
print
(
"建立常態分佈資料 平均值= 75, 標準差 = 15,"
,
N
,
"筆資料"
)
scores1
=
np
.
random
.
normal
(
mu
,
sigma
,
size
=
N
)
# 隨機數
print
(
"資料個數1 :"
,
len
(
scores1
))
print
(
"最高分 :"
,
max
(
scores1
))
print
(
"最低分 :"
,
min
(
scores1
))
scores2
=
scores1
[
scores1
<=
100.0
]
scores3
=
scores2
[
scores2
>=
0.0
]
print
(
"資料個數2 :"
,
len
(
scores2
))
print
(
"資料個數3 :"
,
len
(
scores3
))
print
(
"最高分 :"
,
max
(
scores3
))
print
(
"最低分 :"
,
min
(
scores3
))
print
(
"資料1 平均 :"
,
sum
(
scores1
)
/
len
(
scores1
))
print
(
"資料3 平均 :"
,
sum
(
scores3
)
/
len
(
scores3
))
freq
=
[
0
]
*
100
illegal_cnt
=
0
for
score
in
scores1
:
if
score
<
0
:
print
(
"XXXXXXXX111 "
,
score
)
illegal_cnt
+=
1
continue
elif
score
>=
100
:
# print('XXXXXXXX222 ', score)
illegal_cnt
+=
1
continue
rank
=
int
(
score
)
freq
[
rank
]
+=
1
# print(score)
# print(rank)
print
(
"不合法的個數 :"
,
illegal_cnt
)
print
(
"人數分佈頻率:"
,
freq
)
plt
.
plot
(
freq
,
"--"
,
color
=
"r"
,
linewidth
=
2
)
# 指定bins
num_bins
=
range
(
0
,
100
,
1
)
# 設定bin的範圍
"""
for _ in num_bins:
print(_, end = " ")
"""
plt
.
hist
(
scores1
,
bins
=
num_bins
,
histtype
=
"bar"
,
facecolor
=
"b"
,
edgecolor
=
"g"
,
alpha
=
0.6
,
rwidth
=
0.7
,
label
=
"Normal distribution"
,
)
plt
.
title
(
"建立N筆成績資料 常態分佈"
)
show
()
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
# 測試 北大
plt
.
figure
(
figsize
=
(
10
,
6
))
N
=
1000
mu
,
sigma
=
620
,
37.4
x
=
np
.
linspace
(
mu
-
200
,
mu
+
200
,
N
)
# 從N1到N2, 分成N個, 包含頭尾
y
=
(
1
/
(
sigma
*
np
.
sqrt
(
2
*
np
.
pi
)))
*
np
.
exp
(
-
((
x
-
mu
)
**
2
)
/
(
2
*
sigma
**
2
))
plt
.
plot
(
x
,
y
,
"--"
,
color
=
"g"
,
linewidth
=
2
)
xx
=
[
580
,
600
,
680
,
620
]
X
=
[]
Y
=
[]
for
x
in
xx
:
y
=
(
1
/
(
sigma
*
np
.
sqrt
(
2
*
np
.
pi
)))
*
np
.
exp
(
-
((
x
-
mu
)
**
2
)
/
(
2
*
sigma
**
2
))
X
.
append
(
x
)
Y
.
append
(
y
)
plt
.
scatter
(
X
,
Y
)
plt
.
scatter
(
X
,
Y
,
s
=
200
,
c
=
"r"
)
show
()
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"作業完成"
)
print
(
"------------------------------------------------------------"
)
# 60個
sys
.
exit
()
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"新進"
)
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
from
scipy
.
stats
import
norm
import
statistics
# Plot between -10 and 10 with .001 steps.
x_axis
=
np
.
arange
(
-
20
,
20
,
0.01
)
# Calculating mean and standard deviation
mean
=
statistics
.
mean
(
x_axis
)
sd
=
statistics
.
stdev
(
x_axis
)
plt
.
plot
(
x_axis
,
norm
.
pdf
(
x_axis
,
mean
,
sd
))
show
()
print
(
"------------------------------------------------------------"
)
# 60個
import
scipy
.
stats
as
stats
mu
=
0
variance
=
1
sigma
=
math
.
sqrt
(
variance
)
x
=
np
.
linspace
(
mu
-
3
*
sigma
,
mu
+
3
*
sigma
,
100
)
plt
.
plot
(
x
,
stats
.
norm
.
pdf
(
x
,
mu
,
sigma
))
show
()
print
(
"------------------------------------------------------------"
)
# 60個
from
scipy
.
stats
import
norm
# Plot between -10 and 10 with .001 steps.
x_axis
=
np
.
arange
(
-
10
,
10
,
0.001
)
# Mean = 0, SD = 2.
plt
.
plot
(
x_axis
,
norm
.
pdf
(
x_axis
,
0
,
2
))
show
()
print
(
"------------------------------------------------------------"
)
# 60個
"""
import scipy as sp
from scipy import stats
## generate the data and plot it for an ideal normal curve
## x-axis for the plot
x_data = np.arange(-5, 5, 0.001)
## y-axis as the gaussian
y_data = stats.norm.pdf(x_axis, 0, 1)
## plot data
plt.plot(x_data, y_data)
show()
"""
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"將產生出來的常態分布做數字分析"
)
print
(
"normal 常態分布 N = 1000"
)
mu
,
sigma
=
100
,
15
N
=
100000
x
=
np
.
random
.
normal
(
mu
,
sigma
,
size
=
N
)
# 隨機數
print
(
"型態 : "
,
type
(
x
))
print
(
"長度 : "
,
len
(
x
))
print
(
"最大 : "
,
x
.
max
())
print
(
"最小 : "
,
x
.
min
())
print
(
"最大 : "
,
max
(
x
))
print
(
"最小 : "
,
min
(
x
))
print
(
"平均 : "
,
x
.
mean
())
print
(
"標準差 : "
,
x
.
std
())
print
(
"使用 numpy 模組計算統計資料"
)
print
(
f"Numpy模組 母體變異數 :
{
np
.
var
(
x
):6.2f
}
"
)
print
(
f"Numpy模組 樣本變異數 :
{
np
.
var
(
x
,
ddof
=
1
):6.2f
}
"
)
print
(
f"Numpy模組 母體標準差 :
{
np
.
std
(
x
):6.2f
}
"
)
print
(
f"Numpy模組 樣本標準差 :
{
np
.
std
(
x
,
ddof
=
1
):6.2f
}
"
)
import
statistics
print
(
"使用 statistics 模組計算統計資料"
)
print
(
f"Statistics 母體變異數 :
{
statistics
.
pvariance
(
x
):6.2f
}
"
)
print
(
f"Statistics 樣本變異數 :
{
statistics
.
variance
(
x
):6.2f
}
"
)
print
(
f"Statistics 母體標準差 :
{
statistics
.
pstdev
(
x
):6.2f
}
"
)
print
(
f"Statistics 樣本標準差 :
{
statistics
.
stdev
(
x
):6.2f
}
"
)
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
sys
.
exit
()
# ------------------------------------------------------------------------------------
# 共同抽出
# plt.title(r"$\mu=100,\ \sigma=15$, 加 理想曲線", fontweight="bold")
plt
.
title
(
r"$f(x)=\frac{1}{\sigma\sqrt{2\pi}}e^{\frac{-1}{2}(\frac{x-\mu}{\sigma})^{2}} $"
,
)
print
(
"------------------------------------------------------------"
)
# 60個
h
=
plt
.
hist
(
x
,
bins
=
num_bins
,
color
=
"g"
)
print
(
f"bins的 y 軸 =
{
h
[
0
]
}
"
)
print
(
f"bins的 x 軸 =
{
h
[
1
]
}
"
)
# plt.hist(x, color="g", rwidth=0.8) # 寬度設定 80%
# plt.hist(x, bins=num_bins, color="g", cumulative=True, rwidth=0.8) # 累計
print
(
"------------------------------------------------------------"
)
# 60個
# plt.hist 參數
# plt.hist(cc, bins=num_bins, color="g", alpha=0.5, density=False)
# density=True #以密度表示
# plt.hist 參數
# plt.hist(cc, bins=num_bins, color="g", alpha=0.5, density=False)
# density=True #以密度表示
print
(
"------------------------------------------------------------"
)
# 60個
hist參數
plt
.
hist
(
minutes
,
bins
=
4
,
edgecolor
=
"white"
,
linewidth
=
1.2
)
plt
.
hist
(
scores
,
bins
=
4
,
color
=
"red"
,
edgecolor
=
"white"
,
linewidth
=
1.2
)
# h = plt.hist(dice,sides,rwidth=0.8) # 繪製hist圖
h
=
plt
.
hist
(
dice
,
sides
,
rwidth
=
0.5
,
cumulative
=
True
)
# 繪製hist圖
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------------------------------------"
)
# 60個
print
(
"------------------------------"
)
# 30個
Back
|
FazBrowse Home
|
New Git URL