Skip to content

Commit

Permalink
More polishing, and exercises are separated
Browse files Browse the repository at this point in the history
  • Loading branch information
WeatherGod committed Jun 25, 2014
1 parent 31d4479 commit 86b0b73
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 184 deletions.
51 changes: 13 additions & 38 deletions AnatomyOfMatplotlib-Part1-pyplot.ipynb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:bb79f3fb1b583fa80ed2817cbc765825ca44470bce6a68afd866d1e59a9ab3c7"
"signature": "sha256:c7159e6f4ecac56d72f7333ed9bb24bbfeb4c66fba3000a1b2cb3c382fd148fd"
},
"nbformat": 3,
"nbformat_minor": 0,
Expand Down Expand Up @@ -413,7 +413,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise\n",
"### Exercise 1.1\n",
"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",
"\n",
"Hint: [`set_ylim`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_ylim)"
Expand All @@ -423,9 +423,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"fig, ax = plt.subplots(1, 1)\n",
"ax.set_ylim( )\n",
"plt.show()"
"%load exercises/1.1-limits.py"
],
"language": "python",
"metadata": {},
Expand Down Expand Up @@ -585,7 +583,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
"# Without \"tight_layout\"\n",
"def example_plot(ax):\n",
" ax.plot([1, 2])\n",
" ax.set_xlabel('x-label', fontsize=16)\n",
Expand All @@ -597,23 +594,7 @@
"example_plot(ax2)\n",
"example_plot(ax3)\n",
"example_plot(ax4)\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# With \"tight_layout\"\n",
"fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)\n",
"example_plot(ax1)\n",
"example_plot(ax2)\n",
"example_plot(ax3)\n",
"example_plot(ax4)\n",
"plt.tight_layout()\n",
"#plt.tight_layout()\n",
"plt.show()"
],
"language": "python",
Expand All @@ -632,7 +613,7 @@
"metadata": {},
"source": [
"## GridSpec\n",
"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."
"Under the hood, matplotlib utilizes [`GridSpec`](http://matplotlib.org/api/gridspec_api.html) to lay out 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."
]
},
{
Expand All @@ -649,6 +630,7 @@
"input": [
"# going out of inline mode to demonstrate the zooming and panning\n",
"%matplotlib\n",
"\n",
"fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)\n",
"ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])\n",
"ax2.plot([3, 4, 5, 6], [6, 5, 4, 3])\n",
Expand Down Expand Up @@ -685,6 +667,9 @@
"ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])\n",
"ax2 = ax1.twinx()\n",
"ax2.scatter([1, 2, 3, 4], [60, 50, 40, 30])\n",
"ax1.set_xlabel('X')\n",
"ax1.set_ylabel('First scale')\n",
"ax2.set_ylabel('Other scale')\n",
"plt.show()"
],
"language": "python",
Expand Down Expand Up @@ -735,8 +720,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise\n",
"Modify the above example to have outward spines and tick labels on both sides, with tickmarks *through* the spines.\n",
"## Exercise 1.2\n",
"Create a plot that have outward spines on the left and bottom with tick labels, and \"centered\" spines with no tick labels and tickmarks *through* the spines.\n",
"\n",
"Hints:\n",
"[`tick_params()`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.tick_params)\n",
Expand All @@ -747,17 +732,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"fig, ax = plt.subplots(1, 1)\n",
"ax.plot([1, 2, 3, 4], [10, 20, 25, 30])\n",
"ax.spines['top'].set_visible(False)\n",
"ax.xaxis.set_ticks_position('bottom') # no ticklines at the top\n",
"ax.tick_params(axis='y', direction='inout', length=10,\n",
" labelleft=True, labelright=True,\n",
" right=True, left=True)\n",
"ax.spines['bottom'].set_position(('outward', 10))\n",
"ax.spines['right'].set_position(('outward', 10))\n",
"ax.spines['left'].set_position(('outward', 10))\n",
"plt.show()"
"%load exercises/1.2-spines.py"
],
"language": "python",
"metadata": {},
Expand All @@ -768,7 +743,7 @@
"metadata": {},
"source": [
"# Colorbars\n",
"While not required for plotting, colorbars are much like legends because they help to describe the data being displayed. While legends describe plots, i.e., plot(), scatter(), hist(), stem(), colorbars describe images. To be really specific and technical, they can be used for any \"ScalarMappable\", which will be discussed in the Artists section. Let us take a look at a very simple example of a colorbar for a simple 2D image."
"Colorbars are much like legends because they help to describe the data being displayed. While legends describe plots, i.e., plot(), scatter(), hist(), stem(), colorbars describe images. To be really specific and technical, they can be used for any \"ScalarMappable\", which will be discussed in the `Artists` section. Let us take a look at a very simple example of a colorbar for a simple 2D image."
]
},
{
Expand Down
Loading

0 comments on commit 86b0b73

Please sign in to comment.