-
Notifications
You must be signed in to change notification settings - Fork 808
/
README.html
1940 lines (1864 loc) · 108 KB
/
README.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
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>Matplotlib tutorial</title>
<link rel="stylesheet" href="dana.css" type="text/css" />
</head>
<body>
<div class="document" id="matplotlib-tutorial">
<h1 class="title">Matplotlib tutorial</h1>
<h2 class="subtitle" id="nicolas-p-rougier">Nicolas P. Rougier</h2>
<a class="reference external image-reference" href="http://dx.doi.org/10.5281/zenodo.28747"><object data="https://zenodo.org/badge/doi/10.5281/zenodo.28747.svg" type="image/svg+xml">https://zenodo.org/badge/doi/10.5281/zenodo.28747.svg</object></a>
<div class="contents local topic" id="table-of-contents">
<p class="topic-title">Table of Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#introduction" id="id5">Introduction</a></li>
<li><a class="reference internal" href="#simple-plot" id="id6">Simple plot</a></li>
<li><a class="reference internal" href="#figures-subplots-axes-and-ticks" id="id7">Figures, Subplots, Axes and Ticks</a></li>
<li><a class="reference internal" href="#animation" id="id8">Animation</a></li>
<li><a class="reference internal" href="#other-types-of-plots" id="id9">Other Types of Plots</a></li>
<li><a class="reference internal" href="#beyond-this-tutorial" id="id10">Beyond this tutorial</a></li>
<li><a class="reference internal" href="#quick-references" id="id11">Quick references</a></li>
</ul>
</div>
<p>Sources are available from
<a class="reference external" href="https://github.com/rougier/matplotlib-tutorial">github</a></p>
<p>All code and material is licensed under a <a class="reference external" href="http://creativecommons.org/licenses/by-sa/4.0">Creative Commons
Attribution-ShareAlike 4.0</a>.</p>
<p>You can test your installation before the tutorial using the <a class="reference external" href="scripts/check-installation.py">check-installation.py</a> script.</p>
<p>See also:</p>
<ul class="simple">
<li><a class="reference external" href="http://www.labri.fr/perso/nrougier/from-python-to-numpy/">From Python to Numpy</a></li>
<li><a class="reference external" href="https://github.com/rougier/numpy-100">100 Numpy exercices</a></li>
<li><a class="reference external" href="http://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1003833">Ten simple rules for better figures</a></li>
</ul>
<div class="section" id="introduction">
<h1><a class="toc-backref" href="#id5">Introduction</a></h1>
<p>matplotlib is probably the single most used Python package for 2D-graphics. It
provides both a very quick way to visualize data from Python and
publication-quality figures in many formats. We are going to explore
matplotlib in interactive mode covering most common cases.</p>
<div class="section" id="ipython">
<h2>IPython</h2>
<p><a class="reference external" href="http://ipython.org/">IPython</a> is an enhanced interactive Python shell that
has lots of interesting features including named inputs and outputs, access to
shell commands, improved debugging and much more. It allows
interactive matplotlib sessions that have Matlab/Mathematica-like functionality.</p>
</div>
<div class="section" id="pyplot">
<h2>pyplot</h2>
<p>pyplot provides a convenient interface to the matplotlib object-oriented
plotting library. It is modeled closely after Matlab(TM). Therefore, the
majority of plotting commands in pyplot have Matlab(TM) analogs with similar
arguments. Important commands are explained with interactive examples.</p>
</div>
</div>
<div class="section" id="simple-plot">
<h1><a class="toc-backref" href="#id6">Simple plot</a></h1>
<p>In this section, we want to draw the cosine and sine functions on the same
plot. Starting from the default settings, we'll enrich the figure step by step
to make it nicer.</p>
<p>The first step is to get the data for the sine and cosine functions:</p>
<pre class="literal-block">
import numpy as np
X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C, S = np.cos(X), np.sin(X)
</pre>
<p>X is now a NumPy array with 256 values ranging from -π to +π (included). C is
the cosine (256 values) and S is the sine (256 values).</p>
<p>To run the example, you can download each of the examples and run it using:</p>
<pre class="literal-block">
$ python exercice_1.py
</pre>
<p>You can get source for each step by clicking on the corresponding figure.</p>
<div class="section" id="using-defaults">
<h2>Using defaults</h2>
<div class="admonition admonition-documentation">
<p class="first admonition-title">Documentation</p>
<ul class="last simple">
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/pyplot_tutorial.html">plot tutorial</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot">plot() command</a></li>
</ul>
</div>
<a class="reference external image-reference" href="scripts/exercice_1.py"><img alt="figures/exercice_1.png" class="align-right" src="figures/exercice_1.png" /></a>
<p>Matplotlib comes with a set of default settings that allow customizing all
kinds of properties. You can control the defaults of almost every property in
matplotlib: figure size and dpi, line width, color and style, axes, axis and
grid properties, text and font properties and so on. While matplotlib defaults
are rather good in most cases, you may want to modify some properties for
specific cases.</p>
<pre class="code python literal-block">
<span class="keyword namespace">import</span> <span class="name namespace">numpy</span> <span class="keyword">as</span> <span class="name namespace">np</span>
<span class="keyword namespace">import</span> <span class="name namespace">matplotlib.pyplot</span> <span class="keyword">as</span> <span class="name namespace">plt</span>
<span class="name">X</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">linspace</span><span class="punctuation">(</span><span class="operator">-</span><span class="name">np</span><span class="operator">.</span><span class="name">pi</span><span class="punctuation">,</span> <span class="name">np</span><span class="operator">.</span><span class="name">pi</span><span class="punctuation">,</span> <span class="literal number integer">256</span><span class="punctuation">,</span> <span class="name">endpoint</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">)</span>
<span class="name">C</span><span class="punctuation">,</span><span class="name">S</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">cos</span><span class="punctuation">(</span><span class="name">X</span><span class="punctuation">),</span> <span class="name">np</span><span class="operator">.</span><span class="name">sin</span><span class="punctuation">(</span><span class="name">X</span><span class="punctuation">)</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">plot</span><span class="punctuation">(</span><span class="name">X</span><span class="punctuation">,</span><span class="name">C</span><span class="punctuation">)</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">plot</span><span class="punctuation">(</span><span class="name">X</span><span class="punctuation">,</span><span class="name">S</span><span class="punctuation">)</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">show</span><span class="punctuation">()</span>
</pre>
</div>
<div class="section" id="instantiating-defaults">
<h2>Instantiating defaults</h2>
<div class="admonition admonition-documentation">
<p class="first admonition-title">Documentation</p>
<ul class="last simple">
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/customizing.html">Customizing matplotlib</a></li>
</ul>
</div>
<a class="reference external image-reference" href="scripts/exercice_2.py"><img alt="figures/exercice_2.png" class="align-right" src="figures/exercice_2.png" /></a>
<p>In the script below, we've instantiated (and commented) all the figure settings
that influence the appearance of the plot. The settings have been explicitly
set to their default values, but now you can interactively play with the values
to explore their affect (see <a class="reference internal" href="#line-properties">Line properties</a> and <a class="reference internal" href="#line-styles">Line styles</a> below).</p>
<pre class="code python literal-block">
<span class="comment single"># Imports</span>
<span class="keyword namespace">import</span> <span class="name namespace">numpy</span> <span class="keyword">as</span> <span class="name namespace">np</span>
<span class="keyword namespace">import</span> <span class="name namespace">matplotlib.pyplot</span> <span class="keyword">as</span> <span class="name namespace">plt</span>
<span class="comment single"># Create a new figure of size 8x6 points, using 100 dots per inch</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">figure</span><span class="punctuation">(</span><span class="name">figsize</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">8</span><span class="punctuation">,</span><span class="literal number integer">6</span><span class="punctuation">),</span> <span class="name">dpi</span><span class="operator">=</span><span class="literal number integer">100</span><span class="punctuation">)</span>
<span class="comment single"># Create a new subplot from a grid of 1x1</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">subplot</span><span class="punctuation">(</span><span class="literal number integer">111</span><span class="punctuation">)</span>
<span class="name">X</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">linspace</span><span class="punctuation">(</span><span class="operator">-</span><span class="name">np</span><span class="operator">.</span><span class="name">pi</span><span class="punctuation">,</span> <span class="name">np</span><span class="operator">.</span><span class="name">pi</span><span class="punctuation">,</span> <span class="literal number integer">256</span><span class="punctuation">,</span><span class="name">endpoint</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">)</span>
<span class="name">C</span><span class="punctuation">,</span><span class="name">S</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">cos</span><span class="punctuation">(</span><span class="name">X</span><span class="punctuation">),</span> <span class="name">np</span><span class="operator">.</span><span class="name">sin</span><span class="punctuation">(</span><span class="name">X</span><span class="punctuation">)</span>
<span class="comment single"># Plot cosine using blue color with a continuous line of width 1 (pixels)</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">plot</span><span class="punctuation">(</span><span class="name">X</span><span class="punctuation">,</span> <span class="name">C</span><span class="punctuation">,</span> <span class="name">color</span><span class="operator">=</span><span class="literal string double">"blue"</span><span class="punctuation">,</span> <span class="name">linewidth</span><span class="operator">=</span><span class="literal number float">1.0</span><span class="punctuation">,</span> <span class="name">linestyle</span><span class="operator">=</span><span class="literal string double">"-"</span><span class="punctuation">)</span>
<span class="comment single"># Plot sine using green color with a continuous line of width 1 (pixels)</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">plot</span><span class="punctuation">(</span><span class="name">X</span><span class="punctuation">,</span> <span class="name">S</span><span class="punctuation">,</span> <span class="name">color</span><span class="operator">=</span><span class="literal string double">"green"</span><span class="punctuation">,</span> <span class="name">linewidth</span><span class="operator">=</span><span class="literal number float">1.0</span><span class="punctuation">,</span> <span class="name">linestyle</span><span class="operator">=</span><span class="literal string double">"-"</span><span class="punctuation">)</span>
<span class="comment single"># Set x limits</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">xlim</span><span class="punctuation">(</span><span class="operator">-</span><span class="literal number float">4.0</span><span class="punctuation">,</span><span class="literal number float">4.0</span><span class="punctuation">)</span>
<span class="comment single"># Set x ticks</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">xticks</span><span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">linspace</span><span class="punctuation">(</span><span class="operator">-</span><span class="literal number integer">4</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">,</span><span class="literal number integer">9</span><span class="punctuation">,</span><span class="name">endpoint</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">))</span>
<span class="comment single"># Set y limits</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">ylim</span><span class="punctuation">(</span><span class="operator">-</span><span class="literal number float">1.0</span><span class="punctuation">,</span><span class="literal number float">1.0</span><span class="punctuation">)</span>
<span class="comment single"># Set y ticks</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">yticks</span><span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">linspace</span><span class="punctuation">(</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="name">endpoint</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">))</span>
<span class="comment single"># Save figure using 72 dots per inch</span>
<span class="comment single"># savefig("../figures/exercice_2.png",dpi=72)</span>
<span class="comment single"># Show result on screen</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">show</span><span class="punctuation">()</span>
</pre>
</div>
<div class="section" id="changing-colors-and-line-widths">
<h2>Changing colors and line widths</h2>
<div class="admonition admonition-documentation">
<p class="first admonition-title">Documentation</p>
<ul class="last simple">
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/pyplot_tutorial.html#controlling-line-properties">Controlling line properties</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/artist_api.html#matplotlib.lines.Line2D">Line API</a></li>
</ul>
</div>
<a class="reference external image-reference" href="scripts/exercice_3.py"><img alt="figures/exercice_3.png" class="align-right" src="figures/exercice_3.png" /></a>
<p>As a first step, we want to have the cosine in blue and the sine in red and a
slightly thicker line for both of them. We'll also slightly alter the figure
size to make it more horizontal.</p>
<pre class="literal-block">
...
plt.figure(figsize=(10,6), dpi=80)
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-")
...
</pre>
</div>
<div class="section" id="setting-limits">
<h2>Setting limits</h2>
<div class="admonition admonition-documentation">
<p class="first admonition-title">Documentation</p>
<ul class="last simple">
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.xlim">xlim() command</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.ylim">ylim() command</a></li>
</ul>
</div>
<a class="reference external image-reference" href="scripts/exercice_4.py"><img alt="figures/exercice_4.png" class="align-right" src="figures/exercice_4.png" /></a>
<p>Current limits of the figure are a bit too tight and we want to make some space
in order to clearly see all data points.</p>
<pre class="literal-block">
...
plt.xlim(X.min()*1.1, X.max()*1.1)
plt.ylim(C.min()*1.1, C.max()*1.1)
...
</pre>
</div>
<div class="section" id="setting-ticks">
<h2>Setting ticks</h2>
<div class="admonition admonition-documentation">
<p class="first admonition-title">Documentation</p>
<ul class="last simple">
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.xticks">xticks() command</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.yticks">yticks() command</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/artists.html#axis-container">Tick container</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/ticker_api.html">Tick locating and formatting</a></li>
</ul>
</div>
<a class="reference external image-reference" href="scripts/exercice_5.py"><img alt="figures/exercice_5.png" class="align-right" src="figures/exercice_5.png" /></a>
<p>Current ticks are not ideal because they do not show the interesting values
(+/-π,+/-π/2) for sine and cosine. We'll change them such that they show only
these values.</p>
<pre class="literal-block">
...
plt.xticks( [-np.pi, -np.pi/2, 0, np.pi/2, np.pi])
plt.yticks([-1, 0, +1])
...
</pre>
</div>
<div class="section" id="setting-tick-labels">
<h2>Setting tick labels</h2>
<div class="admonition admonition-documentation">
<p class="first admonition-title">Documentation</p>
<ul class="last simple">
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/index_text.html">Working with text</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.xticks">xticks() command</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.yticks">yticks() command</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/axes_api.html?#matplotlib.axes.Axes.set_xticklabels">set_xticklabels()</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/axes_api.html?#matplotlib.axes.Axes.set_yticklabels">set_yticklabels()</a></li>
</ul>
</div>
<a class="reference external image-reference" href="scripts/exercice_6.py"><img alt="figures/exercice_6.png" class="align-right" src="figures/exercice_6.png" /></a>
<p>Ticks are now properly placed but their label is not very explicit. We could
guess that 3.142 is π but it would be better to make it explicit. When we set
tick values, we can also provide a corresponding label in the second argument
list. Note that we'll use latex to allow for nice rendering of the label.</p>
<pre class="literal-block">
...
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
plt.yticks([-1, 0, +1],
[r'$-1$', r'$0$', r'$+1$'])
...
</pre>
</div>
<div class="section" id="moving-spines">
<h2>Moving spines</h2>
<div class="admonition admonition-documentation">
<p class="first admonition-title">Documentation</p>
<ul class="last simple">
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/spines_api.html#matplotlib.spines">Spines</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/artists.html#axis-container">Axis container</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/transforms_tutorial.html">Transformations tutorial</a></li>
</ul>
</div>
<a class="reference external image-reference" href="scripts/exercice_7.py"><img alt="figures/exercice_7.png" class="align-right" src="figures/exercice_7.png" /></a>
<p>Spines are the lines connecting the axis tick marks and noting the boundaries
of the data area. They can be placed at arbitrary positions and until now, they
were on the border of the axis. We'll change that since we want to have them in
the middle. Since there are four of them (top/bottom/left/right), we'll discard
the top and right by setting their color to none and we'll move the bottom and
left ones to coordinate 0 in data space coordinates.</p>
<pre class="literal-block">
...
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
...
</pre>
</div>
<div class="section" id="adding-a-legend">
<h2>Adding a legend</h2>
<div class="admonition admonition-documentation">
<p class="first admonition-title">Documentation</p>
<ul class="last simple">
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/legend_guide.html">Legend guide</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.legend">legend() command</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/legend_api.html#matplotlib.legend.Legend">Legend API</a></li>
</ul>
</div>
<a class="reference external image-reference" href="scripts/exercice_8.py"><img alt="figures/exercice_8.png" class="align-right" src="figures/exercice_8.png" /></a>
<p>Let's add a legend in the upper left corner. This only requires adding the
keyword argument label (that will be used in the legend box) to the plot
commands.</p>
<pre class="literal-block">
...
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sine")
plt.legend(loc='upper left', frameon=False)
...
</pre>
</div>
<div class="section" id="annotate-some-points">
<h2>Annotate some points</h2>
<div class="admonition admonition-documentation">
<p class="first admonition-title">Documentation</p>
<ul class="last simple">
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/annotations_guide.html">Annotating axis</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.annotate">annotate() command</a></li>
</ul>
</div>
<a class="reference external image-reference" href="scripts/exercice_9.py"><img alt="figures/exercice_9.png" class="align-right" src="figures/exercice_9.png" /></a>
<p>Let's annotate some interesting points using the annotate command. We choose the
2π/3 value and we want to annotate both the sine and the cosine. We'll first
draw a marker on the curve as well as a straight dotted line. Then, we'll use
the annotate command to display some text with an arrow.</p>
<pre class="literal-block">
...
t = 2*np.pi/3
plt.plot([t,t],[0,np.cos(t)], color ='blue', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.cos(t),], 50, color ='blue')
plt.annotate(r'$\sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
xy=(t, np.sin(t)), xycoords='data',
xytext=(+10, +30), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.plot([t,t],[0,np.sin(t)], color ='red', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.sin(t),], 50, color ='red')
plt.annotate(r'$\cos(\frac{2\pi}{3})=-\frac{1}{2}$',
xy=(t, np.cos(t)), xycoords='data',
xytext=(-90, -50), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
...
</pre>
</div>
<div class="section" id="devil-is-in-the-details">
<h2>Devil is in the details</h2>
<div class="admonition admonition-documentation">
<p class="first admonition-title">Documentation</p>
<ul class="last simple">
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/artist_api.html">Artists</a></li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/artist_api.html#matplotlib.text.Text.set_bbox">BBox</a></li>
</ul>
</div>
<a class="reference external image-reference" href="scripts/exercice_10.py"><img alt="figures/exercice_10.png" class="align-right" src="figures/exercice_10.png" /></a>
<p>The tick labels are now hardly visible because of the blue and red lines. We can
make them bigger and we can also adjust their properties such that they'll be
rendered on a semi-transparent white background. This will allow us to see both
the data and the labels.</p>
<pre class="literal-block">
...
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(16)
label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65 ))
...
</pre>
</div>
</div>
<div class="section" id="figures-subplots-axes-and-ticks">
<h1><a class="toc-backref" href="#id7">Figures, Subplots, Axes and Ticks</a></h1>
<p>So far we have used implicit figure and axes creation. This is handy for fast
plots. We can have more control over the display using figure, subplot, and
axes explicitly. A figure in matplotlib means the whole window in the user
interface. Within this figure there can be subplots. While subplot positions
the plots in a regular grid, axes allows free placement within the figure. Both
can be useful depending on your intention. We've already worked with figures
and subplots without explicitly calling them. When we call plot, matplotlib
calls gca() to get the current axes and gca in turn calls gcf() to get the
current figure. If there is none it calls figure() to make one, strictly
speaking, to make a subplot(111). Let's look at the details.</p>
<div class="section" id="figures">
<h2>Figures</h2>
<p>A figure is the windows in the GUI that has "Figure #" as title. Figures
are numbered starting from 1 as opposed to the normal Python way starting
from 0. This is clearly MATLAB-style. There are several parameters that
determine what the figure looks like:</p>
<table border="1" class="docutils">
<colgroup>
<col width="17%" />
<col width="28%" />
<col width="54%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Argument</th>
<th class="head">Default</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>num</td>
<td>1</td>
<td>number of figure</td>
</tr>
<tr><td>figsize</td>
<td>figure.figsize</td>
<td>figure size in in inches (width, height)</td>
</tr>
<tr><td>dpi</td>
<td>figure.dpi</td>
<td>resolution in dots per inch</td>
</tr>
<tr><td>facecolor</td>
<td>figure.facecolor</td>
<td>color of the drawing background</td>
</tr>
<tr><td>edgecolor</td>
<td>figure.edgecolor</td>
<td>color of edge around the drawing background</td>
</tr>
<tr><td>frameon</td>
<td>True</td>
<td>draw figure frame or not</td>
</tr>
</tbody>
</table>
<p>The defaults can be specified in the resource file and will be used most of the
time. Only the number of the figure is frequently changed.</p>
<p>When you work with the GUI you can close a figure by clicking on the x in the
upper right corner. You can also close a figure programmatically by calling
close. Depending on the argument it closes (1) the current figure (no
argument), (2) a specific figure (figure number or figure instance as
argument), or (3) all figures (all as argument).</p>
<p>As with other objects, you can set figure properties with the set_something methods.</p>
</div>
<div class="section" id="subplots">
<h2>Subplots</h2>
<p>With subplot you can arrange plots in a regular grid. You need to specify the
number of rows and columns and the number of the plot. Note that the <a class="reference external" href="http://matplotlib.sourceforge.net/users/gridspec.html">gridspec</a> command is a more
powerful alternative.</p>
<a class="reference external image-reference" href="scripts/subplot-horizontal.py"><img alt="figures/subplot-horizontal.png" src="figures/subplot-horizontal.png" /></a>
<a class="reference external image-reference" href="scripts/subplot-vertical.py"><img alt="figures/subplot-vertical.png" src="figures/subplot-vertical.png" /></a>
<a class="reference external image-reference" href="scripts/subplot-grid.py"><img alt="figures/subplot-grid.png" src="figures/subplot-grid.png" /></a>
<a class="reference external image-reference" href="scripts/gridspec.py"><img alt="figures/gridspec.png" src="figures/gridspec.png" /></a>
</div>
<div class="section" id="axes">
<h2>Axes</h2>
<p>Axes are very similar to subplots but allow placement of plots at any location
in the figure. So if we want to put a smaller plot inside a bigger one we do
so with axes.</p>
<a class="reference external image-reference" href="scripts/axes.py"><img alt="figures/axes.png" src="figures/axes.png" /></a>
<a class="reference external image-reference" href="scripts/axes-2.py"><img alt="figures/axes-2.png" src="figures/axes-2.png" /></a>
</div>
<div class="section" id="ticks">
<h2>Ticks</h2>
<p>Well formatted ticks are an important part of publishing-ready
figures. Matplotlib provides a totally configurable system for ticks. There are
tick locators to specify where ticks should appear and tick formatters to give
ticks the appearance you want. Major and minor ticks can be located and
formatted independently from each other. By default minor ticks are not shown,
i.e. there is only an empty list for them because it is as NullLocator (see
below).</p>
<div class="section" id="tick-locators">
<h3>Tick Locators</h3>
<p>There are several locators for different kind of requirements:</p>
<table border="1" class="colwidths-given docutils">
<colgroup>
<col width="22%" />
<col width="78%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Class</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal">NullLocator</tt></td>
<td><p class="first">No ticks.</p>
<img alt="figures/ticks-NullLocator.png" class="last" src="figures/ticks-NullLocator.png" />
</td>
</tr>
<tr><td><tt class="docutils literal">IndexLocator</tt></td>
<td><p class="first">Place a tick on every multiple of some base number of points plotted.</p>
<img alt="figures/ticks-IndexLocator.png" class="last" src="figures/ticks-IndexLocator.png" />
</td>
</tr>
<tr><td><tt class="docutils literal">FixedLocator</tt></td>
<td><p class="first">Tick locations are fixed.</p>
<img alt="figures/ticks-FixedLocator.png" class="last" src="figures/ticks-FixedLocator.png" />
</td>
</tr>
<tr><td><tt class="docutils literal">LinearLocator</tt></td>
<td><p class="first">Determine the tick locations.</p>
<img alt="figures/ticks-LinearLocator.png" class="last" src="figures/ticks-LinearLocator.png" />
</td>
</tr>
<tr><td><tt class="docutils literal">MultipleLocator</tt></td>
<td><p class="first">Set a tick on every integer that is multiple of some base.</p>
<img alt="figures/ticks-MultipleLocator.png" class="last" src="figures/ticks-MultipleLocator.png" />
</td>
</tr>
<tr><td><tt class="docutils literal">AutoLocator</tt></td>
<td><p class="first">Select no more than n intervals at nice locations.</p>
<img alt="figures/ticks-AutoLocator.png" class="last" src="figures/ticks-AutoLocator.png" />
</td>
</tr>
<tr><td><tt class="docutils literal">LogLocator</tt></td>
<td><p class="first">Determine the tick locations for log axes.</p>
<img alt="figures/ticks-LogLocator.png" class="last" src="figures/ticks-LogLocator.png" />
</td>
</tr>
</tbody>
</table>
<p>All of these locators derive from the base class matplotlib.ticker.Locator.
You can make your own locator deriving from it. Handling dates as ticks can be
especially tricky. Therefore, matplotlib provides special locators in
matplotlib.dates.</p>
</div>
</div>
</div>
<div class="section" id="animation">
<h1><a class="toc-backref" href="#id8">Animation</a></h1>
<p>For quite a long time, animation in matplotlib was not an easy task and was
done mainly through clever hacks. However, things have started to change since
version 1.1 and the introduction of tools for creating animation very
intuitively, with the possibility to save them in all kind of formats (but don't
expect to be able to run very complex animations at 60 fps though).</p>
<div class="admonition admonition-documentation">
<p class="first admonition-title">Documentation</p>
<ul class="last simple">
<li>See <a class="reference external" href="http://matplotlib.org/api/animation_api.html">Animation</a></li>
</ul>
</div>
<p>The most easy way to make an animation in matplotlib is to declare a
FuncAnimation object that specifies to matplotlib what is the figure to
update, what is the update function and what is the delay between frames.</p>
<div class="section" id="drip-drop">
<h2>Drip drop</h2>
<p>A very simple rain effect can be obtained by having small growing rings
randomly positioned over a figure. Of course, they won't grow forever since the
wave is supposed to damp with time. To simulate that, we can use a more and
more transparent color as the ring is growing, up to the point where it is no
more visible. At this point, we remove the ring and create a new one.</p>
<p>First step is to create a blank figure:</p>
<pre class="code python literal-block">
<span class="comment single"># New figure with white background</span>
<span class="name">fig</span> <span class="operator">=</span> <span class="name">plt</span><span class="operator">.</span><span class="name">figure</span><span class="punctuation">(</span><span class="name">figsize</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">6</span><span class="punctuation">,</span><span class="literal number integer">6</span><span class="punctuation">),</span> <span class="name">facecolor</span><span class="operator">=</span><span class="literal string single">'white'</span><span class="punctuation">)</span>
<span class="comment single"># New axis over the whole figure, no frame and a 1:1 aspect ratio</span>
<span class="name">ax</span> <span class="operator">=</span> <span class="name">fig</span><span class="operator">.</span><span class="name">add_axes</span><span class="punctuation">([</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">],</span> <span class="name">frameon</span><span class="operator">=</span><span class="keyword constant">False</span><span class="punctuation">,</span> <span class="name">aspect</span><span class="operator">=</span><span class="literal number integer">1</span><span class="punctuation">)</span>
</pre>
<p>Next, we need to create several rings. For this, we can use the scatter plot
object that is generally used to visualize points cloud, but we can also use it
to draw rings by specifying we don't have a facecolor. We also have to take
care of initial size and color for each ring such that we have all sizes between
a minimum and a maximum size. In addition, we need to make sure the largest ring
is almost transparent.</p>
<a class="reference external image-reference" href="scripts/rain-static.py"><img alt="figures/rain-static.png" class="align-right" src="figures/rain-static.png" /></a>
<pre class="code python literal-block">
<span class="comment single"># Number of ring</span>
<span class="name">n</span> <span class="operator">=</span> <span class="literal number integer">50</span>
<span class="name">size_min</span> <span class="operator">=</span> <span class="literal number integer">50</span>
<span class="name">size_max</span> <span class="operator">=</span> <span class="literal number integer">50</span><span class="operator">*</span><span class="literal number integer">50</span>
<span class="comment single"># Ring position</span>
<span class="name">P</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">uniform</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,(</span><span class="name">n</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">))</span>
<span class="comment single"># Ring colors</span>
<span class="name">C</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">ones</span><span class="punctuation">((</span><span class="name">n</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">))</span> <span class="operator">*</span> <span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="comment single"># Alpha color channel goes from 0 (transparent) to 1 (opaque)</span>
<span class="name">C</span><span class="punctuation">[:,</span><span class="literal number integer">3</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">linspace</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="name">n</span><span class="punctuation">)</span>
<span class="comment single"># Ring sizes</span>
<span class="name">S</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">linspace</span><span class="punctuation">(</span><span class="name">size_min</span><span class="punctuation">,</span> <span class="name">size_max</span><span class="punctuation">,</span> <span class="name">n</span><span class="punctuation">)</span>
<span class="comment single"># Scatter plot</span>
<span class="name">scat</span> <span class="operator">=</span> <span class="name">ax</span><span class="operator">.</span><span class="name">scatter</span><span class="punctuation">(</span><span class="name">P</span><span class="punctuation">[:,</span><span class="literal number integer">0</span><span class="punctuation">],</span> <span class="name">P</span><span class="punctuation">[:,</span><span class="literal number integer">1</span><span class="punctuation">],</span> <span class="name">s</span><span class="operator">=</span><span class="name">S</span><span class="punctuation">,</span> <span class="name">lw</span> <span class="operator">=</span> <span class="literal number float">0.5</span><span class="punctuation">,</span>
<span class="name">edgecolors</span> <span class="operator">=</span> <span class="name">C</span><span class="punctuation">,</span> <span class="name">facecolors</span><span class="operator">=</span><span class="literal string single">'None'</span><span class="punctuation">)</span>
<span class="comment single"># Ensure limits are [0,1] and remove ticks</span>
<span class="name">ax</span><span class="operator">.</span><span class="name">set_xlim</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">),</span> <span class="name">ax</span><span class="operator">.</span><span class="name">set_xticks</span><span class="punctuation">([])</span>
<span class="name">ax</span><span class="operator">.</span><span class="name">set_ylim</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">),</span> <span class="name">ax</span><span class="operator">.</span><span class="name">set_yticks</span><span class="punctuation">([])</span>
</pre>
<p>Now, we need to write the update function for our animation. We know that at
each time step each ring should grow and become more transparent while the
largest ring should be totally transparent and thus removed. Of course, we won't
actually remove the largest ring but re-use it to set a new ring at a new random
position, with nominal size and color. Hence, we keep the number of rings
constant.</p>
<a class="reference external image-reference" href="scripts/rain-dynamic.py"><img alt="figures/rain.gif" class="align-right" src="figures/rain.gif" /></a>
<pre class="code python literal-block">
<span class="keyword">def</span> <span class="name function">update</span><span class="punctuation">(</span><span class="name">frame</span><span class="punctuation">):</span>
<span class="keyword">global</span> <span class="name">P</span><span class="punctuation">,</span> <span class="name">C</span><span class="punctuation">,</span> <span class="name">S</span>
<span class="comment single"># Every ring is made more transparent</span>
<span class="name">C</span><span class="punctuation">[:,</span><span class="literal number integer">3</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">maximum</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="name">C</span><span class="punctuation">[:,</span><span class="literal number integer">3</span><span class="punctuation">]</span> <span class="operator">-</span> <span class="literal number float">1.0</span><span class="operator">/</span><span class="name">n</span><span class="punctuation">)</span>
<span class="comment single"># Each ring is made larger</span>
<span class="name">S</span> <span class="operator">+=</span> <span class="punctuation">(</span><span class="name">size_max</span> <span class="operator">-</span> <span class="name">size_min</span><span class="punctuation">)</span> <span class="operator">/</span> <span class="name">n</span>
<span class="comment single"># Reset ring specific ring (relative to frame number)</span>
<span class="name">i</span> <span class="operator">=</span> <span class="name">frame</span> <span class="operator">%</span> <span class="literal number integer">50</span>
<span class="name">P</span><span class="punctuation">[</span><span class="name">i</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">uniform</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">)</span>
<span class="name">S</span><span class="punctuation">[</span><span class="name">i</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name">size_min</span>
<span class="name">C</span><span class="punctuation">[</span><span class="name">i</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">1</span>
<span class="comment single"># Update scatter object</span>
<span class="name">scat</span><span class="operator">.</span><span class="name">set_edgecolors</span><span class="punctuation">(</span><span class="name">C</span><span class="punctuation">)</span>
<span class="name">scat</span><span class="operator">.</span><span class="name">set_sizes</span><span class="punctuation">(</span><span class="name">S</span><span class="punctuation">)</span>
<span class="name">scat</span><span class="operator">.</span><span class="name">set_offsets</span><span class="punctuation">(</span><span class="name">P</span><span class="punctuation">)</span>
<span class="comment single"># Return the modified object</span>
<span class="keyword">return</span> <span class="name">scat</span><span class="punctuation">,</span>
</pre>
<p>Last step is to tell matplotlib to use this function as an update function for
the animation and display the result or save it as a movie:</p>
<pre class="code python literal-block">
<span class="name">animation</span> <span class="operator">=</span> <span class="name">FuncAnimation</span><span class="punctuation">(</span><span class="name">fig</span><span class="punctuation">,</span> <span class="name">update</span><span class="punctuation">,</span> <span class="name">interval</span><span class="operator">=</span><span class="literal number integer">10</span><span class="punctuation">,</span> <span class="name">blit</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">,</span> <span class="name">frames</span><span class="operator">=</span><span class="literal number integer">200</span><span class="punctuation">)</span>
<span class="comment single"># animation.save('rain.gif', writer='imagemagick', fps=30, dpi=40)</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">show</span><span class="punctuation">()</span>
</pre>
<p>If you use IPython, you'll have to render the animation into an html video
in order to show it in the Jupyter notebook:</p>
<pre class="code python literal-block">
<span class="keyword namespace">from</span> <span class="name namespace">IPython.display</span> <span class="keyword namespace">import</span> <span class="name">HTML</span>
<span class="name">HTML</span><span class="punctuation">(</span><span class="name">animation</span><span class="operator">.</span><span class="name">to_html5_video</span><span class="punctuation">())</span>
</pre>
</div>
<div class="section" id="earthquakes">
<h2>Earthquakes</h2>
<p>We'll now use the rain animation to visualize earthquakes on the planet from
the last 30 days. The USGS Earthquake Hazards Program is part of the National
Earthquake Hazards Reduction Program (NEHRP) and provides several data on their
<a class="reference external" href="https://earthquake.usgs.gov">website</a>. Those data are sorted according to
earthquakes magnitude, ranging from significant only down to all earthquakes,
major or minor. You would be surprised by the number of minor earthquakes
happening every hour on the planet. Since this would represent too much data
for us, we'll stick to earthquakes with magnitude > 4.5. At the time of writing,
this already represent more than 300 earthquakes in the last 30 days.</p>
<p>First step is to read and convert data. We'll use the <cite>urllib</cite> library that
allows us to open and read remote data. Data on the website use the <cite>CSV</cite> format
whose content is given by the first line:</p>
<pre class="literal-block">
time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net,id,updated,place,type
2015-08-17T13:49:17.320Z,37.8365,-122.2321667,4.82,4.01,mw,...
2015-08-15T07:47:06.640Z,-10.9045,163.8766,6.35,6.6,mwp,...
</pre>
<p>We are only interested in latitude, longitude and magnitude and we won't parse
time of event (ok, that's bad, feel free to send me a PR).</p>
<pre class="code python literal-block">
<span class="keyword namespace">import</span> <span class="name namespace">urllib</span>
<span class="comment single"># -> https://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php</span>
<span class="name">feed</span> <span class="operator">=</span> <span class="literal string double">"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/"</span>
<span class="comment single"># Significant earthquakes in the last 30 days</span>
<span class="comment single"># url = urllib.request.urlopen(feed + "significant_month.csv")</span>
<span class="comment single"># Magnitude > 4.5</span>
<span class="name">url</span> <span class="operator">=</span> <span class="name">urllib</span><span class="operator">.</span><span class="name">request</span><span class="operator">.</span><span class="name">urlopen</span><span class="punctuation">(</span><span class="name">feed</span> <span class="operator">+</span> <span class="literal string double">"4.5_month.csv"</span><span class="punctuation">)</span>
<span class="comment single"># Magnitude > 2.5</span>
<span class="comment single"># url = urllib.request.urlopen(feed + "2.5_month.csv")</span>
<span class="comment single"># Magnitude > 1.0</span>
<span class="comment single"># url = urllib.request.urlopen(feed + "1.0_month.csv")</span>
<span class="comment single"># Reading and storage of data</span>
<span class="name">data</span> <span class="operator">=</span> <span class="name">url</span><span class="operator">.</span><span class="name">read</span><span class="punctuation">()</span>
<span class="name">data</span> <span class="operator">=</span> <span class="name">data</span><span class="operator">.</span><span class="name">split</span><span class="punctuation">(</span><span class="literal string affix">b</span><span class="literal string single">'</span><span class="literal string escape">\n</span><span class="literal string single">'</span><span class="punctuation">)[</span><span class="operator">+</span><span class="literal number integer">1</span><span class="punctuation">:</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">]</span>
<span class="name">E</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">zeros</span><span class="punctuation">(</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">data</span><span class="punctuation">),</span> <span class="name">dtype</span><span class="operator">=</span><span class="punctuation">[(</span><span class="literal string single">'position'</span><span class="punctuation">,</span> <span class="name builtin">float</span><span class="punctuation">,</span> <span class="literal number integer">2</span><span class="punctuation">),</span>
<span class="punctuation">(</span><span class="literal string single">'magnitude'</span><span class="punctuation">,</span> <span class="name builtin">float</span><span class="punctuation">)])</span>
<span class="keyword">for</span> <span class="name">i</span> <span class="operator word">in</span> <span class="name builtin">range</span><span class="punctuation">(</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">data</span><span class="punctuation">)):</span>
<span class="name">row</span> <span class="operator">=</span> <span class="name">data</span><span class="punctuation">[</span><span class="name">i</span><span class="punctuation">]</span><span class="operator">.</span><span class="name">split</span><span class="punctuation">(</span><span class="literal string affix">b</span><span class="literal string single">','</span><span class="punctuation">)</span>
<span class="name">E</span><span class="punctuation">[</span><span class="literal string single">'position'</span><span class="punctuation">][</span><span class="name">i</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name builtin">float</span><span class="punctuation">(</span><span class="name">row</span><span class="punctuation">[</span><span class="literal number integer">2</span><span class="punctuation">]),</span><span class="name builtin">float</span><span class="punctuation">(</span><span class="name">row</span><span class="punctuation">[</span><span class="literal number integer">1</span><span class="punctuation">])</span>
<span class="name">E</span><span class="punctuation">[</span><span class="literal string single">'magnitude'</span><span class="punctuation">][</span><span class="name">i</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name builtin">float</span><span class="punctuation">(</span><span class="name">row</span><span class="punctuation">[</span><span class="literal number integer">4</span><span class="punctuation">])</span>
</pre>
<p>Now, we need to draw the earth on a figure to show precisely where the earthquake
center is and to translate latitude/longitude in some coordinates matplotlib
can handle. Fortunately, there is the <a class="reference external" href="https://matplotlib.org/basemap/">basemap</a> project (which is now deprecated in favor
of the <a class="reference external" href="https://scitools.org.uk/cartopy/docs/latest/">cartopy</a> project) that is really
simple to install and to use. First step is to define a projection to draw the
earth onto a screen (there exists many different projections) and we'll stick
to the <cite>mill</cite> projection which is rather standard for non-specialist like me.</p>
<pre class="code python literal-block">
<span class="keyword namespace">from</span> <span class="name namespace">mpl_toolkits.basemap</span> <span class="keyword namespace">import</span> <span class="name">Basemap</span>
<span class="name">fig</span> <span class="operator">=</span> <span class="name">plt</span><span class="operator">.</span><span class="name">figure</span><span class="punctuation">(</span><span class="name">figsize</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">14</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">))</span>
<span class="name">ax</span> <span class="operator">=</span> <span class="name">plt</span><span class="operator">.</span><span class="name">subplot</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="name builtin">map</span> <span class="operator">=</span> <span class="name">Basemap</span><span class="punctuation">(</span><span class="name">projection</span><span class="operator">=</span><span class="literal string single">'mill'</span><span class="punctuation">)</span>
</pre>
<p>Next, we request to draw coastline and fill continents:</p>
<pre class="code python literal-block">
<span class="name builtin">map</span><span class="operator">.</span><span class="name">drawcoastlines</span><span class="punctuation">(</span><span class="name">color</span><span class="operator">=</span><span class="literal string single">'0.50'</span><span class="punctuation">,</span> <span class="name">linewidth</span><span class="operator">=</span><span class="literal number float">0.25</span><span class="punctuation">)</span>
<span class="name builtin">map</span><span class="operator">.</span><span class="name">fillcontinents</span><span class="punctuation">(</span><span class="name">color</span><span class="operator">=</span><span class="literal string single">'0.95'</span><span class="punctuation">)</span>
</pre>
<p>For cartopy, the steps are quite similar:</p>
<pre class="code python literal-block">
<span class="keyword namespace">import</span> <span class="name namespace">cartopy</span>
<span class="name">ax</span> <span class="operator">=</span> <span class="name">plt</span><span class="operator">.</span><span class="name">axes</span><span class="punctuation">(</span><span class="name">projection</span><span class="operator">=</span><span class="name">cartopy</span><span class="operator">.</span><span class="name">crs</span><span class="operator">.</span><span class="name">Miller</span><span class="punctuation">())</span>
<span class="name">ax</span><span class="operator">.</span><span class="name">coastlines</span><span class="punctuation">(</span><span class="name">color</span><span class="operator">=</span><span class="literal string single">'0.50'</span><span class="punctuation">,</span> <span class="name">linewidth</span><span class="operator">=</span><span class="literal number float">0.25</span><span class="punctuation">)</span>
<span class="name">ax</span><span class="operator">.</span><span class="name">add_feature</span><span class="punctuation">(</span><span class="name">cartopy</span><span class="operator">.</span><span class="name">feature</span><span class="operator">.</span><span class="name">LAND</span><span class="punctuation">,</span> <span class="name">color</span><span class="operator">=</span><span class="literal string single">'0.95'</span><span class="punctuation">)</span>
<span class="name">ax</span><span class="operator">.</span><span class="name">set_global</span><span class="punctuation">()</span>
<span class="name">trans</span> <span class="operator">=</span> <span class="name">cartopy</span><span class="operator">.</span><span class="name">crs</span><span class="operator">.</span><span class="name">PlateCarree</span><span class="punctuation">()</span>
</pre>
<p>We are almost finished. Last step is to adapt the rain code and
put some eye candy. For basemap we use the map object to
transform the coordinates whereas for cartopy we use the transform_point
function of the chosen Miller projection:</p>
<pre class="code python literal-block">
<span class="name">P</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">zeros</span><span class="punctuation">(</span><span class="literal number integer">50</span><span class="punctuation">,</span> <span class="name">dtype</span><span class="operator">=</span><span class="punctuation">[(</span><span class="literal string single">'position'</span><span class="punctuation">,</span> <span class="name builtin">float</span><span class="punctuation">,</span> <span class="literal number integer">2</span><span class="punctuation">),</span>
<span class="punctuation">(</span><span class="literal string single">'size'</span><span class="punctuation">,</span> <span class="name builtin">float</span><span class="punctuation">),</span>
<span class="punctuation">(</span><span class="literal string single">'growth'</span><span class="punctuation">,</span> <span class="name builtin">float</span><span class="punctuation">),</span>
<span class="punctuation">(</span><span class="literal string single">'color'</span><span class="punctuation">,</span> <span class="name builtin">float</span><span class="punctuation">,</span> <span class="literal number integer">4</span><span class="punctuation">)])</span>
<span class="name">scat</span> <span class="operator">=</span> <span class="name">ax</span><span class="operator">.</span><span class="name">scatter</span><span class="punctuation">(</span><span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'position'</span><span class="punctuation">][:,</span><span class="literal number integer">0</span><span class="punctuation">],</span> <span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'position'</span><span class="punctuation">][:,</span><span class="literal number integer">1</span><span class="punctuation">],</span> <span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'size'</span><span class="punctuation">],</span> <span class="name">lw</span><span class="operator">=</span><span class="literal number float">0.5</span><span class="punctuation">,</span>
<span class="name">edgecolors</span> <span class="operator">=</span> <span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'color'</span><span class="punctuation">],</span> <span class="name">facecolors</span><span class="operator">=</span><span class="literal string single">'None'</span><span class="punctuation">,</span> <span class="name">zorder</span><span class="operator">=</span><span class="literal number integer">10</span><span class="punctuation">)</span>
<span class="keyword">def</span> <span class="name function">update</span><span class="punctuation">(</span><span class="name">frame</span><span class="punctuation">):</span>
<span class="name">current</span> <span class="operator">=</span> <span class="name">frame</span> <span class="operator">%</span> <span class="name builtin">len</span><span class="punctuation">(</span><span class="name">E</span><span class="punctuation">)</span>
<span class="name">i</span> <span class="operator">=</span> <span class="name">frame</span> <span class="operator">%</span> <span class="name builtin">len</span><span class="punctuation">(</span><span class="name">P</span><span class="punctuation">)</span>
<span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'color'</span><span class="punctuation">][:,</span><span class="literal number integer">3</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">maximum</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'color'</span><span class="punctuation">][:,</span><span class="literal number integer">3</span><span class="punctuation">]</span> <span class="operator">-</span> <span class="literal number float">1.0</span><span class="operator">/</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">P</span><span class="punctuation">))</span>
<span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'size'</span><span class="punctuation">]</span> <span class="operator">+=</span> <span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'growth'</span><span class="punctuation">]</span>
<span class="name">magnitude</span> <span class="operator">=</span> <span class="name">E</span><span class="punctuation">[</span><span class="literal string single">'magnitude'</span><span class="punctuation">][</span><span class="name">current</span><span class="punctuation">]</span>
<span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'position'</span><span class="punctuation">][</span><span class="name">i</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name builtin">map</span><span class="punctuation">(</span><span class="operator">*</span><span class="name">E</span><span class="punctuation">[</span><span class="literal string single">'position'</span><span class="punctuation">][</span><span class="name">current</span><span class="punctuation">])</span> <span class="keyword">if</span> <span class="name">use_basemap</span> <span class="keyword">else</span> \
<span class="name">cartopy</span><span class="operator">.</span><span class="name">crs</span><span class="operator">.</span><span class="name">Miller</span><span class="punctuation">()</span><span class="operator">.</span><span class="name">transform_point</span><span class="punctuation">(</span><span class="operator">*</span><span class="name">E</span><span class="punctuation">[</span><span class="literal string single">'position'</span><span class="punctuation">][</span><span class="name">current</span><span class="punctuation">],</span> <span class="name">cartopy</span><span class="operator">.</span><span class="name">crs</span><span class="operator">.</span><span class="name">PlateCarree</span><span class="punctuation">())</span>
<span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'size'</span><span class="punctuation">][</span><span class="name">i</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">5</span>
<span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'growth'</span><span class="punctuation">][</span><span class="name">i</span><span class="punctuation">]</span><span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">exp</span><span class="punctuation">(</span><span class="name">magnitude</span><span class="punctuation">)</span> <span class="operator">*</span> <span class="literal number float">0.1</span>
<span class="keyword">if</span> <span class="name">magnitude</span> <span class="operator"><</span> <span class="literal number integer">6</span><span class="punctuation">:</span>
<span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'color'</span><span class="punctuation">][</span><span class="name">i</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">1</span>
<span class="keyword">else</span><span class="punctuation">:</span>
<span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'color'</span><span class="punctuation">][</span><span class="name">i</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span>
<span class="name">scat</span><span class="operator">.</span><span class="name">set_edgecolors</span><span class="punctuation">(</span><span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'color'</span><span class="punctuation">])</span>
<span class="name">scat</span><span class="operator">.</span><span class="name">set_facecolors</span><span class="punctuation">(</span><span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'color'</span><span class="punctuation">]</span><span class="operator">*</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number float">0.25</span><span class="punctuation">))</span>
<span class="name">scat</span><span class="operator">.</span><span class="name">set_sizes</span><span class="punctuation">(</span><span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'size'</span><span class="punctuation">])</span>
<span class="name">scat</span><span class="operator">.</span><span class="name">set_offsets</span><span class="punctuation">(</span><span class="name">P</span><span class="punctuation">[</span><span class="literal string single">'position'</span><span class="punctuation">])</span>
<span class="keyword">return</span> <span class="name">scat</span><span class="punctuation">,</span>
<span class="name">animation</span> <span class="operator">=</span> <span class="name">FuncAnimation</span><span class="punctuation">(</span><span class="name">fig</span><span class="punctuation">,</span> <span class="name">update</span><span class="punctuation">,</span> <span class="name">interval</span><span class="operator">=</span><span class="literal number integer">10</span><span class="punctuation">,</span> <span class="name">blit</span><span class="operator">=</span><span class="keyword constant">True</span><span class="punctuation">)</span>
<span class="name">plt</span><span class="operator">.</span><span class="name">show</span><span class="punctuation">()</span>
</pre>
<p>If everything went well, you should obtain something like this (with animation):</p>
<a class="reference external image-reference" href="scripts/earthquakes.py"><img alt="figures/earthquakes.png" src="figures/earthquakes.png" style="width: 50%;" /></a>
</div>
</div>
<div class="section" id="other-types-of-plots">
<h1><a class="toc-backref" href="#id9">Other Types of Plots</a></h1>
<a class="reference internal image-reference" href="#regular-plots"><img alt="figures/plot.png" src="figures/plot.png" /></a>
<a class="reference internal image-reference" href="#scatter-plots"><img alt="figures/scatter.png" src="figures/scatter.png" /></a>
<a class="reference internal image-reference" href="#bar-plots"><img alt="figures/bar.png" src="figures/bar.png" /></a>
<a class="reference internal image-reference" href="#contour-plots"><img alt="figures/contour.png" src="figures/contour.png" /></a>
<a class="reference internal image-reference" href="#imshow"><img alt="figures/imshow.png" src="figures/imshow.png" /></a>
<a class="reference internal image-reference" href="#quiver-plots"><img alt="figures/quiver.png" src="figures/quiver.png" /></a>
<a class="reference internal image-reference" href="#pie-charts"><img alt="figures/pie.png" src="figures/pie.png" /></a>
<a class="reference internal image-reference" href="#grids"><img alt="figures/grid.png" src="figures/grid.png" /></a>
<a class="reference internal image-reference" href="#multi-plots"><img alt="figures/multiplot.png" src="figures/multiplot.png" /></a>
<a class="reference internal image-reference" href="#polar-axis"><img alt="figures/polar.png" src="figures/polar.png" /></a>
<a class="reference internal image-reference" href="#d-plots"><img alt="figures/plot3d.png" src="figures/plot3d.png" /></a>
<a class="reference internal image-reference" href="#text"><img alt="figures/text.png" src="figures/text.png" /></a>
<div class="section" id="regular-plots">
<h2>Regular Plots</h2>
<a class="reference external image-reference" href="scripts/plot_ex.py"><img alt="figures/plot_ex.png" class="align-right" src="figures/plot_ex.png" /></a>
<div class="admonition admonition-hints">
<p class="first admonition-title">Hints</p>
<p class="last">You need to use the <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.fill_between">fill_between</a>
command.</p>
</div>
<p>Starting from the code below, try to reproduce the graphic on the right taking
care of filled areas.</p>
<pre class="literal-block">
import numpy as np
import matplotlib.pyplot as plt
n = 256
X = np.linspace(-np.pi,np.pi,n,endpoint=True)
Y = np.sin(2*X)
plt.plot (X, Y+1, color='blue', alpha=1.00)
plt.plot (X, Y-1, color='blue', alpha=1.00)
plt.show()
</pre>
<p>Click on figure for solution.</p>
</div>
<div class="section" id="scatter-plots">
<h2>Scatter Plots</h2>
<a class="reference external image-reference" href="scripts/scatter_ex.py"><img alt="figures/scatter_ex.png" class="align-right" src="figures/scatter_ex.png" /></a>
<div class="admonition admonition-hints">
<p class="first admonition-title">Hints</p>
<p class="last">Color is given by angle of (X,Y).</p>
</div>
<p>Starting from the code below, try to reproduce the graphic on the right taking
care of marker size, color and transparency.</p>
<pre class="literal-block">
import numpy as np
import matplotlib.pyplot as plt
n = 1024
X = np.random.normal(0,1,n)
Y = np.random.normal(0,1,n)
plt.scatter(X,Y)
plt.show()
</pre>
<p>Click on figure for solution.</p>
</div>
<div class="section" id="bar-plots">
<h2>Bar Plots</h2>
<a class="reference external image-reference" href="scripts/bar_ex.py"><img alt="figures/bar_ex.png" class="align-right" src="figures/bar_ex.png" /></a>
<div class="admonition admonition-hints">
<p class="first admonition-title">Hints</p>
<p class="last">You need to take care of text alignment.</p>
</div>
<p>Starting from the code below, try to reproduce the graphic on the right by
adding labels for red bars.</p>
<pre class="literal-block">
import numpy as np
import matplotlib.pyplot as plt
n = 12
X = np.arange(n)
Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
for x,y in zip(X,Y1):
plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom')
plt.ylim(-1.25,+1.25)
plt.show()
</pre>
<p>Click on figure for solution.</p>
</div>
<div class="section" id="contour-plots">
<h2>Contour Plots</h2>
<a class="reference external image-reference" href="scripts/contour_ex.py"><img alt="figures/contour_ex.png" class="align-right" src="figures/contour_ex.png" /></a>
<div class="admonition admonition-hints">
<p class="first admonition-title">Hints</p>
<p class="last">You need to use the <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.clabel">clabel</a>
command.</p>
</div>
<p>Starting from the code below, try to reproduce the graphic on the right taking
care of the colormap (see <a class="reference internal" href="#colormaps">Colormaps</a> below).</p>
<pre class="literal-block">
import numpy as np
import matplotlib.pyplot as plt
def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)
n = 256
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)
X,Y = np.meshgrid(x,y)
plt.contourf(X, Y, f(X,Y), 8, alpha=.75, cmap='jet')
C = plt.contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5)
plt.show()
</pre>
<p>Click on figure for solution.</p>
</div>
<div class="section" id="imshow">
<h2>Imshow</h2>
<a class="reference external image-reference" href="scripts/imshow_ex.py"><img alt="figures/imshow_ex.png" class="align-right" src="figures/imshow_ex.png" /></a>
<div class="admonition admonition-hints">
<p class="first admonition-title">Hints</p>
<p class="last">You need to take care of the <tt class="docutils literal">origin</tt> of the image in the imshow command and
use a <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.colorbar">colorbar</a>.</p>
</div>
<p>Starting from the code below, try to reproduce the graphic on the right taking
care of colormap, image interpolation and origin.</p>
<pre class="literal-block">
import numpy as np
import matplotlib.pyplot as plt
def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)
n = 10
x = np.linspace(-3,3,4*n)
y = np.linspace(-3,3,3*n)
X,Y = np.meshgrid(x,y)
plt.imshow(f(X,Y))
plt.show()
</pre>
<p>Click on figure for solution.</p>
</div>
<div class="section" id="pie-charts">
<h2>Pie Charts</h2>
<a class="reference external image-reference" href="scripts/pie_ex.py"><img alt="figures/pie_ex.png" class="align-right" src="figures/pie_ex.png" /></a>
<div class="admonition admonition-hints">
<p class="first admonition-title">Hints</p>
<p class="last">You need to modify Z.</p>
</div>
<p>Starting from the code below, try to reproduce the graphic on the right taking
care of colors and slices size.</p>
<pre class="literal-block">
import numpy as np
import matplotlib.pyplot as plt
n = 20
Z = np.random.uniform(0,1,n)
plt.pie(Z)
plt.show()
</pre>
<p>Click on figure for solution.</p>
</div>
<div class="section" id="quiver-plots">
<h2>Quiver Plots</h2>
<a class="reference external image-reference" href="scripts/quiver_ex.py"><img alt="figures/quiver_ex.png" class="align-right" src="figures/quiver_ex.png" /></a>
<div class="admonition admonition-hints">
<p class="first admonition-title">Hints</p>
<p class="last">You need to draw arrows twice.</p>
</div>
<p>Starting from the code above, try to reproduce the graphic on the right taking
care of colors and orientations.</p>
<pre class="literal-block">
import numpy as np
import matplotlib.pyplot as plt
n = 8
X,Y = np.mgrid[0:n,0:n]
plt.quiver(X,Y)
plt.show()
</pre>
<p>Click on figure for solution.</p>
</div>
<div class="section" id="grids">
<h2>Grids</h2>
<a class="reference external image-reference" href="scripts/grid_ex.py"><img alt="figures/grid_ex.png" class="align-right" src="figures/grid_ex.png" /></a>
<p>Starting from the code below, try to reproduce the graphic on the right taking
care of line styles.</p>
<pre class="literal-block">
import numpy as np
import matplotlib.pyplot as plt
axes = gca()
axes.set_xlim(0,4)
axes.set_ylim(0,3)
axes.set_xticklabels([])
axes.set_yticklabels([])
plt.show()
</pre>
<p>Click on figure for solution.</p>
</div>
<div class="section" id="multi-plots">
<h2>Multi Plots</h2>
<a class="reference external image-reference" href="scripts/multiplot_ex.py"><img alt="figures/multiplot_ex.png" class="align-right" src="figures/multiplot_ex.png" /></a>
<div class="admonition admonition-hints">
<p class="first admonition-title">Hints</p>
<p class="last">You can use several subplots with different partition.</p>
</div>
<p>Starting from the code below, try to reproduce the graphic on the right.</p>
<pre class="literal-block">
import numpy as np
import matplotlib.pyplot as plt
plt.subplot(2,2,1)
plt.subplot(2,2,3)
plt.subplot(2,2,4)
plt.show()
</pre>
<p>Click on figure for solution.</p>
</div>
<div class="section" id="polar-axis">
<h2>Polar Axis</h2>
<a class="reference external image-reference" href="scripts/polar_ex.py"><img alt="figures/polar_ex.png" class="align-right" src="figures/polar_ex.png" /></a>
<div class="admonition admonition-hints">
<p class="first admonition-title">Hints</p>
<p class="last">You only need to modify the <tt class="docutils literal">axes</tt> line.</p>
</div>
<p>Starting from the code below, try to reproduce the graphic on the right.</p>
<pre class="literal-block">
import numpy as np
import matplotlib.pyplot as plt
plt.axes([0,0,1,1])
N = 20
theta = np.arange(0.0, 2*np.pi, 2*np.pi/N)
radii = 10*np.random.rand(N)
width = np.pi/4*np.random.rand(N)
bars = plt.bar(theta, radii, width=width, bottom=0.0)
for r,bar in zip(radii, bars):
bar.set_facecolor( cm.jet(r/10.))
bar.set_alpha(0.5)
plt.show()
</pre>
<p>Click on figure for solution.</p>
</div>
<div class="section" id="d-plots">
<h2>3D Plots</h2>
<a class="reference external image-reference" href="scripts/plot3d_ex.py"><img alt="figures/plot3d_ex.png" class="align-right" src="figures/plot3d_ex.png" /></a>
<div class="admonition admonition-hints">
<p class="first admonition-title">Hints</p>
<p class="last">You need to use <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.contourf">contourf</a>.</p>
</div>
<p>Starting from the code below, try to reproduce the graphic on the right.</p>
<pre class="literal-block">
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D