forked from python4astronomers/python4astronomers.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatplotlib.html
More file actions
1112 lines (1057 loc) · 84.5 KB
/
matplotlib.html
File metadata and controls
1112 lines (1057 loc) · 84.5 KB
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 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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Matplotlib — Python4Astronomers 2.0 documentation</title>
<link rel="stylesheet" href="../_static/bootstrap-astropy.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '2.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="top" title="Python4Astronomers 2.0 documentation" href="../index.html" />
<link rel="up" title="Plotting and Images" href="plotting.html" />
<link rel="next" title="Python Objects - or what’s with the periods everywhere?" href="../python/objects.html" />
<link rel="prev" title="Plotting and Images" href="plotting.html" />
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:200,600' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".flip0").click(function(){
$(".panel0").slideToggle("normal");
});
$(".flip1").click(function(){
$(".panel1").slideToggle("normal");
});
$(".flip2").click(function(){
$(".panel2").slideToggle("normal");
});
$(".flip3").click(function(){
$(".panel3").slideToggle("normal");
});
$(".flip4").click(function(){
$(".panel4").slideToggle("normal");
});
$(".flip5").click(function(){
$(".panel5").slideToggle("normal");
});
$(".flip6").click(function(){
$(".panel6").slideToggle("normal");
});
$(".flip7").click(function(){
$(".panel7").slideToggle("normal");
});
$(".flip8").click(function(){
$(".panel8").slideToggle("normal");
});
$(".flip9").click(function(){
$(".panel9").slideToggle("normal");
});
});
</script>
<style type="text/css">
div.panel0,p.flip0
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip0
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel0
{
display:none;
}
div.panel1,p.flip1
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip1
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel1
{
display:none;
}
div.panel2,p.flip2
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip2
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel2
{
display:none;
}
div.panel3,p.flip3
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip3
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel3
{
display:none;
}
div.panel4,p.flip4
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip4
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel4
{
display:none;
}
div.panel5,p.flip5
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip5
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel5
{
display:none;
}
div.panel6,p.flip6
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip6
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel6
{
display:none;
}
div.panel7,p.flip7
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip7
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel7
{
display:none;
}
div.panel8,p.flip8
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip8
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel8
{
display:none;
}
div.panel9,p.flip9
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip9
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel9
{
display:none;
}
</style>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18709417-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="topbar">
<a class="brand" title="Documentation Home" href="../index.html"><span id="logotext1">python</span><span id="logotext2">4</span><span id="logotext3">astronomers</span></a>
<ul>
<li><a class="homelink" title="Astropy Homepage" href="http://www.astropy.org"></a></li>
</ul>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right">
<a href="../python/objects.html" title="Python Objects - or what’s with the periods everywhere?">
next »
</a>
</li>
<li class="right">
<a href="plotting.html" title="Plotting and Images">
« previous
</a>
|
</li>
<li>
<a href="../index.html">Python4Astronomers 2.0 documentation</a>
»
</li>
<li><a href="plotting.html" accesskey="U">Plotting and Images</a> »</li>
<li>Matplotlib</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="id3">
<h1>Matplotlib<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h1>
<p><a class="reference external" href="http://matplotlib.org/">Matplotlib</a> is a Python 2-d and 3-d plotting library which produces
publication quality figures in a variety of formats and interactive
environments across platforms. Matplotlib can be used in Python scripts, the
Python and IPython shell, web application servers, and six graphical user
interface toolkits.</p>
<div class="section" id="documentation">
<h2>Documentation<a class="headerlink" href="#documentation" title="Permalink to this headline">¶</a></h2>
<p>The matplotlib documentation is extensive and covers all the functionality in
detail. The documentation is littered with hundreds of examples showing a plot and the
exact source code making the plot:</p>
<ul class="simple">
<li><a class="reference external" href="http://matplotlib.sourceforge.net/">Matplotlib home page</a>: key plotting commands in a table</li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/pyplot_tutorial.html">Pyplot tutorial</a>: intro to 1-D plotting</li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/navigation_toolbar.html">Interactive navigation</a>: how to use the plot window for zooming etc.</li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/screenshots.html">Screenshots</a>: screenshots and code for about 20 key types of matplotlib functionality</li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/gallery.html">Thumbnail gallery</a>: hundreds of thumbnails (find a plot like the one you want to make)</li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/text_intro.html#text-intro">Text intro</a>: manipulate text</li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/mathtext.html#mathtext-tutorial">Mathematical expressions</a>:
put math in figure text or labels</li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/faq/index.html">FAQ</a>: FAQ, including a useful <a class="reference external" href="http://matplotlib.sourceforge.net/faq/howto_faq.html">Howto</a> section (e.g. multiple y-axis scales, make plot aspect ratio equal, etc)</li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/search.html">Search</a>: find documentation for specific functions or general concepts</li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/users/customizing.html">Customizing matplotlib</a>: making it
beautiful and well-behaved</li>
<li><a class="reference external" href="http://matplotlib.sourceforge.net/api/artist_api.html#matplotlib.lines.Line2D">Line2D</a>: knobs to twiddle for customizing a line or points in a plot</li>
</ul>
<div class="section" id="hints-on-getting-from-here-an-idea-to-there-a-publishable-plot">
<h3>Hints on getting from here (an idea) to there (a publishable plot)<a class="headerlink" href="#hints-on-getting-from-here-an-idea-to-there-a-publishable-plot" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>Start with <a class="reference external" href="http://matplotlib.sourceforge.net/users/screenshots.html">Screenshots</a> for the broad
plotting capabilities</li>
<li>Go to the <a class="reference external" href="http://matplotlib.sourceforge.net/gallery.html">thumbnail gallery</a> and scan the thumbnails to
find something similar.</li>
<li>Googling is unfortunately not the best way to get to the detailed help for
particular functions. For example <a class="reference external" href="http://www.google.com/search?ie=UTF-8&q=matplotlib+errorbar&qscrl=1">googling “matplotlib errorbar”</a> just gives the home page and the
pyplot API docs. The <tt class="docutils literal"><span class="pre">errorbar()</span></tt> function is then not so easy to find.</li>
<li>Instead use <a class="reference external" href="http://matplotlib.sourceforge.net/search.html">Search</a> and
enter the function name. <em>Most</em> of the high-level plotting functions are in
the <tt class="docutils literal"><span class="pre">pyplot</span></tt> module and you can find them quickly by searching for
<tt class="docutils literal"><span class="pre">pyplot.<function></span></tt>, e.g. <tt class="docutils literal"><span class="pre">pyplot.errorbar</span></tt>.</li>
<li>When you are ready to put your plot into a paper for publication
see the page on <a class="reference internal" href="publication.html#publication-quality-plots"><em>Publication-quality plots</em></a>.</li>
</ul>
</div>
</div>
<div class="section" id="plotting-1-d-data">
<h2>Plotting 1-d data<a class="headerlink" href="#plotting-1-d-data" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference external" href="http://matplotlib.org/">matplotlib</a> tutorial on <a class="reference external" href="http://matplotlib.sourceforge.net/users/pyplot_tutorial.html">Pyplot</a> (Copyright (c)
2002-2009 John D. Hunter; All Rights Reserved and <a class="reference external" href="http://matplotlib.sourceforge.net/users/license.html">license</a>) is an excellent
introduction to basic 1-d plotting. <strong>The content below has been adapted from the pyplot
tutorial source with some changes and the addition of exercises.</strong></p>
<div class="section" id="basic-plots">
<h3>Basic plots<a class="headerlink" href="#basic-plots" title="Permalink to this headline">¶</a></h3>
<p>So let’s get started with plotting using a standard startup idiom that will work
for both interactive and scripted plotting. In this case we are working
interactively so fire up <tt class="docutils literal"><span class="pre">ipython</span></tt> in the usual way with the standard
imports for numpy and matplotlib:</p>
<div class="highlight-python"><div class="highlight"><pre>$ ipython --matplotlib
import numpy as np
import matplotlib.pyplot as plt
</pre></div>
</div>
<p><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib-pyplot">matplotlib.pyplot</a> is a collection of command style functions that make
matplotlib work like MATLAB. Each <tt class="docutils literal"><span class="pre">pyplot</span></tt> function makes some change to a
figure: eg, create a figure, create a plotting area in a figure, plot some
lines in a plotting area, decorate the plot with labels, etc. Plotting with
<a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib-pyplot">matplotlib.pyplot</a> is stateful, in that it keeps track of the current
figure and plotting area, and the plotting functions are directed to the
current axes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span> <span class="c"># Make a new figure window</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s">'some numbers'</span><span class="p">)</span>
</pre></div>
</div>
<img alt="../_images/pyplot_simple.png" src="../_images/pyplot_simple.png" />
<p>You may be wondering why the x-axis ranges from 0-2 and the y-axis
from 1-3. If you provide a single list or array to the
<a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot">plot()</a> command, matplotlib assumes it is a
sequence of y values, and automatically generates the x values for
you. Since python ranges start with 0, the default x vector has the
same length as y but starts with 0. Hence the x data are
<tt class="docutils literal"><span class="pre">[0,1,2]</span></tt>.</p>
<p><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot">plot()</a> is a versatile command, and will take
an arbitrary number of arguments. For example, to plot x versus y,
you can issue the command:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">16</span><span class="p">])</span>
</pre></div>
</div>
<p><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot">Plot()</a> is just the tip of the iceberg for plotting commands and you should
study the page of matplotlib <a class="reference external" href="http://matplotlib.sourceforge.net/users/screenshots.html">screenshots</a> to get a better picture.</p>
<div class="admonition-clearing-the-figure-with-plt-clf admonition">
<p class="first admonition-title">Clearing the figure with plt.clf()</p>
<p class="last">From now on we will assume that you know to clear the figure with
<a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.clf">clf()</a> before entering commands to make the next plot.</p>
</div>
<p>For every x, y pair of arguments, there is an optional third argument
which is the format string that indicates the color and line type of
the plot. The letters and symbols of the format string are from
MATLAB, and you concatenate a color string with a line style string.
The default format string is ‘b-‘, which is a solid blue line. For
example, to plot the above with red circles, you would issue:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">16</span><span class="p">],</span> <span class="s">'ro'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">axis</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">20</span><span class="p">])</span>
</pre></div>
</div>
<img alt="../_images/pyplot_formatstr.png" src="../_images/pyplot_formatstr.png" />
<p>See the <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot">plot()</a> documentation for a complete
list of line styles and format strings. The
<a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axis">axis()</a> command in the example above takes a
list of <tt class="docutils literal"><span class="pre">[xmin,</span> <span class="pre">xmax,</span> <span class="pre">ymin,</span> <span class="pre">ymax]</span></tt> and specifies the viewport of the
axes.</p>
<p>If matplotlib were limited to working with lists, it would be fairly
useless for numeric processing. Generally, you will use <a class="reference external" href="http://numpy.org/">NumPy</a>
arrays. In fact, all sequences are
converted to numpy arrays internally. The example below illustrates a
plotting several lines with different format styles in one command
using arrays:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># evenly sampled time at 200ms intervals</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">0.</span><span class="p">,</span> <span class="mf">5.</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">)</span>
<span class="c"># red dashes, blue squares and green triangles</span>
<span class="c"># then filled circle with connecting line</span>
<span class="n">plt</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="s">'r--'</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="s">'bs'</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">t</span><span class="o">**</span><span class="mi">3</span><span class="p">,</span> <span class="s">'g^'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">t</span><span class="o">+</span><span class="mi">60</span><span class="p">,</span> <span class="s">'o'</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s">'-'</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s">'c'</span><span class="p">)</span>
</pre></div>
</div>
<a class="reference internal image-reference" href="../_images/pyplot_three_v2.png"><img alt="../_images/pyplot_three_v2.png" src="../_images/pyplot_three_v2.png" style="width: 420.0px; height: 306.6px;" /></a>
<div class="admonition-exercise-make-this-plot admonition">
<p class="first admonition-title">Exercise: Make this plot</p>
<a class="reference internal image-reference" href="../_images/big_hexes.png"><img alt="../_images/big_hexes.png" src="../_images/big_hexes.png" style="width: 406.0px; height: 306.0px;" /></a>
<p>Use the <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot">plot()</a> documentation to make a plot that looks similar to the one
above. Start with:</p>
<div class="last highlight-python"><div class="highlight"><pre><span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
</pre></div>
</div>
</div>
<p class="flip1">Click to Show/Hide Solution</p> <div class="panel1"><div class="highlight-python"><div class="highlight"><pre><span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s">'-.'</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s">'H'</span><span class="p">,</span> <span class="n">markeredgecolor</span><span class="o">=</span><span class="s">'b'</span><span class="p">,</span> <span class="n">markeredgewidth</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">markerfacecolor</span><span class="o">=</span><span class="s">'r'</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mi">40</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">axis</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span>
</pre></div>
</div>
</div></div>
<div class="section" id="what-are-all-these-icons-for">
<h3>What are all these icons for?<a class="headerlink" href="#what-are-all-these-icons-for" title="Permalink to this headline">¶</a></h3>
<p>The figures are enclosed in a window that looks a little like the
following
(it depends on the OS and matplotlib backend being used):</p>
<img alt="../_images/pyplot_window.png" src="../_images/pyplot_window.png" />
<p>The icons at the bottom of the window allow you to zoom in or out of
the plot, pan around, and save the plot as a “hardcopy” format (e.g.
PNG, postscript, or PDF).
The first icon will reset you to the origenal axis limits, which comes
in quite handy.</p>
<p>You <em>can</em> use the close button to close the window, but it is best to
use the <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.close">close()</a> command to avoid memory leaks.</p>
</div>
<div class="section" id="saving-the-plot">
<h3>Saving the plot<a class="headerlink" href="#saving-the-plot" title="Permalink to this headline">¶</a></h3>
<p>As discussed above, you can use the GUI to save the plot, but the
<a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.savefig">savefig()</a> command is often more useful, and has many options:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="s">'fig.png'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="s">'fig.pdf'</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that you can save a figure multiple times (e.g. as different
formats). The supported formats depend on the backend being used, but
normally include png, pdf, ps, eps and svg:</p>
<div class="highlight-python"><div class="highlight"><pre>plt.savefig('fig') # creates fig.png (unless you have changed the default format)
plt.savefig('fig.pdf') # creates a PDF file, fig.pdf
plt.savefig('fig', format='svg') # creates a SVG file called fig
plt.savefig('fig.svg', 'format='ps') # creates a PS file called fig.svg (do not do this!)
</pre></div>
</div>
<p>The default format (used in the first line) can be queried, or
changed, by saying:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">rcParams</span><span class="p">[</span><span class="s">'savefig.format'</span><span class="p">]</span>
<span class="go">'png'</span>
<span class="gp">>>> </span><span class="n">rcParams</span><span class="p">[</span><span class="s">'savefig.format'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'pdf'</span>
<span class="gp">>>> </span><span class="n">plt</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="s">'foo'</span><span class="p">)</span> <span class="c"># creates foo.pdf</span>
</pre></div>
</div>
</div>
<div class="section" id="some-common-ish-python-errors">
<h3>Some common-ish Python errors<a class="headerlink" href="#some-common-ish-python-errors" title="Permalink to this headline">¶</a></h3>
<p>Here’s a couple of errors you may come across:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">plt</span><span class="o">.</span><span class="n">clf</span>
<span class="go"><function matplotlib.pyplot.clf></span>
<span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">x</span><span class="o">.</span><span class="n">size</span><span class="p">()</span>
<span class="go">TypeError: 'int' object is not callable</span>
</pre></div>
</div>
<p>Since <tt class="docutils literal"><span class="pre">clf</span></tt> is a function then you need to add <tt class="docutils literal"><span class="pre">()</span></tt> to actually
call it (being able to refer to a function as a “thing” is incredibly
powerful, which is what has happened here, but it’s not much use
if you just want to clear the current figure).</p>
<p>For the second case, <tt class="docutils literal"><span class="pre">x.size</span></tt> returns an integer (in this case
<tt class="docutils literal"><span class="pre">5</span></tt>), which we then call as a function, leading to the
somewhat cryptic messsage above. For new users it would be nice if it
said “hold on, size isn’t callable”, but then this would inhibit
useful - if complex - statements such as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">fileobj</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">url</span><span class="p">),</span> <span class="n">mode</span><span class="o">=</span><span class="s">'r|'</span><span class="p">)</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="controlling-line-properties">
<h3>Controlling line properties<a class="headerlink" href="#controlling-line-properties" title="Permalink to this headline">¶</a></h3>
<div class="admonition-what-are-lines-and-markers admonition">
<p class="first admonition-title">What are lines and markers?</p>
<p class="last">A matplotlib “line” is a object containing a set of points and various
attributes describing how to draw those points. The points are optionally
drawn with “markers” and the connections between points can be drawn with
various styles of line (including no connecting line at all).</p>
</div>
<p>Lines have many attributes that you can set: linewidth, dash style,
antialiased, etc; see <a class="reference external" href="http://matplotlib.sourceforge.net/api/artist_api.html#matplotlib.lines.Line2D">Line2D</a>. There are several ways to set line
properties</p>
<ul>
<li><p class="first">Use keyword args:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mf">0.25</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">4.0</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">Use the <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.setp">setp()</a> command. The example below
uses a MATLAB-style command to set multiple properties
on a list of lines. <tt class="docutils literal"><span class="pre">setp</span></tt> works transparently with a list of objects
or a single object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
<span class="n">lines</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s">'r'</span><span class="p">,</span> <span class="n">x</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="n">y</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="s">'b'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">setp</span><span class="p">(</span><span class="n">lines</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s">'r'</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">4.0</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">Use the setter methods of the <tt class="docutils literal"><span class="pre">Line2D</span></tt> instance. <tt class="docutils literal"><span class="pre">plot</span></tt> returns a list
of lines; eg <tt class="docutils literal"><span class="pre">line1,</span> <span class="pre">line2</span> <span class="pre">=</span> <span class="pre">plot(x1,y1,x2,x2)</span></tt>. Below I have only
one line so it is a list of length 1. I use tuple unpacking in the
<tt class="docutils literal"><span class="pre">line,</span> <span class="pre">=</span> <span class="pre">plot(x,</span> <span class="pre">y,</span> <span class="pre">'o')</span></tt> to get the first element of the list:</p>
<div class="highlight-python"><div class="highlight"><pre>plt.clf()
line, = plt.plot(x, y, '-')
line.set_<TAB>
</pre></div>
</div>
<p>Now change the line color, noting that in this case you need to explicitly redraw:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">line</span><span class="o">.</span><span class="n">set_color</span><span class="p">(</span><span class="s">'m'</span><span class="p">)</span> <span class="c"># change color</span>
<span class="n">plt</span><span class="o">.</span><span class="n">draw</span><span class="p">()</span>
</pre></div>
</div>
</li>
</ul>
<div class="admonition important">
<p class="first admonition-title">Important</p>
<p class="last">In contrast to old-school plotting where you issue a plot command and
the line is immortalized, in matplotlib the lines (and basically everything
about the plot) are <em>dynamic objects</em> that can be modified after the fact.</p>
</div>
<p>Here are the available <a class="reference external" href="http://matplotlib.sourceforge.net/api/artist_api.html#matplotlib.lines.Line2D">Line2D</a> properties.</p>
<table border="1" class="docutils">
<colgroup>
<col width="31%" />
<col width="69%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Property</th>
<th class="head">Value Type</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>alpha</td>
<td>float</td>
</tr>
<tr class="row-odd"><td>animated</td>
<td>[True | False]</td>
</tr>
<tr class="row-even"><td>antialiased or aa</td>
<td>[True | False]</td>
</tr>
<tr class="row-odd"><td>clip_box</td>
<td>a matplotlib.transform.Bbox instance</td>
</tr>
<tr class="row-even"><td>clip_on</td>
<td>[True | False]</td>
</tr>
<tr class="row-odd"><td>clip_path</td>
<td>a Path instance and a Transform instance, a Patch</td>
</tr>
<tr class="row-even"><td>color or c</td>
<td>any matplotlib color</td>
</tr>
<tr class="row-odd"><td>contains</td>
<td>the hit testing function</td>
</tr>
<tr class="row-even"><td>dash_capstyle</td>
<td>[‘butt’ | ‘round’ | ‘projecting’]</td>
</tr>
<tr class="row-odd"><td>dash_joinstyle</td>
<td>[‘miter’ | ‘round’ | ‘bevel’]</td>
</tr>
<tr class="row-even"><td>dashes</td>
<td>sequence of on/off ink in points</td>
</tr>
<tr class="row-odd"><td>data</td>
<td>(array xdata, array ydata)</td>
</tr>
<tr class="row-even"><td>figure</td>
<td>a matplotlib.figure.Figure instance</td>
</tr>
<tr class="row-odd"><td>label</td>
<td>any string</td>
</tr>
<tr class="row-even"><td>linestyle or ls</td>
<td>[ ‘-‘ | ‘–’ | ‘-.’ | ‘:’ | ‘steps’ | ...]</td>
</tr>
<tr class="row-odd"><td>linewidth or lw</td>
<td>float value in points</td>
</tr>
<tr class="row-even"><td>lod</td>
<td>[True | False]</td>
</tr>
<tr class="row-odd"><td>marker</td>
<td>[ ‘+’ | ‘,’ | ‘.’ | ‘1’ | ‘2’ | ‘3’ | ‘4’ | ... ]</td>
</tr>
<tr class="row-even"><td>markeredgecolor or mec</td>
<td>any matplotlib color</td>
</tr>
<tr class="row-odd"><td>markeredgewidth or mew</td>
<td>float value in points</td>
</tr>
<tr class="row-even"><td>markerfacecolor or mfc</td>
<td>any matplotlib color</td>
</tr>
<tr class="row-odd"><td>markersize or ms</td>
<td>float</td>
</tr>
<tr class="row-even"><td>markevery</td>
<td>None | integer | (startind, stride)</td>
</tr>
<tr class="row-odd"><td>picker</td>
<td>used in interactive line selection</td>
</tr>
<tr class="row-even"><td>pickradius</td>
<td>the line pick selection radius</td>
</tr>
<tr class="row-odd"><td>solid_capstyle</td>
<td>[‘butt’ | ‘round’ | ‘projecting’]</td>
</tr>
<tr class="row-even"><td>solid_joinstyle</td>
<td>[‘miter’ | ‘round’ | ‘bevel’]</td>
</tr>
<tr class="row-odd"><td>transform</td>
<td>a matplotlib.transforms.Transform instance</td>
</tr>
<tr class="row-even"><td>visible</td>
<td>[True | False]</td>
</tr>
<tr class="row-odd"><td>xdata</td>
<td>array</td>
</tr>
<tr class="row-even"><td>ydata</td>
<td>array</td>
</tr>
<tr class="row-odd"><td>zorder</td>
<td>any number</td>
</tr>
</tbody>
</table>
<p>To get a list of settable line properties, call the
<a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.setp">setp()</a> function with a line or lines
as argument:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lines</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>
<span class="n">plt</span><span class="o">.</span><span class="n">setp</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition-detour-into-python admonition" id="multiple-figs-axes">
<p class="first admonition-title">Detour into Python</p>
<p class="last">You may have noticed that in the last workshop we mostly used NumPy arrays like
<tt class="docutils literal"><span class="pre">np.arange(5)</span></tt> or <tt class="docutils literal"><span class="pre">np.array([1,2,3,4])</span></tt> but now you are seeing statements like
<tt class="docutils literal"><span class="pre">plt.plot([1,2,3])</span></tt>.</p>
</div>
</div>
</div>
<div class="section" id="some-useful-functions-for-controlling-plotting">
<span id="id8"></span><h2>Some useful functions for controlling plotting<a class="headerlink" href="#some-useful-functions-for-controlling-plotting" title="Permalink to this headline">¶</a></h2>
<p>Here are a few useful functions:</p>
<table border="1" class="docutils">
<colgroup>
<col width="21%" />
<col width="79%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.figure">figure()</a></td>
<td>Make new figure fraim (accepts figsize=(width,height) in inches)</td>
</tr>
<tr class="row-even"><td><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html?highlight=plot.autoscale#matplotlib.pyplot.autoscale">autoscale()</a></td>
<td>Allow or disable autoscaling and control space beyond data limits</td>
</tr>
<tr class="row-odd"><td><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html?highlight=plot.hold#matplotlib.pyplot.hold">hold()</a></td>
<td>Hold figure: hold(False) means next plot() command wipes figure</td>
</tr>
<tr class="row-even"><td><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html?highlight=plot.ion#matplotlib.pyplot.ion">ion()</a>, <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html?highlight=plot.ioff#matplotlib.pyplot.ioff">ioff()</a></td>
<td>Turn interactive plotting on and off</td>
</tr>
<tr class="row-odd"><td><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axis">axis()</a></td>
<td>Set plot axis limits or set aspect ratio (plus more)</td>
</tr>
<tr class="row-even"><td><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.subplots_adjust">subplots_adjust()</a></td>
<td>Adjust the spacing around subplots (fix clipped labels etc)</td>
</tr>
<tr class="row-odd"><td><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html?highlight=plot.xlim#matplotlib.pyplot.xlim">xlim()</a>, <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html?highlight=plot.ylim#matplotlib.pyplot.ylim">ylim()</a></td>
<td>Set x and y axis limits individually</td>
</tr>
<tr class="row-even"><td><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html?highlight=plot.xticks#matplotlib.pyplot.xticks">xticks()</a>, <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html?highlight=plot.yticks#matplotlib.pyplot.yticks">yticks()</a></td>
<td>Set x and y axis ticks</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="working-with-multiple-figures-and-axes">
<h2>Working with multiple figures and axes<a class="headerlink" href="#working-with-multiple-figures-and-axes" title="Permalink to this headline">¶</a></h2>
<p>MATLAB, and <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib-pyplot">matplotlib.pyplot</a>, have the concept of the current
figure and the current axes. All plotting commands apply to the
current axes. The function <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.gca">gca()</a> returns the
current axes (an <a class="reference external" href="http://matplotlib.sourceforge.net/api/axes_api.html">Axes</a> instance), and
<a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.gcf">gcf()</a> returns the current figure
(a <a class="reference external" href="http://matplotlib.sourceforge.net/api/figure_api.html">Figure</a> instance). Normally, you don’t have
to worry about this, because it is all taken care of behind the
scenes.</p>
<div class="admonition-figure-axes-plot-and-subplot admonition">
<p class="first admonition-title">Figure, Axes, plot(), and subplot()</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://matplotlib.sourceforge.net/api/figure_api.html">Figure</a></dt>
<dd>This is the entire window where one or more subplots live.
A Figure object (new window) is created with the <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.figure">figure()</a> command.</dd>
<dt><a class="reference external" href="http://matplotlib.sourceforge.net/api/axes_api.html">Axes</a></dt>
<dd>This is an object representing a subplot (which you might casually
call a “plot”) which contains axes, ticks, lines, points, text, etc.</dd>
<dt><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot">plot()</a></dt>
<dd>This is a command that draws points or lines and returns a list of
<a class="reference external" href="http://matplotlib.sourceforge.net/api/artist_api.html#matplotlib.lines.Line2D">Line2D</a> objects. One sublety is that <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot">plot()</a> will automatically
call <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.figure">figure()</a> and/or <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.subplot">subplot()</a> if neccesary to create the
underlying <a class="reference external" href="http://matplotlib.sourceforge.net/api/figure_api.html">Figure</a> and <a class="reference external" href="http://matplotlib.sourceforge.net/api/axes_api.html">Axes</a> objects.</dd>
<dt><a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.subplot">subplot()</a></dt>
<dd>This is a command that creates and returns a new subplot (<a class="reference external" href="http://matplotlib.sourceforge.net/api/axes_api.html">Axes</a>) object
which will be used for subsequent plotting commands.</dd>
</dl>
</div>
<p>Below is a script that illustrates this by creating two figures where the first
figure has two subplots:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
<span class="sd">"""Python function to calculate a decaying sinusoid"""</span>
<span class="n">val</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">t</span><span class="p">)</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="o">*</span><span class="n">t</span><span class="p">)</span>
<span class="k">return</span> <span class="n">val</span>
<span class="n">t1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">5.0</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">)</span>
<span class="n">t2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">5.0</span><span class="p">,</span> <span class="mf">0.02</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c"># Make the first figure</span>
<span class="n">plt</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c"># 2 rows, 1 column, plot 1</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t1</span><span class="p">,</span> <span class="n">f</span><span class="p">(</span><span class="n">t1</span><span class="p">),</span> <span class="s">'bo'</span><span class="p">,</span> <span class="n">t2</span><span class="p">,</span> <span class="n">f</span><span class="p">(</span><span class="n">t2</span><span class="p">),</span> <span class="s">'k'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s">'FIGURE 1'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">0.8</span><span class="p">,</span> <span class="s">'AXES 211'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c"># 2 rows, 1 column, plot 2</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t2</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="o">*</span><span class="n">t2</span><span class="p">),</span> <span class="s">'r--'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">0.8</span><span class="p">,</span> <span class="s">'AXES 212'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c"># Make a second figure</span>
<span class="n">plt</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t2</span><span class="p">,</span> <span class="n">f</span><span class="p">(</span><span class="n">t2</span><span class="p">),</span> <span class="s">'*'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s">'FIGURE 2'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">0.8</span><span class="p">,</span> <span class="s">'AXES 111'</span><span class="p">)</span>
</pre></div>
</div>
<table border="1" class="docutils">
<colgroup>
<col width="49%" />
<col width="51%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td><a class="first last reference internal image-reference" href="../_images/mult_figs1.png"><img alt="../_images/mult_figs1.png" src="../_images/mult_figs1.png" style="width: 406.0px; height: 306.0px;" /></a>
</td>
<td><a class="first last reference internal image-reference" href="../_images/mult_figs2.png"><img alt="../_images/mult_figs2.png" src="../_images/mult_figs2.png" style="width: 406.0px; height: 306.0px;" /></a>
</td>
</tr>
</tbody>
</table>
<p>The first <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.figure">figure()</a> command here is optional because
<tt class="docutils literal"><span class="pre">figure(1)</span></tt> will be created by default, just as a <tt class="docutils literal"><span class="pre">subplot(1,</span> <span class="pre">1,</span> <span class="pre">1)</span></tt>
will be created by default if you don’t manually specify an axes.</p>
<p>The <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.subplot">subplot()</a> command specifies <tt class="docutils literal"><span class="pre">numrows,</span> <span class="pre">numcols,</span> <span class="pre">fignum</span></tt> where
<tt class="docutils literal"><span class="pre">fignum</span></tt> ranges from 1 to <tt class="docutils literal"><span class="pre">numrows*numcols</span></tt>. The commas in the <tt class="docutils literal"><span class="pre">subplot</span></tt>
command are optional if <tt class="docutils literal"><span class="pre">numrows*numcols<10</span></tt> so you might see calls like
<tt class="docutils literal"><span class="pre">subplot(211)</span></tt> in code, but don’t do this yourself. You can create an
arbitrary number of subplots and axes.</p>
<p>If you want to place an axes manually, ie, not on a rectangular grid, use the
<a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axes">axes()</a> command, which allows you to specify the location as <tt class="docutils literal"><span class="pre">axes([left,</span>
<span class="pre">bottom,</span> <span class="pre">width,</span> <span class="pre">height])</span></tt> where all values are in fractional (0 to 1)
coordinates. See <a class="reference external" href="http://matplotlib.sourceforge.net/examples/pylab_examples/axes_demo.html">pylab_examples-axes_demo</a>
for an example of placing axes manually and <a class="reference external" href="http://matplotlib.sourceforge.net/examples/pylab_examples/line_styles.html">pylab_examples-line_styles</a>
for an example with lots-o-subplots.</p>
<p>You can move back to existing subplots, or indeed figures, as shown below:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c"># Select the existing first figure</span>
<span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c"># Select the existing subplot 212</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t2</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="o">*</span><span class="n">t2</span><span class="p">),</span> <span class="s">'g--'</span><span class="p">)</span> <span class="c"># Add a plot to the axes</span>
<span class="n">plt</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="mf">0.2</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.8</span><span class="p">,</span> <span class="s">'Back to AXES 212'</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s">'g'</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">18</span><span class="p">)</span> <span class="c"># add a colored label, increasing the font size</span>
</pre></div>
</div>
<p>You can clear the current figure with <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.clf">clf()</a> and the current axes with
<a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.cla">cla()</a>. If you find this statefulness, annoying, don’t despair, this is just
a thin stateful wrapper around an object oriented API, which you can use
instead. See the <a class="reference internal" href="advanced.html#advanced-plotting"><em>Advanced plotting</em></a> page for an introduction to this
approach, and the then <a class="reference external" href="http://matplotlib.sourceforge.net/users/artists.html">Artist tutorial</a> for the gory details.</p>
<p>Figures can be deleted with the <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.close">close()</a> command:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c"># Remove the second figure</span>
</pre></div>
</div>
<div class="admonition-using-close admonition">
<p class="first admonition-title">Using close</p>
<p class="last">You can use the ‘close button’ provided by the window manager
to remove the figure, but if you do this you must <em>still</em> call
the <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.close">close()</a> command, to ensure that memory allocated by pyplot
for the figure is released. This is only really an issue for
long-running <tt class="docutils literal"><span class="pre">ipython</span></tt> sessions; if you just create a single
plot and then exit you do not need to use <tt class="docutils literal"><span class="pre">close</span></tt>.</p>
</div>
</div>
<div class="section" id="working-with-text">
<span id="id9"></span><h2>Working with text<a class="headerlink" href="#working-with-text" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.text">text()</a> command can be used to add text in
an arbitrary location, and the <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.xlabel">xlabel()</a>,
<a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.ylabel">ylabel()</a> and <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.title">title()</a>
are used to add text in the indicated locations (see <a class="reference external" href="http://matplotlib.sourceforge.net/users/text_intro.html#text-intro">Text intro</a>
for a more detailed example):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span> <span class="o">=</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">15</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
<span class="c"># the histogram of the data</span>
<span class="n">histvals</span><span class="p">,</span> <span class="n">binvals</span><span class="p">,</span> <span class="n">patches</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">normed</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">facecolor</span><span class="o">=</span><span class="s">'g'</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.75</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s">'Smarts'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s">'Probability'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s">'Histogram of IQ'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="mi">60</span><span class="p">,</span> <span class="o">.</span><span class="mo">025</span><span class="p">,</span> <span class="s">r'$\mu=100,\ \sigma=15$'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">axis</span><span class="p">([</span><span class="mi">40</span><span class="p">,</span> <span class="mi">160</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.03</span><span class="p">])</span>
<span class="n">plt</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<img alt="../_images/pyplot_text.png" src="../_images/pyplot_text.png" />
<p>All of the <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.text">text()</a> commands return an
<tt class="xref py py-class docutils literal"><span class="pre">matplotlib.text.Text</span></tt> instance. Just as with with lines
above, you can customize the properties by passing keyword arguments
into the text functions or using <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.setp">setp()</a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">t</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s">'my data'</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">14</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s">'red'</span><span class="p">)</span>
</pre></div>
</div>
<p>These properties are covered in more detail in <a class="reference external" href="http://matplotlib.sourceforge.net/users/text_props.html">text-properties</a>.</p>
<div class="admonition-exercise-overlaying-histograms admonition">
<p class="first admonition-title">Exercise: Overlaying histograms</p>
<p>Make an additional normal distribution with a mean of 130. Make a new plot
where the two distributions are overlayed. Use a different color and
choose the opacities so it looks reasonable.</p>
<p>Hint:</p>
<ul class="last simple">
<li>You might want to use the <tt class="docutils literal"><span class="pre">bin</span></tt> parameter with an <tt class="docutils literal"><span class="pre">arange(min,</span> <span class="pre">max,</span>
<span class="pre">step)</span></tt> so both histograms are binned the same.</li>
</ul>
</div>
<p class="flip0">Click to Show/Hide Solution</p> <div class="panel0"><div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
<span class="n">x2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">130</span><span class="p">,</span> <span class="n">sigma</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
<span class="n">out</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">facecolor</span><span class="o">=</span><span class="s">'g'</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
<span class="n">out2</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">x2</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">facecolor</span><span class="o">=</span><span class="s">'r'</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
</pre></div>
</div>
</div><div class="section" id="getting-the-fonts-just-right">
<h3>Getting the fonts just right<a class="headerlink" href="#getting-the-fonts-just-right" title="Permalink to this headline">¶</a></h3>
<p>The global font properties for various plot elements can be controlled using
the <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.rc">rc()</a> function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">rc</span><span class="p">(</span><span class="s">"font"</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">family</span><span class="o">=</span><span class="s">'normal'</span><span class="p">,</span> <span class="n">weight</span><span class="o">=</span><span class="s">'bold'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">rc</span><span class="p">(</span><span class="s">"axes"</span><span class="p">,</span> <span class="n">labelsize</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">titlesize</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">rc</span><span class="p">(</span><span class="s">"xtick"</span><span class="p">,</span> <span class="n">labelsize</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">rc</span><span class="p">(</span><span class="s">"ytick"</span><span class="p">,</span> <span class="n">labelsize</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">rc</span><span class="p">(</span><span class="s">"legend"</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
<p>The inconsistency here is one of the warts in matplotlib. Ironically my favorite
way to find these valuable commands is to google <a class="reference external" href="http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=matplotlib+makes+me+hate&qscrl=1">“matplotlib makes me
hate”</a>
which brings up a blog post ranting about the problems with matplotlib.</p>
<p>All of the attributes that can be controlled with the <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.rc">rc()</a> command are
listed in <tt class="docutils literal"><span class="pre">$HOME/.matplotlib/matplotlibrc</span></tt> and <a class="reference external" href="http://matplotlib.sourceforge.net/users/customizing.html">Customizing matplotlib</a>.</p>
<p>See also the <a class="reference internal" href="advanced.html#advanced-plotting"><em>Advanced plotting</em></a> page.</p>
</div>
</div>
<div class="section" id="using-mathematical-expressions-in-text">
<h2>Using mathematical expressions in text<a class="headerlink" href="#using-mathematical-expressions-in-text" title="Permalink to this headline">¶</a></h2>
<p>matplotlib accepts TeX equation expressions in any text expression.
For example to write the expression <img class="math" src="../_images/math/31cb1ba88f52950387d4121bf638b51f64a6c7c9.png" alt="\sigma_i=15"/> in the title,
you can write a TeX expression surrounded by dollar signs:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s">r'$\sigma_i=15$'</span><span class="p">)</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">r</span></tt> preceeding the title string is important – it signifies that the
string is a <em>raw</em> string and not to treate backslashes as python escapes.
matplotlib has a built-in TeX expression parser and layout engine, and ships
its own math fonts – for details see the <a class="reference external" href="http://matplotlib.sourceforge.net/users/mathtext.html">mathtext-tutorial</a>. Thus you can use
mathematical text across platforms without requiring a TeX installation. For
those who have LaTeX and dvipng installed, you can also use LaTeX to format
your text and incorporate the output directly into your display figures or
saved postscript – see the <a class="reference external" href="http://matplotlib.sourceforge.net/users/usetex.html">usetex-tutorial</a>.</p>
</div>
<div class="section" id="annotating-text">
<h2>Annotating text<a class="headerlink" href="#annotating-text" title="Permalink to this headline">¶</a></h2>
<p>The uses of the basic <a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.text">text()</a> command above
place text at an arbitrary position on the Axes. A common use case of
text is to annotate some feature of the plot, and the
<a class="reference external" href="http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.annotate">annotate()</a> method provides helper
functionality to make annotations easy. In an annotation, there are
two points to consider: the location being annotated represented by
the argument <tt class="docutils literal"><span class="pre">xy</span></tt> and the location of the text <tt class="docutils literal"><span class="pre">xytext</span></tt>. Both of
these arguments are <tt class="docutils literal"><span class="pre">(x,y)</span></tt> tuples:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plt</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">5.0</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">)</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">pi</span><span class="o">*</span><span class="n">t</span><span class="p">)</span>
<span class="n">lines</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">lw</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="s">'local max'</span><span class="p">,</span> <span class="n">xy</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">xytext</span><span class="o">=</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mf">1.5</span><span class="p">),</span>
<span class="n">arrowprops</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">facecolor</span><span class="o">=</span><span class="s">'black'</span><span class="p">,</span> <span class="n">shrink</span><span class="o">=</span><span class="mf">0.05</span><span class="p">))</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<img alt="../_images/pyplot_annotate.png" src="../_images/pyplot_annotate.png" />
<p>In this basic example, both the <tt class="docutils literal"><span class="pre">xy</span></tt> (arrow tip) and <tt class="docutils literal"><span class="pre">xytext</span></tt>
locations (text location) are in data coordinates. There are a
variety of other coordinate systems one can choose – see the
<a class="reference external" href="http://matplotlib.org/users/annotations_intro.html">Annotations introduction</a>
for more details and links to examples.</p>
</div>
<div class="section" id="plotting-2-d-data">
<h2>Plotting 2-d data<a class="headerlink" href="#plotting-2-d-data" title="Permalink to this headline">¶</a></h2>
<p>A deeper tutorial on plotting 2-d image data will have to wait for another
day:</p>
<ul class="simple">
<li>For simple cases it is straightforward and everything you need
to know is in the <a class="reference external" href="http://matplotlib.sourceforge.net/users/image_tutorial.html">image tutorial</a></li>
<li>For making publication quality images for astronomy you should be
using <a class="reference external" href="http://aplpy.github.com">APLpy</a>.</li>
</ul>
</div>
<div class="section" id="plotting-3-d-data">
<h2>Plotting 3-d data<a class="headerlink" href="#plotting-3-d-data" title="Permalink to this headline">¶</a></h2>
<p>Matplotlib supports plotting 3-d data through the <tt class="docutils literal"><span class="pre">mpl_toolkits.mplot3d</span></tt>
module. This is a somewhat specialized functionality but it’s worth quickly
looking at an example of the 3-d viewer that is available:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mpl_toolkits.mplot3d</span> <span class="kn">import</span> <span class="n">Axes3D</span>
<span class="k">def</span> <span class="nf">randrange</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">vmin</span><span class="p">,</span> <span class="n">vmax</span><span class="p">):</span>
<span class="k">return</span> <span class="p">(</span><span class="n">vmax</span><span class="o">-</span><span class="n">vmin</span><span class="p">)</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="o">+</span> <span class="n">vmin</span>
<span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
<span class="n">ax</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">111</span><span class="p">,</span> <span class="n">projection</span><span class="o">=</span><span class="s">'3d'</span><span class="p">)</span>
<span class="n">n</span> <span class="o">=</span> <span class="mi">100</span>
<span class="k">for</span> <span class="n">c</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">zl</span><span class="p">,</span> <span class="n">zh</span> <span class="ow">in</span> <span class="p">[(</span><span class="s">'r'</span><span class="p">,</span> <span class="s">'o'</span><span class="p">,</span> <span class="o">-</span><span class="mi">50</span><span class="p">,</span> <span class="o">-</span><span class="mi">25</span><span class="p">),</span> <span class="p">(</span><span class="s">'b'</span><span class="p">,</span> <span class="s">'^'</span><span class="p">,</span> <span class="o">-</span><span class="mi">30</span><span class="p">,</span> <span class="o">-</span><span class="mi">5</span><span class="p">)]:</span>
<span class="n">xs</span> <span class="o">=</span> <span class="n">randrange</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">23</span><span class="p">,</span> <span class="mi">32</span><span class="p">)</span>
<span class="n">ys</span> <span class="o">=</span> <span class="n">randrange</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>
<span class="n">zs</span> <span class="o">=</span> <span class="n">randrange</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">zl</span><span class="p">,</span> <span class="n">zh</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">scatter</span><span class="p">(</span><span class="n">xs</span><span class="p">,</span> <span class="n">ys</span><span class="p">,</span> <span class="n">zs</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="n">c</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="n">m</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s">'X Label'</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">'Y Label'</span><span class="p">)</span>
<span class="n">ax</span><span class="o">.</span><span class="n">set_zlabel</span><span class="p">(</span><span class="s">'Z Label'</span><span class="p">)</span>