Skip to content

Commit ab40b8a

Browse files
authored
Merge pull request #20 from matplotlib/minor_edits
Minor edits
2 parents 9c61c2e + 5d4d1ee commit ab40b8a

8 files changed

+130
-204
lines changed

AnatomyOfMatplotlib-Part1-Figures_Subplots_and_layouts.ipynb

+66-78
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,33 @@
2525
"source": [
2626
"# Matplotlib\n",
2727
"## Introduction\n",
28-
"Matplotlib is a library for producing publication-quality figures. mpl (for short) was designed from the beginning to serve two purposes. First, allow for interactive, cross-platform control of figures and plots, and second, to make it very easy to produce static raster or vector graphics files without the need for any GUIs. Furthermore, mpl -- much like Python itself -- gives the developer complete control over the appearance of their plots, while still being very usable through a powerful defaults system."
28+
"\n",
29+
"Matplotlib is a library for producing publication-quality figures. mpl (for short) was designed from the beginning to serve two purposes: \n",
30+
"\n",
31+
" 1. allow for interactive, cross-platform control of figures and plots\n",
32+
" 2. make it easy to produce static raster or vector graphics files without the need for any GUIs. \n",
33+
" \n",
34+
"Furthermore, mpl -- much like Python itself -- gives the developer complete control over the appearance of their plots, while still being very usable through a powerful defaults system."
2935
]
3036
},
3137
{
3238
"cell_type": "markdown",
3339
"metadata": {},
3440
"source": [
3541
"## Online Documentation\n",
36-
"The [matplotlib.org](http://matplotlib.org) project website is the primary online resource for the library's documentation. It contains [examples](http://matplotlib.org/examples/index.html), [FAQs](http://matplotlib.org/faq/index.html), [API documentation](http://matplotlib.org/api/index.html), and, most importantly, the [gallery](http://matplotlib.org/gallery.html).\n",
42+
"The [matplotlib.org](http://matplotlib.org) project website is the primary online resource for the library's documentation. It contains the [example galleries](https://matplotlib.org/gallery/index.html), [FAQs](http://matplotlib.org/faq/index.html), [API documentation](http://matplotlib.org/api/index.html), and [tutorials](https://matplotlib.org/tutorials/index.html).\n",
3743
"\n",
3844
"## Gallery\n",
39-
"Many users of Matplotlib are often faced with the question, \"I want to make a figure that has X with Y in the same figure, but it needs to look like Z\". Good luck getting an answer from a web search with that query! This is why the [gallery](http://matplotlib.org/gallery.html) is so useful, because it showcases the variety of ways one can make figures. Browse through the gallery, click on any figure that has pieces of what you want to see and the code that generated it. Soon enough, you will be like a chef, mixing and matching components to produce your masterpiece!\n",
45+
"Many users of Matplotlib are often faced with the question, \"I want to make a figure that has X with Y in the same figure, but it needs to look like Z\". Good luck getting an answer from a web search with that query! This is why the [gallery](https://matplotlib.org/gallery/index.html) is so useful, because it showcases the variety of ways one can make figures. Browse through the gallery, click on any figure that has pieces of what you want to see and the code that generated it. Soon enough, you will be like a chef, mixing and matching components to produce your masterpiece!\n",
4046
"\n",
4147
"As always, if you have a new and interesting plot that demonstrates a feature of Matplotlib, feel free to submit a concise, well-commented version of the code for inclusion in the gallery.\n",
4248
"\n",
43-
"## Mailing Lists and StackOverflow\n",
44-
"When you are just simply stuck, and cannot figure out how to get something to work, or just need some hints on how to get started, you will find much of the community at the matplotlib-users [mailing list](https://mail.python.org/mailman/listinfo/matplotlib-users). This mailing list is an excellent resource of information with many friendly members who just love to help out newcomers. The number one rule to remember with this list is to be persistant. While many questions do get answered fairly quickly, some do fall through the cracks, or the one person who knows the answer isn't available. Therefore, try again with your questions rephrased, or with a plot showing your attempts so far. We love plots, so an image showing what is wrong often gets the quickest responses.\n",
49+
"## Mailing Lists, StackOverflow, and gitter\n",
50+
"When you are just simply stuck, and cannot figure out how to get something to work, or just need some hints on how to get started, you will find much of the community at the matplotlib-users [mailing list](https://mail.python.org/mailman/listinfo/matplotlib-users). This mailing list is an excellent resource of information with many friendly members who just love to help out newcomers. We love plots, so an image showing what is wrong often gets the quickest responses.\n",
4551
"\n",
46-
"Another community resource is [StackOverflow](http://stackoverflow.com/questions/tagged/matplotlib), so if you need to build up karma points, submit your questions here, and help others out too! We are also on [Gitter](https://gitter.im/matplotlib/matplotlib).\n",
52+
"Another community resource is [StackOverflow](http://stackoverflow.com/questions/tagged/matplotlib), so if you need to build up karma points, submit your questions here, and help others out too! \n",
53+
"\n",
54+
"We are also on [Gitter](https://gitter.im/matplotlib/matplotlib).\n",
4755
"\n",
4856
"## Github repository\n",
4957
"### Location\n",
@@ -58,15 +66,16 @@
5866
"metadata": {},
5967
"source": [
6068
"# Quick note on \"backends\" and Jupyter notebooks\n",
61-
"Matplotlib has multiple backends. The backends allow mpl to be used on a variety of platforms with a variety of GUI toolkits (GTK, Qt, Wx, etc.), all of them written so that most of the time, you will not need to care which backend you are using. "
69+
"\n",
70+
"Matplotlib has multiple \"backends\" that handle converting Matplotlib's in-memory representation of your plot into the colorful output you can look at. This is done either by writing files (e.g., png, svg, pdf) that you can use an external tool to look at or by embedding into your GUI toolkit of choice (Qt, Tk, Wx, etc).\n",
71+
"\n",
72+
"To check what backend Matplotlib is currently using:"
6273
]
6374
},
6475
{
6576
"cell_type": "code",
6677
"execution_count": null,
67-
"metadata": {
68-
"collapsed": false
69-
},
78+
"metadata": {},
7079
"outputs": [],
7180
"source": [
7281
"import matplotlib\n",
@@ -78,32 +87,33 @@
7887
"cell_type": "markdown",
7988
"metadata": {},
8089
"source": [
81-
"Normally we wouldn't need to think about this too much, but IPython/Jupyter notebooks behave a touch differently than \"normal\" python.\n",
90+
"If you are working interactively at an (I)python prompt, the GUI framework is not critical (mostly aesthetic) however when working in Jupyter we need to pick a backend that integrates with Jupyter (javascript) framework.\n",
8291
"\n",
83-
"Inside of IPython, it's often easiest to use the Jupyter ``nbagg`` or ``notebook`` backend. This allows plots to be displayed and interacted with in the browser in a Jupyter notebook. Otherwise, figures will pop up in a separate GUI window.\n",
84-
"\n",
85-
"We can do this in two ways:\n",
86-
"\n",
87-
"1. The IPython ``%matplotlib backend_name`` \"magic\" command (or ``plt.ion()``, which behaves similarly)\n",
88-
" - Figures will be shown automatically by IPython, even if you don't call ``plt.show()``.\n",
89-
" \n",
90-
"2. ``matplotlib.use(\"backend_name\")``\n",
91-
" - Figures will only be shown when you call ``plt.show()``.\n",
92-
"\n",
93-
"Here, we'll use the second method for one simple reason: it allows our code to behave the same way regardless of whether we run it inside of an Jupyter notebook or from a python script at the command line. Feel free to use the ``%matplotlib`` magic command if you'd prefer.\n",
94-
"\n",
95-
"One final note: You will always need to do this before you ``import matplotlib.pyplot as plt``."
92+
"To select the backend use ``matplotlib.use(\"backend_name\")``, in this case we want ``'nbagg'``\n"
9693
]
9794
},
9895
{
9996
"cell_type": "code",
10097
"execution_count": null,
101-
"metadata": {
102-
"collapsed": false
103-
},
98+
"metadata": {},
10499
"outputs": [],
105100
"source": [
106-
"matplotlib.use('nbagg')"
101+
"matplotlib.use('nbagg')\n",
102+
"print(matplotlib.get_backend())"
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"metadata": {},
108+
"source": [
109+
"which must be done *before* you `import matplotlib.pyplot as plt`.\n",
110+
"\n",
111+
"You can also set the backend via an 'ipython magic' ``%matplotlib backend_name``. In addition to setting the backend, the magic also calls `plt.ion()`, which puts Matplotlib in 'interacitve mode' (the inverse is `plt.ioff()`). In 'interactive mode' figures are shown (injected into the web page in the notebook) as soon as they are created. Otherwise, figures are not shown until you explicitly call `plt.show()`.\n",
112+
"\n",
113+
"\n",
114+
"In these tutorials we will mostly work in non-interactive mode for better control of when\n",
115+
"figures are shown in the notebooks.\n",
116+
"This also better mimics the behavior you can expect in regular python scripts.\n"
107117
]
108118
},
109119
{
@@ -138,9 +148,7 @@
138148
{
139149
"cell_type": "code",
140150
"execution_count": null,
141-
"metadata": {
142-
"collapsed": false
143-
},
151+
"metadata": {},
144152
"outputs": [],
145153
"source": [
146154
"import numpy as np\n",
@@ -159,12 +167,10 @@
159167
{
160168
"cell_type": "code",
161169
"execution_count": null,
162-
"metadata": {
163-
"collapsed": false
164-
},
170+
"metadata": {},
165171
"outputs": [],
166172
"source": [
167-
"fig = plt.figure()"
173+
"fig = plt.figure(facecolor=(1, 0, 0, .1)) # red background to see where the figure is"
168174
]
169175
},
170176
{
@@ -179,9 +185,7 @@
179185
{
180186
"cell_type": "code",
181187
"execution_count": null,
182-
"metadata": {
183-
"collapsed": false
184-
},
188+
"metadata": {},
185189
"outputs": [],
186190
"source": [
187191
"plt.show()"
@@ -193,6 +197,7 @@
193197
"source": [
194198
"Great, a blank figure! Not terribly useful yet.\n",
195199
"\n",
200+
"\n",
196201
"However, while we're on the topic, you can control the size of the figure through the ``figsize`` argument, which expects a tuple of ``(width, height)`` in inches. \n",
197202
"\n",
198203
"A really useful utility function is [`figaspect`](http://matplotlib.org/api/figure_api.html?highlight=figaspect#matplotlib.figure.figaspect)"
@@ -201,13 +206,11 @@
201206
{
202207
"cell_type": "code",
203208
"execution_count": null,
204-
"metadata": {
205-
"collapsed": false
206-
},
209+
"metadata": {},
207210
"outputs": [],
208211
"source": [
209212
"# Twice as tall as it is wide:\n",
210-
"fig = plt.figure(figsize=plt.figaspect(2.0))\n",
213+
"fig = plt.figure(figsize=plt.figaspect(2.0), facecolor=(1, 0, 0, .1))\n",
211214
"plt.show()"
212215
]
213216
},
@@ -227,9 +230,7 @@
227230
{
228231
"cell_type": "code",
229232
"execution_count": null,
230-
"metadata": {
231-
"collapsed": false
232-
},
233+
"metadata": {},
233234
"outputs": [],
234235
"source": [
235236
"fig = plt.figure()\n",
@@ -254,16 +255,15 @@
254255
{
255256
"cell_type": "code",
256257
"execution_count": null,
257-
"metadata": {
258-
"collapsed": false
259-
},
258+
"metadata": {},
260259
"outputs": [],
261260
"source": [
262261
"ax.set_xlim([0.5, 4.5])\n",
263262
"ax.set_ylim([-2, 8])\n",
264-
"ax.set_title('An Example Axes')\n",
265-
"ax.set_ylabel('Y-Axis')\n",
266-
"ax.set_xlabel('X-Axis')"
263+
"ax.set_title('A Different Example Axes Title')\n",
264+
"ax.set_ylabel('Y-Axis (changed)')\n",
265+
"ax.set_xlabel('X-Axis (changed)')\n",
266+
"plt.show()"
267267
]
268268
},
269269
{
@@ -295,15 +295,13 @@
295295
{
296296
"cell_type": "code",
297297
"execution_count": null,
298-
"metadata": {
299-
"collapsed": false
300-
},
298+
"metadata": {},
301299
"outputs": [],
302300
"source": [
303301
"fig = plt.figure()\n",
304302
"ax = fig.add_subplot(111)\n",
305303
"ax.plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)\n",
306-
"ax.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], color='darkgreen', marker='^')\n",
304+
"ax.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], c=[1, 2, 3, 5], marker='^')\n",
307305
"ax.set_xlim(0.5, 4.5)\n",
308306
"plt.show()"
309307
]
@@ -320,13 +318,11 @@
320318
{
321319
"cell_type": "code",
322320
"execution_count": null,
323-
"metadata": {
324-
"collapsed": false
325-
},
321+
"metadata": {},
326322
"outputs": [],
327323
"source": [
328324
"plt.plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)\n",
329-
"plt.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], color='darkgreen', marker='^')\n",
325+
"plt.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], c=[1, 2, 3, 5], marker='^')\n",
330326
"plt.xlim(0.5, 4.5)\n",
331327
"plt.show()"
332328
]
@@ -335,7 +331,7 @@
335331
"cell_type": "markdown",
336332
"metadata": {},
337333
"source": [
338-
"Much cleaner, and much clearer! So, why will most of my examples not follow the pyplot approach? Because [PEP20](http://www.python.org/dev/peps/pep-0020/) \"The Zen of Python\" says:\n",
334+
"That is a bit terser and has fewer local varialbes, so, why will most of my examples not follow the pyplot approach? Because [PEP20](http://www.python.org/dev/peps/pep-0020/) \"The Zen of Python\" says:\n",
339335
"\n",
340336
"\"Explicit is better than implicit\"\n",
341337
"\n",
@@ -358,9 +354,7 @@
358354
{
359355
"cell_type": "code",
360356
"execution_count": null,
361-
"metadata": {
362-
"collapsed": false
363-
},
357+
"metadata": {},
364358
"outputs": [],
365359
"source": [
366360
"fig, axes = plt.subplots(nrows=2, ncols=2)\n",
@@ -381,9 +375,7 @@
381375
{
382376
"cell_type": "code",
383377
"execution_count": null,
384-
"metadata": {
385-
"collapsed": false
386-
},
378+
"metadata": {},
387379
"outputs": [],
388380
"source": [
389381
"fig, axes = plt.subplots(nrows=2, ncols=2)\n",
@@ -441,9 +433,7 @@
441433
{
442434
"cell_type": "code",
443435
"execution_count": null,
444-
"metadata": {
445-
"collapsed": false
446-
},
436+
"metadata": {},
447437
"outputs": [],
448438
"source": [
449439
"%load exercises/1.1-subplots_and_basic_plotting.py"
@@ -452,9 +442,7 @@
452442
{
453443
"cell_type": "code",
454444
"execution_count": null,
455-
"metadata": {
456-
"collapsed": false
457-
},
445+
"metadata": {},
458446
"outputs": [],
459447
"source": [
460448
"import numpy as np\n",
@@ -473,23 +461,23 @@
473461
],
474462
"metadata": {
475463
"kernelspec": {
476-
"display_name": "Python 2",
464+
"display_name": "Python 3",
477465
"language": "python",
478-
"name": "python2"
466+
"name": "python3"
479467
},
480468
"language_info": {
481469
"codemirror_mode": {
482470
"name": "ipython",
483-
"version": 2
471+
"version": 3
484472
},
485473
"file_extension": ".py",
486474
"mimetype": "text/x-python",
487475
"name": "python",
488476
"nbconvert_exporter": "python",
489-
"pygments_lexer": "ipython2",
490-
"version": "2.7.12"
477+
"pygments_lexer": "ipython3",
478+
"version": "3.6.5"
491479
}
492480
},
493481
"nbformat": 4,
494-
"nbformat_minor": 0
482+
"nbformat_minor": 1
495483
}

0 commit comments

Comments
 (0)