-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchartify-a-simple-yet-powerful-python-data-visualization-tool-which-boost-your-productivity-as-a-data-scientist.html
1792 lines (1653 loc) · 194 KB
/
chartify-a-simple-yet-powerful-python-data-visualization-tool-which-boost-your-productivity-as-a-data-scientist.html
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
<!DOCTYPE html>
<!--[if lt IE 9 ]><html class="no-js oldie" lang="zh-hant-tw"> <![endif]-->
<!--[if IE 9 ]><html class="no-js oldie ie9" lang="zh-hant-tw"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!-->
<html class="no-js" lang="zh-hant-tw">
<!--<![endif]-->
<head>
<!--- basic page needs
================================================== -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="author" content="Lee Meng" />
<title>LeeMeng - Chartify:讓數據科學家效率加倍的 Python 資料視覺化工具</title>
<!--- article-specific meta data
================================================== -->
<meta name="description" content="此文會展示如何利用 Chartify,一個直觀且貼心的 Python 繪圖函式庫,來對如 2018 臺北市候選人得票數、歷年各大洲來台人數等公開數據做資料視覺化。如果你想要學習利用 Python 實現資料視覺化,但還不知道怎麼開始;或是覺得目前使用的工具不太直覺,想要提升自己工作效率的話,這篇就是為你而寫的。" />
<meta name="keywords" content="資料視覺化, Python" />
<meta name="tags" content="資料視覺化" />
<meta name="tags" content="Python" />
<!--- Open Graph Object metas
================================================== -->
<meta property="og:image" content="https://leemeng.tw/theme/images/background/chartify-post-cover.jpg" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://leemeng.tw/chartify-a-simple-yet-powerful-python-data-visualization-tool-which-boost-your-productivity-as-a-data-scientist.html" />
<meta property="og:title" content="Chartify:讓數據科學家效率加倍的 Python 資料視覺化工具" />
<meta property="og:description" content="此文會展示如何利用 Chartify,一個直觀且貼心的 Python 繪圖函式庫,來對如 2018 臺北市候選人得票數、歷年各大洲來台人數等公開數據做資料視覺化。如果你想要學習利用 Python 實現資料視覺化,但還不知道怎麼開始;或是覺得目前使用的工具不太直覺,想要提升自己工作效率的話,這篇就是為你而寫的。" />
<!-- mobile specific metas
================================================== -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSS
================================================== -->
<!--for customized css in individual page-->
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/bootstrap.min.css">
<!--for showing toc navigation which slide in from left-->
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/toc-nav.css">
<!--for responsive embed youtube video-->
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/embed_youtube.css">
<!--for prettify dark-mode result-->
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/darkmode.css">
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/base.css">
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/vendor.css">
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/main.css">
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/ipython.css">
<link rel="stylesheet" type="text/css" href='https://leemeng.tw/theme/css/progress-bar.css' />
<!--TiqueSearch-->
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400">
<link rel="stylesheet" href="https://leemeng.tw/theme/tipuesearch/css/normalize.css">
<link rel="stylesheet" href="https://leemeng.tw/theme/tipuesearch/css/tipuesearch.css">
<!-- script
================================================== -->
<script src="https://leemeng.tw/theme/js/modernizr.js"></script>
<script src="https://leemeng.tw/theme/js/pace.min.js"></script>
<!-- favicons
================================================== -->
<link rel="shortcut icon" href="../theme/images/favicon.ico" type="image/x-icon"/>
<link rel="icon" href="../theme/images/favicon.ico" type="image/x-icon"/>
<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-106559980-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)};
gtag('js', new Date());
gtag('config', 'UA-106559980-1');
</script>
</head>
<body id="top">
<!-- header
================================================== -->
<header class="s-header">
<div class="header-logo">
<a class="site-logo" href="../index.html"><img src="https://leemeng.tw/theme/images/logo.png" alt="Homepage"></a>
</div>
<!--navigation bar ref: http://jinja.pocoo.org/docs/2.10/tricks/-->
<nav class="header-nav-wrap">
<ul class="header-nav">
<li>
<a href="../index.html#home">Home</a>
</li>
<li>
<a href="../index.html#about">About</a>
</li>
<li>
<a href="../index.html#projects">Projects</a>
</li>
<li class="current">
<a href="../blog.html">Blog</a>
</li>
<li>
<a href="https://demo.leemeng.tw">Demo</a>
</li>
<li>
<a href="../books.html">Books</a>
</li>
<li>
<a href="../index.html#contact">Contact</a>
</li>
</ul>
<!--<div class="search-container">-->
<!--<form action="../search.html">-->
<!--<input type="text" placeholder="Search.." name="search">-->
<!--<button type="submit"><i class="im im-magnifier" aria-hidden="true"></i></button>-->
<!--</form>-->
<!--</div>-->
</nav>
<a class="header-menu-toggle" href="#0"><span>Menu</span></a>
</header> <!-- end s-header -->
<!--TOC navigation displayed when clicked from left-navigation button-->
<div id="tocNav" class="overlay" onclick="closeTocNav()">
<div class="overlay-content">
<div id="toc"><ul><li><a class="toc-href" href="#" title="Chartify:讓數據科學家效率加倍的 Python 資料視覺化工具">Chartify:讓數據科學家效率加倍的 Python 資料視覺化工具</a><ul><li><a class="toc-href" href="#Chartify-來拯救世界" title="Chartify 來拯救世界">Chartify 來拯救世界</a></li><li><a class="toc-href" href="#這篇適合誰" title="這篇適合誰">這篇適合誰</a></li><li><a class="toc-href" href="#Python-函式庫" title="Python 函式庫">Python 函式庫</a></li><li><a class="toc-href" href="#範例資料及-Tidy-格式" title="範例資料及 Tidy 格式">範例資料及 Tidy 格式</a></li><li><a class="toc-href" href="#第一個-Chartify-Chart" title="第一個 Chartify Chart">第一個 Chartify Chart</a></li><li><a class="toc-href" href="#Chartify-提醒你提供重要訊息" title="Chartify 提醒你提供重要訊息">Chartify 提醒你提供重要訊息</a></li><li><a class="toc-href" href="#凡事有例外,或是懶了" title="凡事有例外,或是懶了">凡事有例外,或是懶了</a></li><li><a class="toc-href" href="#每年有多少人拜訪台灣?" title="每年有多少人拜訪台灣?">每年有多少人拜訪台灣?</a></li><li><a class="toc-href" href="#一個參數就能轉變圖表" title="一個參數就能轉變圖表">一個參數就能轉變圖表</a></li><li><a class="toc-href" href="#所以大家來台灣做什麼?" title="所以大家來台灣做什麼?">所以大家來台灣做什麼?</a></li><li><a class="toc-href" href="#一個字母就能翻轉世界" title="一個字母就能翻轉世界">一個字母就能翻轉世界</a></li><li><a class="toc-href" href="#在台北市,哪些地區比較多意外事故?" title="在台北市,哪些地區比較多意外事故?">在台北市,哪些地區比較多意外事故?</a></li><li><a class="toc-href" href="#Chartify-心法及資料視覺化" title="Chartify 心法及資料視覺化">Chartify 心法及資料視覺化</a></li><li><a class="toc-href" href="#結語" title="結語">結語</a></li><li><a class="toc-href" href="#Chartify-畫廊" title="Chartify 畫廊">Chartify 畫廊</a><ul><li><a class="toc-href" href="#Lollipop-Chart" title="Lollipop Chart">Lollipop Chart</a></li></ul></li></ul></li></ul></div>
</div>
</div>
<!--custom images with icon shown on left nav-->
<!--the details are set in `pelicanconf.py` as `LEFT_NAV_IMAGES`-->
<article class="blog-single">
<!-- page header/blog hero, use custom cover image if available
================================================== -->
<div class="page-header page-header--single page-hero" style="background-image:url(https://leemeng.tw/theme/images/background/chartify-post-cover.jpg)">
<div class="row page-header__content narrow">
<article class="col-full">
<div class="page-header__info">
<div class="page-header__cat">
<a href="https://leemeng.tw/tag/zi-liao-shi-jue-hua.html" rel="tag">資料視覺化</a>
<a href="https://leemeng.tw/tag/python.html" rel="tag">Python</a>
</div>
</div>
<h1 class="page-header__title">
<a href="https://leemeng.tw/chartify-a-simple-yet-powerful-python-data-visualization-tool-which-boost-your-productivity-as-a-data-scientist.html" title="">
Chartify:讓數據科學家效率加倍的 Python 資料視覺化工具
</a>
</h1>
<ul class="page-header__meta">
<li class="date">2018-11-26 (Mon)</li>
<li class="page-view">
15,945 views
</li>
</ul>
</article>
</div>
</div> <!-- end page-header -->
<div class="KW_progressContainer">
<div class="KW_progressBar"></div>
</div>
<div class="row blog-content" style="position: relative">
<div id="left-navigation">
<div id="search-wrap">
<i class="im im-magnifier" aria-hidden="true"></i>
<div id="search">
<form action="../search.html">
<div class="tipue_search_right"><input type="text" name="q" id="tipue_search_input" pattern=".{2,}" title="想搜尋什麼呢?(請至少輸入兩個字)" required></div>
</form>
</div>
</div>
<div id="toc-wrap">
<a title="顯示/隱藏 文章章節">
<i class="im im-menu" aria-hidden="true" onclick="toggleTocNav()"></i>
</a>
</div>
<div id="social-wrap" style="cursor: pointer">
<a class="open-popup" title="訂閱最新文章">
<i class="im im-newspaper-o" aria-hidden="true"></i>
</a>
</div>
<div id="social-wrap">
<a href="https://www.facebook.com/sharer/sharer.php?u=https%3A//leemeng.tw/chartify-a-simple-yet-powerful-python-data-visualization-tool-which-boost-your-productivity-as-a-data-scientist.html" target="_blank" title="分享到 Facebook">
<i class="im im-facebook" aria-hidden="true"></i>
</a>
</div>
<div id="social-wrap">
<a href="https://www.linkedin.com/shareArticle?mini=true&url=https%3A//leemeng.tw/chartify-a-simple-yet-powerful-python-data-visualization-tool-which-boost-your-productivity-as-a-data-scientist.html&title=Chartify%EF%BC%9A%E8%AE%93%E6%95%B8%E6%93%9A%E7%A7%91%E5%AD%B8%E5%AE%B6%E6%95%88%E7%8E%87%E5%8A%A0%E5%80%8D%E7%9A%84%20Python%20%E8%B3%87%E6%96%99%E8%A6%96%E8%A6%BA%E5%8C%96%E5%B7%A5%E5%85%B7&summary=%E6%AD%A4%E6%96%87%E6%9C%83%E5%B1%95%E7%A4%BA%E5%A6%82%E4%BD%95%E5%88%A9%E7%94%A8%20Chartify%EF%BC%8C%E4%B8%80%E5%80%8B%E7%9B%B4%E8%A7%80%E4%B8%94%E8%B2%BC%E5%BF%83%E7%9A%84%20Python%20%E7%B9%AA%E5%9C%96%E5%87%BD%E5%BC%8F%E5%BA%AB%EF%BC%8C%E4%BE%86%E5%B0%8D%E5%A6%82%202018%20%E8%87%BA%E5%8C%97%E5%B8%82%E5%80%99%E9%81%B8%E4%BA%BA%E5%BE%97%E7%A5%A8%E6%95%B8%E3%80%81%E6%AD%B7%E5%B9%B4%E5%90%84%E5%A4%A7%E6%B4%B2%E4%BE%86%E5%8F%B0%E4%BA%BA%E6%95%B8%E7%AD%89%E5%85%AC%E9%96%8B%E6%95%B8%E6%93%9A%E5%81%9A%E8%B3%87%E6%96%99%E8%A6%96%E8%A6%BA%E5%8C%96%E3%80%82%E5%A6%82%E6%9E%9C%E4%BD%A0%E6%83%B3%E8%A6%81%E5%AD%B8%E7%BF%92%E5%88%A9%E7%94%A8%20Python%20%E5%AF%A6%E7%8F%BE%E8%B3%87%E6%96%99%E8%A6%96%E8%A6%BA%E5%8C%96%EF%BC%8C%E4%BD%86%E9%82%84%E4%B8%8D%E7%9F%A5%E9%81%93%E6%80%8E%E9%BA%BC%E9%96%8B%E5%A7%8B%EF%BC%9B%E6%88%96%E6%98%AF%E8%A6%BA%E5%BE%97%E7%9B%AE%E5%89%8D%E4%BD%BF%E7%94%A8%E7%9A%84%E5%B7%A5%E5%85%B7%E4%B8%8D%E5%A4%AA%E7%9B%B4%E8%A6%BA%EF%BC%8C%E6%83%B3%E8%A6%81%E6%8F%90%E5%8D%87%E8%87%AA%E5%B7%B1%E5%B7%A5%E4%BD%9C%E6%95%88%E7%8E%87%E7%9A%84%E8%A9%B1%EF%BC%8C%E9%80%99%E7%AF%87%E5%B0%B1%E6%98%AF%E7%82%BA%E4%BD%A0%E8%80%8C%E5%AF%AB%E7%9A%84%E3%80%82&source=https%3A//leemeng.tw/chartify-a-simple-yet-powerful-python-data-visualization-tool-which-boost-your-productivity-as-a-data-scientist.html" target="_blank" title="分享到 LinkedIn">
<i class="im im-linkedin" aria-hidden="true"></i>
</a>
</div>
<div id="social-wrap">
<a href="https://twitter.com/intent/tweet?text=Chartify%EF%BC%9A%E8%AE%93%E6%95%B8%E6%93%9A%E7%A7%91%E5%AD%B8%E5%AE%B6%E6%95%88%E7%8E%87%E5%8A%A0%E5%80%8D%E7%9A%84%20Python%20%E8%B3%87%E6%96%99%E8%A6%96%E8%A6%BA%E5%8C%96%E5%B7%A5%E5%85%B7&url=https%3A//leemeng.tw/chartify-a-simple-yet-powerful-python-data-visualization-tool-which-boost-your-productivity-as-a-data-scientist.html&hashtags=zi-liao-shi-jue-hua,python" target="_blank" title="分享到 Twitter">
<i class="im im-twitter" aria-hidden="true"></i>
</a>
</div>
<!--custom images with icon shown on left nav-->
</div>
<div class="col-full blog-content__main">
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>如果你正在學習 Python 和/或 資料視覺化,看這篇就對了。</p>
<p>身為一個數據科學家(<strong>D</strong>ata <strong>S</strong>cientist, DS),使用 R 語言或是 Python 來視覺化手邊的數據可說是件稀鬆平常,但卻不一定愉快的工作。很多時候你得要花個 10 到 20 分鐘「微整型」自己的圖表,寫一大堆瑣碎的 code 就只是為了調整 X 軸的數字格式,或是改變長條圖的方向。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Chartify-來拯救世界">Chartify 來拯救世界<a class="anchor-link" href="#Chartify-來拯救世界">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><a href="https://labs.spotify.com/2018/11/15/introducing-chartify-easier-chart-creation-in-python-for-data-scientists/">Spotify 最近開源</a>了一個簡單卻同時強大的 Python 資料視覺化 Package:<a href="https://github.com/spotify/chartify">Chartify</a>。我在工作中實際使用後覺得非常方便,所以想在這邊跟你分享我的經驗、並展示如何實際利用 Chartify 來視覺化一些台灣的 Open Data,讓你快速上手 Chartify。</p>
<p>小提醒:Dark Mode 顯示的圖表顏色會跟真實圖表有所差距。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<center>
<img src="https://leemeng.tw/images/chartify/chartify-gallery.jpg" style="mix-blend-mode: initial;"/>
</center>
<center>
Chartify 圖表
(<a href="https://labs.spotify.com/2018/11/15/introducing-chartify-easier-chart-creation-in-python-for-data-scientists/" target="_blank">圖片來源</a>)
<br/>
<br/>
</center>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Chartify 有 3 大優點:</p>
<ul>
<li>預設就很漂亮的圖表樣式</li>
<li>簡單直覺的 API</li>
<li>內建非常詳細的使用說明</li>
</ul>
<p>透過此文你將會發現,相較於 <a href="https://matplotlib.org/">Matplotlib</a> 或是 <a href="https://ggplot2.tidyverse.org/">ggplot2</a> 等客製化能力強大但學習成本較高的繪圖工具,學習 Chartify 的投資報酬率非常地高,且你馬上就能將其實際應用在自己的工作裡頭。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="這篇適合誰">這篇適合誰<a class="anchor-link" href="#這篇適合誰">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>只要你在學習資料科學或是 Python,這篇基本上都適合你:</p>
<ul>
<li>菜鳥 DS:從頭了解如何使用 Chartify 及 Python 來產生漂亮圖表</li>
<li>老手 DS:利用 Chartify 來有效率地解決你目前 80 % 的繪圖需求</li>
<li>所有想要快速學會如何使用 Python 來做資料視覺化的你</li>
</ul>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<img src="https://leemeng.tw/images/chartify/alex-kotliarskyi-361099-unsplash.jpg"/>
<br/>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>事不宜遲,讓我們馬上開始使用 Chartify 吧!</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Python-函式庫">Python 函式庫<a class="anchor-link" href="#Python-函式庫">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>接下來會畫不少圖,但在這篇文章我們只需要 <a href="https://pandas.pydata.org/">Pandas</a> 以及 Chartify 就能完成所有圖表,輕鬆寫意。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">chartify</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>你可以先跟著此文了解 Chartify 功能,等到自己手癢時再參考<a href="https://github.com/spotify/chartify#installation">官方 repo</a> 安裝。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="範例資料及-Tidy-格式">範例資料及 Tidy 格式<a class="anchor-link" href="#範例資料及-Tidy-格式">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>為了讓你有資料可以馬上開始嘗試各種圖表,Chartify 貼心地內建了一些簡單數據。你可以利用以下的方式將其取出並建立一個 Pandas 的 <a href="https://pandas.pydata.org/pandas-docs/version/0.21/generated/pandas.DataFrame.html">DataFrame</a>:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">df</span> <span class="o">=</span> <span class="n">chartify</span><span class="o">.</span><span class="n">examples</span><span class="o">.</span><span class="n">example_data</span><span class="p">()</span>
<span class="n">df</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe table table-striped table-responsive">
<thead>
<tr style="text-align: right;">
<th></th>
<th>date</th>
<th>country</th>
<th>fruit</th>
<th>unit_price</th>
<th>quantity</th>
<th>total_price</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>2017-10-21</td>
<td>US</td>
<td>Banana</td>
<td>0.303711</td>
<td>4</td>
<td>1.214846</td>
</tr>
<tr>
<th>1</th>
<td>2017-05-30</td>
<td>JP</td>
<td>Banana</td>
<td>0.254109</td>
<td>4</td>
<td>1.016436</td>
</tr>
<tr>
<th>2</th>
<td>2017-05-21</td>
<td>CA</td>
<td>Banana</td>
<td>0.268635</td>
<td>4</td>
<td>1.074539</td>
</tr>
<tr>
<th>3</th>
<td>2017-09-18</td>
<td>BR</td>
<td>Grape</td>
<td>2.215277</td>
<td>2</td>
<td>4.430554</td>
</tr>
<tr>
<th>4</th>
<td>2017-12-08</td>
<td>US</td>
<td>Banana</td>
<td>0.308337</td>
<td>5</td>
<td>1.541687</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>稍微檢視一下,你應該可以看出 <code>df</code> 這個 DataFrame 裡頭紀錄了不同國家 <code>country</code> 在不同日期 <code>date</code> 下,各個水果 <code>fruit</code> 的單價 <code>unit_price</code> 以及總價格 <code>total_price</code>。</p>
<p>但更重要的是,<code>df</code> 的格式是 Tidy 格式。要使用 Chartify 畫圖,基本上你的 DataFrame 都該轉換成 Tidy 格式。Tidy 格式又是什麼呢?依照 R 語言 ggplot2 的作者 <a href="http://hadley.nz/">Hadley Wickham</a> 的解釋:</p>
<ul>
<li>1 個變數只存在一欄裡頭(Column)</li>
<li>一列(Row)則代表 1 個觀測結果</li>
</ul>
<p>比方說,彙整資料的 Pivot Table 大多不是 Tidy 格式:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">pivot_table</span><span class="p">(</span>
<span class="n">values</span><span class="o">=</span><span class="p">[</span><span class="s1">'unit_price'</span><span class="p">],</span>
<span class="n">index</span><span class="o">=</span><span class="s1">'country'</span><span class="p">,</span>
<span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">'fruit'</span><span class="p">])</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead tr th {
text-align: left;
}
.dataframe thead tr:last-of-type th {
text-align: right;
}
</style>
<table border="1" class="dataframe table table-striped table-responsive">
<thead>
<tr>
<th></th>
<th colspan="4" halign="left">unit_price</th>
</tr>
<tr>
<th>fruit</th>
<th>Apple</th>
<th>Banana</th>
<th>Grape</th>
<th>Orange</th>
</tr>
<tr>
<th>country</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>BR</th>
<td>0.978156</td>
<td>0.252695</td>
<td>2.022562</td>
<td>0.501151</td>
</tr>
<tr>
<th>CA</th>
<td>1.009426</td>
<td>0.249756</td>
<td>1.997512</td>
<td>0.504068</td>
</tr>
<tr>
<th>GB</th>
<td>1.007317</td>
<td>0.241405</td>
<td>2.002525</td>
<td>0.501009</td>
</tr>
<tr>
<th>JP</th>
<td>0.988600</td>
<td>0.249436</td>
<td>2.060732</td>
<td>0.499539</td>
</tr>
<tr>
<th>US</th>
<td>0.987463</td>
<td>0.252078</td>
<td>1.970634</td>
<td>0.504666</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>在這邊,明明都屬於 <code>fruit</code> 這個變數底下的 <code>Apple</code>、<code>Banana</code> 等水果,其單價 <code>unit_price</code> 卻都自成一欄,而這會造成我們畫圖的困難。簡而言之,只要是你想拿來畫圖的變數,其值都應該只存在一個 column 裏頭。想了解更多 Tidy 格式的細節,可以查看<a href="https://tomaugspurger.github.io/modern-5-tidy.html">這篇文章</a>。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="第一個-Chartify-Chart">第一個 Chartify Chart<a class="anchor-link" href="#第一個-Chartify-Chart">¶</a></h2><p>現在,讓我們馬上用 Chartify 針對水果的單價 <code>unit_price</code> 以及總價格 <code>total_price</code> 這 2 個變數畫一個簡單的散佈圖(Scatter Plot):</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">ch</span> <span class="o">=</span> <span class="n">chartify</span><span class="o">.</span><span class="n">Chart</span><span class="p">()</span>
<span class="n">ch</span><span class="o">.</span><span class="n">plot</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span>
<span class="n">data_frame</span><span class="o">=</span><span class="n">df</span><span class="p">,</span>
<span class="n">x_column</span><span class="o">=</span><span class="s1">'unit_price'</span><span class="p">,</span>
<span class="n">y_column</span><span class="o">=</span><span class="s1">'total_price'</span>
<span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">show</span><span class="p">(</span><span class="s1">'png'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<img src="https://leemeng.tw/images/chartify/chart1.jpg" style="mix-blend-mode: initial;"/>
<br/>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><br/>
就算完全不熟 Chartify 或 Python,相信你也可以從程式碼感覺出來,使用 Chartify 畫圖非常地直覺。基本上定義好一個 <code>Chart</code> 物件 <code>ch</code>,並使用 <code>ch.plot.圖表類型</code> 的語法即可繪製各種美麗圖表。</p>
<p>最後,你會需要一個 <code>ch.show()</code> 來告訴 Chartify 將圖渲染(Render)出來。而這邊使用 <code>png</code> 只是告訴 Chartify 將圖表輸出為圖片而非 HTML,方便此部落格的使用。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>再看一次剛剛的程式碼:</p>
<div class="highlight"><pre><span></span><span class="n">ch</span> <span class="o">=</span> <span class="n">chartify</span><span class="o">.</span><span class="n">Chart</span><span class="p">()</span>
<span class="n">ch</span><span class="o">.</span><span class="n">plot</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span>
<span class="n">data_frame</span><span class="o">=</span><span class="n">df</span><span class="p">,</span>
<span class="n">x_column</span><span class="o">=</span><span class="s1">'unit_price'</span><span class="p">,</span>
<span class="n">y_column</span><span class="o">=</span><span class="s1">'total_price'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">show</span><span class="p">(</span><span class="s1">'png'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>基本上所有 Chartify 圖表都需要數據(Pandas 的 DataFrame <code>df</code>),以及要在 X 及 Y 軸上呈現的變數/欄位名稱。</p>
<p>參數 <code>x_column</code> 及 <code>y_column</code> 的名稱就暗示著你,1 個 column 對應到 1 個變數。在這邊我們將水果單價 <code>unit_price</code> 對應到 X 軸、總價格 <code>total_price</code> 對應到 Y 軸。</p>
<p>你現在應該可以想像,如果我們手上的數據不是 <code>df</code> 而是前面的 <code>df.pivot_table</code> 的話,就無法輕易地用 Chartify 畫出這個散佈圖了。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<img src="https://leemeng.tw/images/chartify/tidy-vs-non-tidy.jpg" style="mix-blend-mode: initial;"/>
<br/>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>事實上,就算你不是用 Chartify 繪圖,Tidy 格式的數據也是比較推薦的。我們後面會看到,Tidy Data 可以讓你非常輕鬆地探索各個變數之間的關係。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Chartify-提醒你提供重要訊息">Chartify 提醒你提供重要訊息<a class="anchor-link" href="#Chartify-提醒你提供重要訊息">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>雖說「一張圖勝過千言萬語」,有時為了讓你的分析結果更容易被其他人理解、吸收,你需要加上以下訊息:</p>
<ul>
<li>圖表標題、副標</li>
<li>X、Y 軸標籤</li>
<li>圖片的數據來源</li>
</ul>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<img src="https://leemeng.tw/images/chartify/chart2.jpg" style="mix-blend-mode: initial;"/>
<br/>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><br/>
重新檢視剛剛的散佈圖,你會發現 Chartify 跟其他繪圖工具相比非常地貼心。它在對應的位置直接列出程式碼,告訴你每個位置的文字該怎麼修改。</p>
<p>比方說你想修改標題(title)成「水果單價與總價格關係」,只要按照上圖指示在 <code>ch.show()</code> 指令之前輸入:</p>
<div class="highlight"><pre><span></span><span class="n">ch</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'水果單價與總價格關係'</span><span class="p">)</span>
</pre></div>
<p>即可改變標題。</p>
<p>現在讓我們從善如流,依照 Chartify 的提示,將所有必要資訊填入看看:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">ch</span> <span class="o">=</span> <span class="n">chartify</span><span class="o">.</span><span class="n">Chart</span><span class="p">()</span>
<span class="n">ch</span><span class="o">.</span><span class="n">plot</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span>
<span class="n">data_frame</span><span class="o">=</span><span class="n">df</span><span class="p">,</span>
<span class="n">x_column</span><span class="o">=</span><span class="s1">'unit_price'</span><span class="p">,</span>
<span class="n">y_column</span><span class="o">=</span><span class="s1">'total_price'</span><span class="p">)</span>
<span class="c1"># 這部分程式碼都已經被寫在圖上</span>
<span class="c1"># 照抄即可:)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'水果單價與總價格關係'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">set_subtitle</span><span class="p">(</span><span class="s1">'Chartify 內建數據'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">set_source_label</span><span class="p">(</span><span class="s1">'Chartify'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">axes</span><span class="o">.</span><span class="n">set_xaxis_label</span><span class="p">(</span><span class="s1">'單價'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">axes</span><span class="o">.</span><span class="n">set_yaxis_label</span><span class="p">(</span><span class="s1">'總價格'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">show</span><span class="p">(</span><span class="s1">'png'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<img src="https://leemeng.tw/images/chartify/chart3.jpg" style="mix-blend-mode: initial;"/>
<br/>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><br/>
感覺充實許多了,不是嗎?</p>
<p>值得一提的是,現在數據科學家經常利用 <a href="http://jupyter.org/">Jupyter Notebook</a> 進行分析,而裡頭的程式碼及圖表<a href="https://www.kdnuggets.com/2018/11/best-practices-notebooks-data-science.html">常常在不同 Notebook 之間被搬來搬去</a>。為了讓你設計的圖表即使離開原來的 Notebook 也能讓人看得懂,加入如標題等資訊能大大提升圖表的可讀性。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="凡事有例外,或是懶了">凡事有例外,或是懶了<a class="anchor-link" href="#凡事有例外,或是懶了">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>雖然我強烈建議標題一定要有,但有時候如果畫的圖只是要給你自己看,或者是你不需要標註副標和資料來源時,則可以將 <code>Chart</code> 的 <code>blank_labels</code> 設定為 <code>True</code> 來隱藏所有文字:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">ch</span> <span class="o">=</span> <span class="n">chartify</span><span class="o">.</span><span class="n">Chart</span><span class="p">(</span>
<span class="n">blank_labels</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">plot</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span>
<span class="n">data_frame</span><span class="o">=</span><span class="n">df</span><span class="p">,</span>
<span class="n">x_column</span><span class="o">=</span><span class="s1">'unit_price'</span><span class="p">,</span>
<span class="n">y_column</span><span class="o">=</span><span class="s1">'total_price'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'水果單價與總價格關係'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">show</span><span class="p">(</span><span class="s1">'png'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<img src="https://leemeng.tw/images/chartify/chart4.jpg" style="mix-blend-mode: initial;"/>
<br/>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><br/>
貼心提示一鍵消失,看來其他人得猜猜到底水果單價是 X 還是 Y 軸了!</p>
<p>在對 Chartify 有了最基本的了解以後,讓我們以一些台灣的公開數據做點有趣的資料視覺化吧!</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="每年有多少人拜訪台灣?">每年有多少人拜訪台灣?<a class="anchor-link" href="#每年有多少人拜訪台灣?">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>台灣以風景、小吃以及人情味著名,但你曉得每年有多少人來台灣嗎?</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<img src="https://leemeng.tw/images/chartify/photo-1508248467877-aec1b08de376.jpg"/>
<br/>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>甚至再分細一點,在這些來台灣旅遊的人們當中,有多少是從歐洲來的呢?而又有多少是華僑呢?</p>
<p>為了回答這些問題,我們可以利用<a href="https://data.gov.tw/about">政府資料開放平臺</a>裡頭的<a href="https://data.gov.tw/dataset/45537">歷年來台旅客國籍統計</a>數據:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe table table-striped table-responsive">
<thead>
<tr style="text-align: right;">
<th></th>
<th>continent</th>
<th>year</th>
<th>visits</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>亞洲地區</td>
<td>2002</td>
<td>1689854</td>
</tr>
<tr>
<th>1</th>
<td>歐洲地區</td>
<td>2002</td>
<td>209110</td>
</tr>
<tr>
<th>2</th>
<td>美洲地區</td>
<td>2002</td>
<td>439403</td>
</tr>
<tr>
<th>3</th>
<td>華僑</td>
<td>2002</td>
<td>539164</td>
</tr>
<tr>
<th>4</th>
<td>大洋洲</td>
<td>2002</td>
<td>48334</td>
</tr>
<tr>
<th>5</th>
<td>非洲地區</td>
<td>2002</td>
<td>9479</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>上面這個 DataFrame 實際上包含了從 2002 到 2016 年各大洲及華僑的訪台總人次。</p>
<p>值得一提的是,如果你實際下載<a href="https://data.gov.tw/dataset/45537">該資料集</a>,會發現其格式很不 tidy。這個 DataFrame 事實上是經過大約 30 行 Pandas 的前處理而來,但為了讓你能專注在 Chartify 本身,在本文裡會省略所有資料前處理的說明。</p>
<p>接著讓我們以 Chartify 來視覺化此 DataFrame:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">ch</span> <span class="o">=</span> <span class="n">chartify</span><span class="o">.</span><span class="n">Chart</span><span class="p">(</span>
<span class="n">x_axis_type</span><span class="o">=</span><span class="s1">'datetime'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">plot</span><span class="o">.</span><span class="n">area</span><span class="p">(</span>
<span class="n">data_frame</span><span class="o">=</span><span class="n">df</span><span class="p">,</span>
<span class="n">x_column</span><span class="o">=</span><span class="s1">'year'</span><span class="p">,</span>
<span class="n">y_column</span><span class="o">=</span><span class="s1">'visits'</span><span class="p">,</span>
<span class="n">color_column</span><span class="o">=</span><span class="s1">'continent'</span><span class="p">,</span>
<span class="n">stacked</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="c1"># 所有繪圖工具都需要這些說明</span>
<span class="n">ch</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s1">'歷年各大洲及華僑來台旅客人次'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">set_subtitle</span><span class="p">(</span><span class="s1">'西元 2002 至 2016 年'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">set_source_label</span><span class="p">(</span><span class="s1">'政府資料開放平臺'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">axes</span><span class="o">.</span><span class="n">set_xaxis_label</span><span class="p">(</span><span class="s1">'西元年份'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">axes</span><span class="o">.</span><span class="n">set_yaxis_label</span><span class="p">(</span><span class="s1">'來台旅客人次'</span><span class="p">)</span>
<span class="n">ch</span><span class="o">.</span><span class="n">show</span><span class="p">(</span><span class="s1">'png'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<img src="https://leemeng.tw/images/chartify/chart5.jpg" style="mix-blend-mode: initial;"/>
<br/>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><br/>
你可以觀察到,自 2009 年起訪台人數成長加快,而在約 2014 年時來台旅客人次突破 1,000 萬。另外你可以看到華僑的訪台人數增幅十分顯著。</p>
<p>回到 Chartify 本身。</p>
<p>扣除你在不管使用什麼繪圖工具都需要設定的標題、X 與 Y 軸標籤,事實上我們只用了 1 個關鍵的 <code>ch.plot.area</code> 函式,就畫出這張幾乎已經不需再做「微整型」的疊加區域圖了。</p>
<p>讓我們再仔細檢視一下 <code>ch.plot.area</code>:</p>
<div class="highlight"><pre><span></span><span class="n">ch</span><span class="o">.</span><span class="n">plot</span><span class="o">.</span><span class="n">area</span><span class="p">(</span>
<span class="n">data_frame</span><span class="o">=</span><span class="n">df</span><span class="p">,</span>
<span class="n">x_column</span><span class="o">=</span><span class="s1">'year'</span><span class="p">,</span>
<span class="n">y_column</span><span class="o">=</span><span class="s1">'visits'</span><span class="p">,</span>
<span class="n">color_column</span><span class="o">=</span><span class="s1">'continent'</span><span class="p">,</span>
<span class="n">stacked</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
<p>如同上一個散佈圖 <code>ch.plot.scatter</code> 的例子,我們告訴 Chartify <code>df</code> 是我們的 DataFrame,並將年份變數 <code>year</code> 對應到 X 軸、訪客人數變數 <code>visits</code> 對應到 Y 軸。</p>
<p>接著我們告訴 Chartify,我們想要依照洲 <code>continent</code> 來畫不同「顏色」的子區域,並透過 <code>stacked=True</code> 來將這些子區域疊加起來。</p>
<p>另外因為我們的 X 軸是時間類型的變數(年份),為了幫助 Chartify 畫圖,我們在第一行設定 <code>Chart</code> 時需要將 <code>x_axis_type</code> 設定為 <code>datetime</code>:</p>
<pre><code>ch = chartify.Chart(
x_axis_type='datetime')</code></pre>
<p>自此大功告成,你只需要再輸入 <code>ch.show()</code>,美麗的疊加區域圖自動產生。不需要再花時間調整一大堆瑣碎的樣式(style)就能將結果跟別人分享,正是 Chartify 強大與貼心之處。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<img src="https://leemeng.tw/images/chartify/chart5.jpg" style="mix-blend-mode: initial;"/>
<br/>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="一個參數就能轉變圖表">一個參數就能轉變圖表<a class="anchor-link" href="#一個參數就能轉變圖表">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>當然,我們也可以選擇不將各大洲的區域疊加起來,讓它們從同樣的基準點渲染。</p>
<p>這時只需要將上張圖的 <code>ch.plot.area</code> 函式裡頭的 <code>stacked=True</code> 拿掉即可:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<img src="https://leemeng.tw/images/chartify/chart6.jpg" style="mix-blend-mode: initial;"/>
<br/>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><br/>
各自渲染的區域圖讓我們很清楚地看到:從 2012 年開始,光是華僑的訪台人數就超越整個亞洲訪客人次,成為來台旅客的最主要來源,佔了將近一半。</p>
<p>回到 Chartify 本身,你應該已經發現自己甚至不需花時間來手動調整各個子區域的透明度(alpha),省了不少時間。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="所以大家來台灣做什麼?">所以大家來台灣做什麼?<a class="anchor-link" href="#所以大家來台灣做什麼?">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>我們剛剛利用區域圖(Area Plot)觀察了歷年各大洲訪台人數的變化,你會不會好奇他們都來這邊做什麼?</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<img src="https://leemeng.tw/images/chartify/andrew-haimerl-526180-unsplash.jpg"/>
<br/>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>至少我是蠻好奇的。這次一樣讓我們從<a href="https://data.gov.tw/about">政府資料開放平臺</a>取得數據。<a href="https://data.gov.tw/dataset/45512">歷年來台旅客來台目的統計</a>看起來是一個不錯的選擇:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span>
</pre></div>
</div>