Skip to content

Commit 31d4479

Browse files
committed
Added many documentation links
1 parent 2064171 commit 31d4479

4 files changed

+109
-50
lines changed

AnatomyOfMatplotlib-Part1-pyplot.ipynb

+83-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:e18b03e818c4707e39f00f4a9a29e8a07827170e6730dd6bdca85fe40273be71"
4+
"signature": "sha256:bb79f3fb1b583fa80ed2817cbc765825ca44470bce6a68afd866d1e59a9ab3c7"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -16,12 +16,20 @@
1616
"%matplotlib inline"
1717
],
1818
"language": "python",
19-
"metadata": {},
19+
"metadata": {
20+
"slideshow": {
21+
"slide_type": "-"
22+
}
23+
},
2024
"outputs": []
2125
},
2226
{
2327
"cell_type": "markdown",
24-
"metadata": {},
28+
"metadata": {
29+
"slideshow": {
30+
"slide_type": "-"
31+
}
32+
},
2533
"source": [
2634
"# Matplotlib\n",
2735
"## Introduction\n",
@@ -33,12 +41,12 @@
3341
"## Gallery\n",
3442
"Many users of matplotlib are often faced with the question, \"I want to make a plot 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 plots. Browse through the gallery, click on any figure that has pieces of what you want to see the code that generated it. Soon enough, you will be like a chef, mixing and matching components to produce your masterpiece!\n",
3543
"\n",
36-
"As always, if you have a new and interesting plot that demonstrates a feature of matplotlib, feel free to submit a well-commented version of the example code for inclusion (more on that later).\n",
44+
"As always, if you have a new and interesting plot that demonstrates a feature of matplotlib, feel free to submit a well-commented version of the example code for inclusion.\n",
3745
"\n",
3846
"## Mailing Lists and StackOverflow\n",
3947
"When you are just simply stuck, and can not 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](http://sourceforge.net/mail/?group_id=80706). 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",
4048
"\n",
41-
"A newer 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",
49+
"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",
4250
"\n",
4351
"# Github repository\n",
4452
"## Location\n",
@@ -224,7 +232,7 @@
224232
"source": [
225233
"# Figure\n",
226234
"## Introduction\n",
227-
"All plotting is done through the *figure* object. You can create as many figures as you need. Figures can't do much by themselves, but no plotting can happen without them. They are, literally, the \"canvas\" of your plot."
235+
"All plotting is done through the [`Figure`](http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure) object. You can create as many figures as you need. Figures can't do much by themselves, but no plotting can happen without them. They are, literally, the \"canvas\" of your plot."
228236
]
229237
},
230238
{
@@ -261,6 +269,13 @@
261269
"metadata": {},
262270
"outputs": []
263271
},
272+
{
273+
"cell_type": "markdown",
274+
"metadata": {},
275+
"source": [
276+
"A really useful utility function is [`figaspect`](http://matplotlib.org/api/figure_api.html?highlight=figaspect#matplotlib.figure.figaspect)"
277+
]
278+
},
264279
{
265280
"cell_type": "code",
266281
"collapsed": false,
@@ -287,7 +302,7 @@
287302
"metadata": {},
288303
"source": [
289304
"# Axes\n",
290-
"All plotting is done with respect to an *Axes*. An *Axes* is made up of *Axis* objects and many other things. An *Axes* object must belong to a *Figure* (and only one *Figure*). Most commands you will ever issue will be with respect to this *Axes* object."
305+
"All plotting is done with respect to an [`Axes`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes). An *Axes* is made up of [`Axis`](http://matplotlib.org/api/axis_api.html#matplotlib.axis.Axis) objects and many other things. An *Axes* object must belong to a *Figure* (and only one *Figure*). Most commands you will ever issue will be with respect to this *Axes* object."
291306
]
292307
},
293308
{
@@ -399,7 +414,9 @@
399414
"metadata": {},
400415
"source": [
401416
"### Exercise\n",
402-
"How would you make a plot with a y-axis such that it starts at 1000 at the bottom, and goes to 500 at the top?"
417+
"How would you make a plot with a y-axis such that it starts at 1000 at the bottom, and goes to 500 at the top?\n",
418+
"\n",
419+
"Hint: [`set_ylim`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_ylim)"
403420
]
404421
},
405422
{
@@ -468,10 +485,12 @@
468485
"## Ticks, Tick Lines, Tick Labels and Tickers\n",
469486
"This is a constant source of confusion:\n",
470487
"\n",
471-
"* A Tick defines the *location* of a Tick Label.\n",
488+
"* A Tick is the *location* of a Tick Label.\n",
472489
"* A Tick Line is the line that denotes the location of the tick.\n",
473-
"* A Tick Label is the text that is displayed at that Tick.\n",
474-
"* A Ticker automatically determines the Ticks for an Axis."
490+
"* A Tick Label is the text that is displayed at that tick.\n",
491+
"* A [`Ticker`](http://matplotlib.org/api/ticker_api.html#module-matplotlib.ticker) automatically determines the ticks for an Axis and formats the tick labels.\n",
492+
"\n",
493+
"[`tick_params()`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.tick_params) is often used to help configure your tickers."
475494
]
476495
},
477496
{
@@ -483,6 +502,7 @@
483502
"ax.plot([1, 2, 3, 4], [10, 20, 25, 30])\n",
484503
"ax.xaxis.set_ticks(range(1, 5)) # Set ticks at 1, 2, 3, 4\n",
485504
"ax.xaxis.set_ticklabels([3, 100, -12, \"foo\"]) # Label ticks as \"3\", \"100\", \"-12\", and \"foo\"\n",
505+
"ax.tick_params(axis='y', direction='inout', length=10)\n",
486506
"plt.show()"
487507
],
488508
"language": "python",
@@ -528,14 +548,14 @@
528548
"source": [
529549
"There are many ways to add and modify subplots in a figure.\n",
530550
"\n",
531-
"* `plt.subplots()`\n",
532-
"* `plt.subplot()` and `fig.add_subplot()` Which should be familiar to all Matlab users\n",
533-
"* `plt.axes()`\n",
534-
"* `plt.subplot2grid()`\n",
535-
"* `plt.subplot_tool()` Interactive modification of subplot spacing.\n",
551+
"* [`plt.subplots()`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplots)\n",
552+
"* [`plt.subplot()`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplot) and [`fig.add_subplot()`](http://matplotlib.org/api/figure_api.html?#matplotlib.figure.Figure.add_subplot) Which should be familiar to all Matlab users\n",
553+
"* [`plt.axes()`](http://matplotlib.org/api/pyplot_api.html?#matplotlib.pyplot.axes)\n",
554+
"* [`plt.subplot2grid()`](http://matplotlib.org/api/pyplot_api.html?#matplotlib.pyplot.subplot2grid)\n",
555+
"* `plt.subplot_tool()` Interactive modification of subplot spacing.\n",
536556
"\n",
537557
"## Spacing\n",
538-
"The spacing between the subplots can be adjusted using `plt.subplots_adjust()`. Play around with the example below to see how the different arguments affect the spacing."
558+
"The spacing between the subplots can be adjusted using [`plt.subplots_adjust()`](http://matplotlib.org/api/pyplot_api.html?#matplotlib.pyplot.subplots_adjust). Play around with the example below to see how the different arguments affect the spacing."
539559
]
540560
},
541561
{
@@ -558,7 +578,7 @@
558578
"source": [
559579
"A common complaint with matplotlib users is that the labels do not fit with the subplots, or the label of one subplot spills onto another subplot's area. Matplotlib does not currently have any sort of robust layout engine, as it is a design decision to minimize the amount of \"magic\" that matplotlib performs. We intend to let users have complete, 100% control over their plots. LaTeX users would be quite familiar with the amount of frustration that can occur with placement of figures in their documents.\n",
560580
"\n",
561-
"That said, there have been some efforts to develop tools that users can use to help address the most common compaints. The \"Tight Layout\" feature, when invoked, will attempt to resize margins, and subplots so that nothing overlaps."
581+
"That said, there have been some efforts to develop tools that users can use to help address the most common compaints. The \"[Tight Layout](http://matplotlib.org/users/tight_layout_guide.html)\" feature, when invoked, will attempt to resize margins, and subplots so that nothing overlaps."
562582
]
563583
},
564584
{
@@ -612,7 +632,7 @@
612632
"metadata": {},
613633
"source": [
614634
"## GridSpec\n",
615-
"Under the hood, matplotlib utilizes GridSpec to layout the subplots. While `plt.subplots()` is fine for simple cases, sometimes you will need more advanced subplot layouts. In such cases, you should use GridSpec directly. GridSpec is outside the scope of this tutorial, but it is handy to know that it exists. [Here](http://matplotlib.org/users/gridspec.html) is a guide on how to use it."
635+
"Under the hood, matplotlib utilizes [`GridSpec`](http://matplotlib.org/api/gridspec_api.html) to layout the subplots. While `plt.subplots()` is fine for simple cases, sometimes you will need more advanced subplot layouts. In such cases, you should use GridSpec directly. GridSpec is outside the scope of this tutorial, but it is handy to know that it exists. [Here](http://matplotlib.org/users/gridspec.html) is a guide on how to use it."
616636
]
617637
},
618638
{
@@ -675,21 +695,36 @@
675695
"cell_type": "markdown",
676696
"metadata": {},
677697
"source": [
678-
"# Axis Spines"
698+
"# Axis Spines\n",
699+
"Spines are the axis lines for a plot. Each plot can have four spines: \"top\", \"bottom\", \"left\" and \"right\". By default, they are set so that they frame the plot, but they can be individually positioned and configured via the [`set_position()`](http://matplotlib.org/api/spines_api.html#matplotlib.spines.Spine.set_position) method of the spine. Here are some different configurations."
679700
]
680701
},
681702
{
682703
"cell_type": "code",
683704
"collapsed": false,
684705
"input": [
685706
"fig, ax = plt.subplots(1, 1)\n",
686-
"ax.plot([1, 2, 3, 4], [10, 20, 25, 30])\n",
707+
"ax.plot([-2, 2, 3, 4], [-10, 20, 25, 5])\n",
687708
"ax.spines['top'].set_visible(False)\n",
688709
"ax.xaxis.set_ticks_position('bottom') # no ticklines at the top\n",
689710
"ax.spines['right'].set_visible(False)\n",
690711
"ax.yaxis.set_ticks_position('left') # no ticklines on the right\n",
712+
"\n",
713+
"# \"outward\"\n",
714+
"# Move the two remaining spines \"out\" away from the plot by 10 points\n",
691715
"ax.spines['bottom'].set_position(('outward', 10))\n",
692716
"ax.spines['left'].set_position(('outward', 10))\n",
717+
"\n",
718+
"# \"data\"\n",
719+
"# Have the spines stay intersected at (0,0)\n",
720+
"#ax.spines['bottom'].set_position(('data', 0))\n",
721+
"#ax.spines['left'].set_position(('data', 0))\n",
722+
"\n",
723+
"# \"axes\"\n",
724+
"# Have the two remaining spines placed at a fraction of the axes\n",
725+
"#ax.spines['bottom'].set_position(('axes', 0.75))\n",
726+
"#ax.spines['left'].set_position(('axes', 0.25))\n",
727+
"\n",
693728
"plt.show()"
694729
],
695730
"language": "python",
@@ -701,9 +736,33 @@
701736
"metadata": {},
702737
"source": [
703738
"## Exercise\n",
704-
"Modify the above example to have spines and tick labels on both sides, with tickmarks *through* the spines."
739+
"Modify the above example to have outward spines and tick labels on both sides, with tickmarks *through* the spines.\n",
740+
"\n",
741+
"Hints:\n",
742+
"[`tick_params()`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.tick_params)\n",
743+
"[`set_position()`](http://matplotlib.org/api/spines_api.html#matplotlib.spines.Spine.set_position)"
705744
]
706745
},
746+
{
747+
"cell_type": "code",
748+
"collapsed": false,
749+
"input": [
750+
"fig, ax = plt.subplots(1, 1)\n",
751+
"ax.plot([1, 2, 3, 4], [10, 20, 25, 30])\n",
752+
"ax.spines['top'].set_visible(False)\n",
753+
"ax.xaxis.set_ticks_position('bottom') # no ticklines at the top\n",
754+
"ax.tick_params(axis='y', direction='inout', length=10,\n",
755+
" labelleft=True, labelright=True,\n",
756+
" right=True, left=True)\n",
757+
"ax.spines['bottom'].set_position(('outward', 10))\n",
758+
"ax.spines['right'].set_position(('outward', 10))\n",
759+
"ax.spines['left'].set_position(('outward', 10))\n",
760+
"plt.show()"
761+
],
762+
"language": "python",
763+
"metadata": {},
764+
"outputs": []
765+
},
707766
{
708767
"cell_type": "markdown",
709768
"metadata": {},
@@ -779,7 +838,7 @@
779838
"cell_type": "markdown",
780839
"metadata": {},
781840
"source": [
782-
"Looks terrible, right? What is happening is that a colorbar in matplotlib is just a very squashed subplot with an image of the colormap and axis ticks and labels. When told to create a colorbar for an image, matplotlib will simply \"steal\" space from that image's subplot and create a new subplot. There are a couple ways to deal with this issue. First, if you preallocate space for the colorbar (by creating your own Axes object to add to the Figure), you can pass that preallocated Axes to the \"cax\" argument of `plt.colorbar()`, and it won't have to steal any space. The easier option is to pass a list of all the axes objects to `plt.colorbar(..., ax)`, and it will steal space equally from them."
841+
"Looks terrible, right? What is happening is that a colorbar in matplotlib is just a very squashed subplot with an image of the colormap and axis ticks and labels. When told to create a colorbar for an image, matplotlib will simply \"steal\" space from that image's subplot and create a new subplot. There are a couple ways to deal with this issue. First, if you preallocate space for the colorbar (by creating your own Axes object to add to the Figure), you can pass that preallocated Axes to the \"cax\" argument of [`plt.colorbar()`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.colorbar), and it won't have to steal any space. The easier option is to pass a list of all the axes objects to `plt.colorbar(..., ax)`, and it will steal space equally from them."
783842
]
784843
},
785844
{
@@ -800,7 +859,7 @@
800859
"cell_type": "markdown",
801860
"metadata": {},
802861
"source": [
803-
"There is also a third, very powerful option, called `axes_grid1`, which we will discuss in the `mpl_toolkits` section."
862+
"There is also a third, very powerful, option called [`axes_grid1`](http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html), which we will discuss in the `mpl_toolkits` section."
804863
]
805864
}
806865
],

0 commit comments

Comments
 (0)