forked from holtzy/The-Python-Graph-Gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPGG_notebook.py
10318 lines (6486 loc) · 270 KB
/
PGG_notebook.py
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
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ------------------------------------------------ #
# THE PYTHON_GRAPH_GALLERY
# Hundreds of charts made with python
# www.python-graph-gallery.com
#
# by Yan Holtz
# ------------------------------------------------ #
# Welcome to the Python Graph gallery
# All the graphics displayed online are initialy created here.
# Charts are organized per section.
# Feel free to use this file, but honestly, it is more convenient to visit the website I believe...
#
# www.python-graph-gallery.com
#
-----------------------------------------
| |
| |
| SERIE #1 -> #20 BARPLOT MATPLOTLIB
| |
| |
-----------------------------------------
# ————————————————————————————————————————————————————
# #1 Basic barplot
import numpy as np
import matplotlib.pyplot as plt
# Choose the dot per inch
my_dpi=96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# Choose the height of the bars
height = [3, 12, 5, 18, 45]
# Choose the names of the bars
bars = ('A', 'B', 'C', 'D', 'E')
y_pos = np.arange(len(bars))
# Create bars
plt.bar(y_pos, height)
# Create names on the x-axis
plt.xticks(y_pos, bars)
# Save the figure and choose a name
plt.savefig('#1_basic_barplot.png')
# Show graphic
plt.show()
# ————————————————————————————————————————————————————
# #2 Horizontal barplot
import numpy as np
import matplotlib.pyplot as plt
# Choose the dot per inch
my_dpi=96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# Choose the height of the bars
height = [3, 12, 5, 18, 45]
# Choose the names of the bars
bars = ('A', 'B', 'C', 'D', 'E')
y_pos = np.arange(len(bars))
# Create horizontal bars
plt.barh(y_pos, height)
# Create names on the y-axis
plt.yticks(y_pos, bars)
# Save the figure and choose a name
plt.savefig('#2_horizontal_barplot.png')
# Show graphic
plt.show()
#————————————————————————————————————————————————————
# #3 Control color of barplot
import numpy as np
import matplotlib.pyplot as plt
# Choose the dot per inch
my_dpi=96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# Choose the height of the bars
height = [3, 12, 5, 18, 45]
# Choose the names of the bars
bars = ('A', 'B', 'C', 'D', 'E')
y_pos = np.arange(len(bars))
# Choose color using RGB:
plt.bar(y_pos, height, color=(0.2, 0.4, 0.6, 0.6))
plt.xticks(y_pos, bars)
# Save the figure and choose a name
plt.savefig('#3_control_color_barplot1.png')
plt.show()
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# Choose the color for each bar
plt.bar(y_pos, height, color=['black', 'red', 'green', 'blue', 'cyan'])
plt.xticks(y_pos, bars)
# Save the figure and choose a name
plt.savefig('#3_control_color_barplot2.png')
plt.show()
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# Change edge color
plt.bar(y_pos, height, color=(0.1, 0.1, 0.1, 0.1), edgecolor='blue')
plt.xticks(y_pos, bars)
# Save the figure and choose a name
plt.savefig('#3_control_color_barplot3.png')
plt.show()
my_dpi = 96
plt.figure(figsize=(480 / my_dpi, 480 / my_dpi), dpi=my_dpi)
# Change width of edgecolor
plt.bar(y_pos, height, color=(0.1, 0.1, 0.1, 0.1), edgecolor='blue', linewidth = '3')
plt.xticks(y_pos, bars)
# Save the figure and choose a name
plt.savefig('#3_control_color_barplot4.png')
plt.show()
# ———————————————————————————————————
# #4 Add title and axe labels
import numpy as np
import matplotlib.pyplot as plt
# Choose the dot per inch
my_dpi=96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# Choose the height of the bars
height = [3, 12, 5, 18, 45]
# Choose the names of the bars
bars = ('A', 'B', 'C', 'D', 'E')
y_pos = np.arange(len(bars))
# Create bars and choose
plt.bar(y_pos, height, color = (0.5,0.1,0.5,0.6))
plt.title('My title')
plt.xlabel('categories')
plt.ylabel('values')
plt.ylim(0,60)
# Create names
plt.xticks(y_pos, bars)
plt.savefig('#4_add_title_and_axe_labels.png')
# Show graphic
plt.show()
# ———————————————————————————————————
# #5 Custom the space between bars, and their width
import matplotlib.pyplot as plt
# Choose the dot per inch
my_dpi=96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# Choose the height of the bars
height = [3, 12, 5, 18, 45]
# Choose the names of the bars
bars = ('A', 'B', 'C', 'D', 'E')
# Choose the position of each barplots on the x-axis (space=1,4,3,1)
y_pos = [0,1,5,8,9]
# Create bars
plt.bar(y_pos, height)
# Create names on the x-axis
plt.xticks(y_pos, bars)
# Save the figure and choose a name
plt.savefig('#5_custom_space_between_bars.png')
# Show graphic
plt.show()
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# Choose the width of each bar
width = [0.1,0.2,3,1.5,0.3]
y_pos = [0,0.3,2,4.5,5.5]
plt.bar(y_pos, height, width=width)
plt.xticks(y_pos, bars)
plt.savefig('#5_custom_width_of_bars.png')
plt.show()
# ———————————————————————————————————
# #6 Change texture of barplots
import numpy as np
import matplotlib.pyplot as plt
# Choose the dot per inch
my_dpi = 96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480 / my_dpi, 480 / my_dpi), dpi=my_dpi)
# Choose the height of the bars
height = [2, 5, 4, 6]
# Choose the names of the bars
bars = ('A', 'B', 'C', 'D')
# Choose the angle and density of hatch
patterns = ['-', '///', '|||', '//////']
y_pos = np.arange(len(patterns))
# Create bars
for i in range(len(patterns)):
plt.bar(i, height[i], hatch=patterns[i], color='pink', edgecolor='black')
# Create names on the x-axis
plt.xticks(y_pos, bars)
# Save the figure and choose a name
plt.savefig('#6_change_texture.png')
# Show graphic
plt.show()
# ———————————————————————————————————
# #7 Custom Barplot Layout
import numpy as np
import matplotlib.pyplot as plt
# Choose the dot per inch
my_dpi=96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# Choose the height of the bars
height = [3, 12, 5, 18, 45]
# Choose the names of the bars
bars = ('group1', 'group2', 'group3', 'group4', 'group5')
y_pos = np.arange(len(bars))
# Create bars
plt.bar(y_pos, height)
# Create names on the x-axis
plt.xticks(y_pos, bars, color='orange')
plt.yticks(color='orange')
# Save the figure and choose a name
plt.savefig('#7_custom_label.png')
# Show graphic
plt.show()
my_dpi=96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
bars = ('A','B','C','D','E')
# Create bars
plt.bar(y_pos, height)
# Create names on the x-axis
plt.xticks(y_pos, bars)
plt.xlabel('category', fontweight='bold', color = 'orange', fontsize='18')
# Save the figure and choose a name
plt.savefig('#7_custom_axis_name.png')
# Show graphic
plt.show()
my_dpi=96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
bars = ("very long group name 1","very long group name 2","very long group name 3","very long group name 4","very long group name 5")
# Create bars
plt.bar(y_pos, height)
# Rotation of the bars names
plt.xticks(y_pos, bars, rotation=90)
# Custom the subplot layout
plt.subplots_adjust(bottom=0.4, top=0.99)
# Save the figure and choose a name
plt.savefig('#7_increase_margin.png')
# Show graphic
plt.show()
# ———————————————————————————————————
# #8 Confidence Interval on barplot
import numpy as np
import matplotlib.pyplot as plt
# Choose the dot per inch
my_dpi = 96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480 / my_dpi, 480 / my_dpi), dpi=my_dpi)
barWidth = 0.3
# Choose the height of the blue bars
bars1 = [10, 9, 2]
# Choose the height of the cyan bars
bars2 = [10.8, 9.5, 4.5]
# Choose the height of the error bars (bars1)
yer1 = [0.5, 0.4, 0.5]
# Choose the height of the error bars (bars2)
yer2 = [1, 0.7, 1]
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]
# Create blue bars
plt.bar(r1, bars1, width = barWidth, color = 'blue', edgecolor = 'black', yerr=yer1, capsize=7, label='poacee')
# Create cyan bars
plt.bar(r2, bars2, width = barWidth, color = 'cyan', edgecolor = 'black', yerr=yer2, capsize=7, label='sorgho')
plt.xticks([r + barWidth for r in range(len(bars1))], ['cond_A', 'cond_B', 'cond_C'])
plt.ylabel('height')
# Create legend
plt.legend()
# Save the figure and choose a name
plt.savefig('#8_confidence_interval.png')
# Show graphic
plt.show()
# ———————————————————————————————————
# #9 Plotting factors vs factors
# --> Cannot publish witout the source.
import matplotlib.pyplot as plt
# Choose the dot per inch
my_dpi = 96
# Choose the dimensions for the figure (here 480x480)
fig, ax1 = plt.subplots(figsize=(480 / my_dpi, 480 / my_dpi), dpi=my_dpi)
ax2 = ax1.twinx()
labels = " A: Stars, White Dwarfs and Solar System\n B: White Dwarf Binaries, Neutron Star Binaries, Cataclysmic Variables, ULXs and Black Holes\n C: Supernovae, Supernova Remnants, Diffuse (galactic) Emission and Isolated Neutron Stars\n D: Galaxies and Galactic Surveys\n E: Active Galactic Nuclei, Quasars and BL-Lac Objects\n F: Groups of Galaxies, Clusters of Galaxies and Superclusters\n G: Cosmology, Extragalactic Deep Fields and Area Surveys"
# Custom the textBox (data coordinates: x y, text, style)
plt.text(-2.7, -0.45, labels, color='black', size=7,
bbox=dict(facecolor='none', edgecolor='black', boxstyle='round,pad=0.1'))
# Height of green bars
bars1 = [0.3, 0.05, 0.1, 0.15, 0.25, 0.05, 0.19]
# Height of orange bars
bars2 = [0.25, 0.55, 0.55, 0.25, 0.3, 0.4, 0.4]
# Height of blue bars
bars3 = [0.45, 0.4, 0.35, 0.6, 0.45, 0.55, 0.41]
# Height of bars1 + bars2
bars = [0.55, 0.6, 0.65, 0.4, 0.55, 0.45, 0.59]
# The position of the bars on the x-axis
r = [0,1.6,3.2,4.1,6.9,9.9,11]
# Height of the yticks
ytick = [0.10, 0.45, 0.80]
barWidth = [0.8,2,0.8,0.5,4.5,1,0.6]
names = ['A','B','C','D','E','F','G']
name = ['A', 'B', 'C']
# Create green bars
plt.bar(r, bars1, color='#B3E2CD', width=barWidth, edgecolor='black')
# Create orange bars
plt.bar(r, bars2, bottom=bars1, color='#FDCDAC',width=barWidth, edgecolor='black')
# Create blue bars
plt.bar(r, bars3, bottom=bars, color='#CBD5E8',width=barWidth, edgecolor='black')
plt.xticks(r, names)
plt.yticks(ytick, name)
plt.title("XMM AO7 accepted proposals", fontweight='bold')
ax1.set_xlabel("Science Category")
plt.ylabel("Priority")
plt.subplots_adjust(bottom=0.33, top=0.95)
# Save the figure and choose a name
plt.savefig('#9_plotting_factor_vs_factor.png')
# Show graphic
plt.show()
# ———————————————————————————————————
# #10 Barplot with number of observations
import matplotlib.pyplot as plt
# Choose the dot per inch
my_dpi = 96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480 / my_dpi, 480 / my_dpi), dpi=my_dpi)
# The width of the bars
barWidth = 0.9
# Choose the height of the purple bars
bars1 = [3, 3, 1]
# Choose the height of the grey bars
bars2 = [4, 2, 3]
# Choose the height of the green bars
bars3 = [4, 6, 7, 10, 4, 4]
# Height of the bars (y)
bars4 = bars1 + bars2 + bars3
# The position of the purple bars on the x-axis
r1 = [1,5,9]
# The position of the grey bars on the x-axis
r2 = [2,6,10]
# The position of the green bars on the x-axis
r3 = [3,4,7,8,11,12]
# The position of the bars on the x-axis (x)
r4 = r1 + r2 + r3
# Create purple bars
plt.bar(r1, bars1, width = barWidth, color = (0.3,0.1,0.4,0.6), label='Alone')
# Create grey bars
plt.bar(r2, bars2, width = barWidth, color = (0.3,0.5,0.4,0.6), label='With Himself')
# Create green bars
plt.bar(r3, bars3, width = barWidth, color = (0.3,0.9,0.4,0.6), label='With other genotype')
# Create legend
plt.legend()
# Text below each barplot with a rotation at 90°
plt.xticks([r + barWidth for r in range(len(r4))], ['DD', 'with himself', 'with DC', 'with Silur', 'DC', 'with himself', 'with DD', 'with Silur', 'Silur', 'with himself', 'with DD', 'with DC'], rotation=90)
# Create labels
label = ['n = 6', 'n = 25', 'n = 13', 'n = 36', 'n = 30', 'n = 11', 'n = 16', 'n = 37', 'n = 14', 'n = 4', 'n = 31', 'n = 34']
# Text on the top of each barplot
for i in range(len(r4)):
plt.text(x = r4[i]-0.5 , y = bars4[i]+0.1, s = label[i], size = 6)
# Adjust the location of the figure
plt.subplots_adjust(bottom= 0.2, top = 0.98)
# Save the figure and choose a name
plt.savefig('#10_barplot_with_number_of_observations.png')
# Show graphic
plt.show()
# ———————————————————————————————————
# #11 - 12 - 13 Grouped and stacked barplot
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
import pandas as pd
# Choose the dot per inch
my_dpi = 96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480 / my_dpi, 480 / my_dpi), dpi=my_dpi)
# y-axis in bold
rc('font', weight='bold')
# Height of brown bars
bars1 = [12, 28, 1, 8, 22]
# Height of green bars (middle)
bars2 = [28, 7, 16, 4, 10]
# Height of green bars (top)
bars3 = [25, 3, 23, 25, 17]
# Heights of bars1 + bars2
bars = [40, 35, 17, 12, 32]
# The position of the bars on the x-axis
r = [0,1,2,3,4]
names = ['A','B','C','D','E']
barWidth = 1
# Create brown bars
plt.bar(r, bars1, color='#7f6d5f', edgecolor='white', width=barWidth)
# Create green bars (middle)
plt.bar(r, bars2, bottom=bars1, color='#557f2d', edgecolor='white', width=barWidth)
# Create green bars (top)
plt.bar(r, bars3, bottom=bars, color='#2d7f5e', edgecolor='white', width=barWidth)
plt.xticks(r, names, fontweight='bold')
plt.xlabel("group")
# Save the figure and choose a name
plt.savefig('#12_stacked_barplot.png')
# Show graphic
plt.show()
# Choose the dot per inch
my_dpi = 96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480 / my_dpi, 480 / my_dpi), dpi=my_dpi)
barWidth = 0.25
# Height of brown bars
bars1 = [12, 30, 1, 8, 22]
# Height of green bars (second)
bars2 = [28, 6, 16, 5, 10]
# Height of green bars (third)
bars3 = [29, 3, 24, 25, 17]
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]
r3 = [x + barWidth for x in r2]
# Create brown bars
plt.bar(r1, bars1, color='#7f6d5f', width=barWidth, edgecolor='white', label='var1')
# Create green bars (second)
plt.bar(r2, bars2, color='#557f2d', width=barWidth, edgecolor='white', label='var2')
# Create green bars (third)
plt.bar(r3, bars3, color='#2d7f5e', width=barWidth, edgecolor='white', label='var3')
plt.xlabel('group', fontweight='bold')
# Add xticks on the middle of the group bars
plt.xticks([r + barWidth for r in range(len(bars1))], ['A', 'B', 'C', 'D', 'E'])
# Create legend
plt.legend()
# Save the figure and choose a name
plt.savefig('#12_grouped_barplot.png')
# Show graphic
plt.show()
# Choose the dot per inch
my_dpi = 96
# Choose the dimensions for the figure (here 480x480)
plt.figure(figsize=(480 / my_dpi, 480 / my_dpi), dpi=my_dpi)
# The position of the bars on the x-axis
r = [0,1,2,3,4]
raw_data = {'greenBars': [20, 1.5, 7, 10, 5], 'orangeBars': [5, 15, 5, 10, 15],'blueBars': [2, 15, 18, 5, 10]}
df = pd.DataFrame(raw_data)
# Create the total
totals = [i+j+k for i,j,k in zip(df['greenBars'], df['orangeBars'], df['blueBars'])]
# Create the percentage
greenBars = [i / j * 100 for i,j in zip(df['greenBars'], totals)]
# Create the percentage
orangeBars = [i / j * 100 for i,j in zip(df['orangeBars'], totals)]
# Create the percentage
blueBars = [i / j * 100 for i,j in zip(df['blueBars'], totals)]
barWidth = 0.85
names = ('A','B','C','D','E')
# Create green Bars
plt.bar(r, greenBars, color='#b5ffb9', edgecolor='white', width=barWidth)
# Create orange Bars
plt.bar(r, orangeBars, bottom=greenBars, color='#f9bc86', edgecolor='white', width=barWidth)
# Create blue Bars
plt.bar(r, blueBars, bottom=[i+j for i,j in zip(greenBars, orangeBars)], color='#a3acff', edgecolor='white', width=barWidth)
plt.xticks(r, names)
plt.xlabel("group")
# Save the figure and choose a name
plt.savefig('#12_stacked_percent_barplot.png')
# Show graphic
plt.show()
-----------------------------------------
| |
| |
| SERIE #20 -> #30 HISTOGRAM SEABORN
| |
| |
-----------------------------------------
# ———————————————————————————————————
# #20 Basic Histogram | Seaborn
# Import library and dataset
import seaborn as sns
df = sns.load_dataset('iris')
# Make default histogram of sepal length
p1=sns.distplot( df["sepal_length"] )
#sns.plt.show()
#save
fig = p1.get_figure()
fig.set_size_inches(4.8, 4.8)
fig.savefig('PNG/#20_Basic_Histogram_seaborn1.png')
# Note that you can control the number of bins
p2=sns.distplot( df["sepal_length"], bins=20 )
#sns.plt.show()
#save
fig = p2.get_figure()
fig.set_size_inches(4.8, 4.8)
fig.savefig('PNG/#20_Basic_Histogram_seaborn2.png')
# ———————————————————————————————————
# #21 Control Rug and Distribution | Seaborn
# Import library and dataset
import seaborn as sns
df = sns.load_dataset('iris')
# Hist only
p1=sns.distplot( a=df["sepal_length"], hist=True, kde=False, rug=False )
#sns.plt.show()
#save
fig = p1.get_figure()
fig.set_size_inches(4.8, 4.8)
fig.savefig('PNG/#21_Display_Rug_and_distribution_on_hist1.png')
# Hist + Rug + kernel density
p2=sns.distplot( a=df["sepal_length"], hist=True, kde=True, rug=True )
#sns.plt.show()
fig = p2.get_figure()
fig.set_size_inches(4.8, 4.8)
fig.savefig('PNG/#21_Display_Rug_and_distribution_on_hist2.png')
# To change parameters of rug
p3=sns.distplot( a=df["sepal_length"], rug=True,
rug_kws={"color": "r", "alpha":0.3, "linewidth": 2, "height":0.2 }
)
#sns.plt.show()
fig = p3.get_figure()
fig.set_size_inches(4.8, 4.8)
fig.savefig('PNG/#21_Display_Rug_and_distribution_on_hist3.png')
# To change parameters of density distribution
p4=sns.distplot( a=df["sepal_length"], kde=True,
kde_kws={"color": "g", "alpha":0.3, "linewidth": 5, "shade":True }
)
#sns.plt.show()
fig = p4.get_figure()
fig.set_size_inches(4.8, 4.8)
fig.savefig('PNG/#21_Display_Rug_and_distribution_on_hist4.png')
# ———————————————————————————————————
# #22 Control Color of histogram | Seaborn
# Import library and dataset
import seaborn as sns
df = sns.load_dataset('iris')
# Color of bars:
sns.distplot( df["sepal_length"] , color="peru")
sns.plt.show()
# ———————————————————————————————————
# #23 Vertical Histogram | Seaborn
# Import library and dataset
import seaborn as sns
df = sns.load_dataset('iris')
# Vertical hist
p1=sns.distplot( df["sepal_length"] , color="skyblue", vertical=True)
#sns.plt.show()
fig = p1.get_figure()
fig.set_size_inches(4.8, 4.8)
fig.savefig('PNG/#23_Vertical_Histogram.png')
# ———————————————————————————————————
# #24 Histogram with boxplot on top | Seaborn
# Import library and dataset
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')
# Choose the dot per inch
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
# Cut the window in 2 parts
f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, gridspec_kw={"height_ratios": (.15, .85)})
# Add a graph in each part
sns.boxplot(df["sepal_length"], ax=ax_box)
sns.distplot(df["sepal_length"], ax=ax_hist)
ax_box.set(xlabel='')
# Save
plt.set_size_inches(4.8, 4.8)
plt.savefig('PNG/#24_Histogram_with_boxplot_on_top.png')
# -> Pas moyen de la sortir a la bonne taille mais tant pis...
# ———————————————————————————————————
# #25 Histograms of 2 variables | Seaborn
# Import library and dataset
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('iris')
# Method 1: on the same Axis
p1=sns.distplot( df["sepal_length"] , color="skyblue", label="Sepal Length")
p1=sns.distplot( df["sepal_width"] , color="red", label="Sepal Width")
p1=sns.plt.legend()
fig = p1.get_figure()
fig.set_size_inches(4.8, 4.8)
fig.savefig('PNG/#25_Histogram_of_several_variables1.png')
# Method 2: using subplots
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
sns.distplot( df["sepal_length"] , color="skyblue", ax=axes[0, 0])
sns.distplot( df["sepal_width"] , color="olive", ax=axes[0, 1])
sns.distplot( df["petal_length"] , color="gold", ax=axes[1, 0])
sns.distplot( df["petal_width"] , color="teal", ax=axes[1, 1])
plt.savefig('PNG/#25_Histogram_of_several_variables2.png')
# --> link vers page faceting pour plus de détail.
# ———————————————————————————————————
# #26 Bad chart: control size of bins!
# Import library and dataset
# TODO
import seaborn as sns
df = sns.load_dataset('iris')
# Color of bars:
sns.distplot( df["sepal_length"] , color="peru")
sns.plt.show()
-----------------------------------------
| |
| |
| SERIE #30 -> #40 BOXPLOT SEABORN
| |
| |
-----------------------------------------
# ———————————————————————————————————
# #30 Basic Boxplot | Seaborn
# -- ONE VARIABLE
# library & dataset
import seaborn as sns
df = sns.load_dataset('iris')
# Make boxplot for one group only