Skip to content

Minor edits #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 66 additions & 78 deletions AnatomyOfMatplotlib-Part1-Figures_Subplots_and_layouts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,33 @@
"source": [
"# Matplotlib\n",
"## Introduction\n",
"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."
"\n",
"Matplotlib is a library for producing publication-quality figures. mpl (for short) was designed from the beginning to serve two purposes: \n",
"\n",
" 1. allow for interactive, cross-platform control of figures and plots\n",
" 2. make it easy to produce static raster or vector graphics files without the need for any GUIs. \n",
" \n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Online Documentation\n",
"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",
"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",
"\n",
"## Gallery\n",
"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",
"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",
"\n",
"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",
"\n",
"## Mailing Lists and StackOverflow\n",
"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",
"## Mailing Lists, StackOverflow, and gitter\n",
"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",
"\n",
"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",
"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",
"\n",
"We are also on [Gitter](https://gitter.im/matplotlib/matplotlib).\n",
"\n",
"## Github repository\n",
"### Location\n",
Expand All @@ -58,15 +66,16 @@
"metadata": {},
"source": [
"# Quick note on \"backends\" and Jupyter notebooks\n",
"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. "
"\n",
"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",
"\n",
"To check what backend Matplotlib is currently using:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"import matplotlib\n",
Expand All @@ -78,32 +87,33 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Normally we wouldn't need to think about this too much, but IPython/Jupyter notebooks behave a touch differently than \"normal\" python.\n",
"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",
"\n",
"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",
"\n",
"We can do this in two ways:\n",
"\n",
"1. The IPython ``%matplotlib backend_name`` \"magic\" command (or ``plt.ion()``, which behaves similarly)\n",
" - Figures will be shown automatically by IPython, even if you don't call ``plt.show()``.\n",
" \n",
"2. ``matplotlib.use(\"backend_name\")``\n",
" - Figures will only be shown when you call ``plt.show()``.\n",
"\n",
"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",
"\n",
"One final note: You will always need to do this before you ``import matplotlib.pyplot as plt``."
"To select the backend use ``matplotlib.use(\"backend_name\")``, in this case we want ``'nbagg'``\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"matplotlib.use('nbagg')"
"matplotlib.use('nbagg')\n",
"print(matplotlib.get_backend())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"which must be done *before* you `import matplotlib.pyplot as plt`.\n",
"\n",
"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",
"\n",
"\n",
"In these tutorials we will mostly work in non-interactive mode for better control of when\n",
"figures are shown in the notebooks.\n",
"This also better mimics the behavior you can expect in regular python scripts.\n"
]
},
{
Expand Down Expand Up @@ -138,9 +148,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
Expand All @@ -159,12 +167,10 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig = plt.figure()"
"fig = plt.figure(facecolor=(1, 0, 0, .1)) # red background to see where the figure is"
]
},
{
Expand All @@ -179,9 +185,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"plt.show()"
Expand All @@ -193,6 +197,7 @@
"source": [
"Great, a blank figure! Not terribly useful yet.\n",
"\n",
"\n",
"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",
"\n",
"A really useful utility function is [`figaspect`](http://matplotlib.org/api/figure_api.html?highlight=figaspect#matplotlib.figure.figaspect)"
Expand All @@ -201,13 +206,11 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# Twice as tall as it is wide:\n",
"fig = plt.figure(figsize=plt.figaspect(2.0))\n",
"fig = plt.figure(figsize=plt.figaspect(2.0), facecolor=(1, 0, 0, .1))\n",
"plt.show()"
]
},
Expand All @@ -227,9 +230,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig = plt.figure()\n",
Expand All @@ -254,16 +255,15 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"ax.set_xlim([0.5, 4.5])\n",
"ax.set_ylim([-2, 8])\n",
"ax.set_title('An Example Axes')\n",
"ax.set_ylabel('Y-Axis')\n",
"ax.set_xlabel('X-Axis')"
"ax.set_title('A Different Example Axes Title')\n",
"ax.set_ylabel('Y-Axis (changed)')\n",
"ax.set_xlabel('X-Axis (changed)')\n",
"plt.show()"
]
},
{
Expand Down Expand Up @@ -295,15 +295,13 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig = plt.figure()\n",
"ax = fig.add_subplot(111)\n",
"ax.plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)\n",
"ax.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], color='darkgreen', marker='^')\n",
"ax.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], c=[1, 2, 3, 5], marker='^')\n",
"ax.set_xlim(0.5, 4.5)\n",
"plt.show()"
]
Expand All @@ -320,13 +318,11 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"plt.plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)\n",
"plt.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], color='darkgreen', marker='^')\n",
"plt.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], c=[1, 2, 3, 5], marker='^')\n",
"plt.xlim(0.5, 4.5)\n",
"plt.show()"
]
Expand All @@ -335,7 +331,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"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",
"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",
"\n",
"\"Explicit is better than implicit\"\n",
"\n",
Expand All @@ -358,9 +354,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig, axes = plt.subplots(nrows=2, ncols=2)\n",
Expand All @@ -381,9 +375,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig, axes = plt.subplots(nrows=2, ncols=2)\n",
Expand Down Expand Up @@ -441,9 +433,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"%load exercises/1.1-subplots_and_basic_plotting.py"
Expand All @@ -452,9 +442,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
Expand All @@ -473,23 +461,23 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}
Loading