Skip to content
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
406 changes: 406 additions & 0 deletions AnatomyOfMatplotlib-Part2-Plotting_Methods_Overview.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions AnatomyOfMatplotlib-Part3-HowToSpeakMPL.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
"metadata": {},
"source": [
"# How to speak \"MPL\"\n",
"In the previous part, you learned how matplotlib organizes plot-making by figures and axes. We broke down the components of a basic figure and learned how to create them. You also learned how to add one or more axes to a figure, and how to tie them together. You even learned how to change some of the basic appearances of the axes. Finally, we went over some of the many kinds of figures that matplotlib can generate. With all that knowledge, you should be off making great and wonderful figures. Why are you still here?\n",
"In the previous parts, you learned how matplotlib organizes plot-making by figures and axes. We broke down the components of a basic figure and learned how to create them. You also learned how to add one or more axes to a figure, and how to tie them together. You even learned how to change some of the basic appearances of the axes. Finally, we went over some of the many plotting methods that matplotlib has to draw on those axes. With all that knowledge, you should be off making great and wonderful figures. Why are you still here?\n",
"\n",
"\"We don't know how to control our plots and figures!\" says some random voice in the back of the room.\n",
"\n",
"Of course! While the previous section may have taught you some of the structure and syntax of matplotlib, it did not describe much of the substance and vocabulary of the library. This section will go over many of the properties that are used throughout the library. Note that while many of the examples in this section may show one way of setting a particular property, that property may be applicible elsewhere in completely different context. This is the \"language\" of matplotlib.\n",
"Of course! While the previous sections may have taught you some of the structure and syntax of matplotlib, it did not describe much of the substance and vocabulary of the library. This section will go over many of the properties that are used throughout the library. Note that while many of the examples in this section may show one way of setting a particular property, that property may be applicible elsewhere in completely different context. This is the \"language\" of matplotlib.\n",
"\n",
"# Colors\n",
"This is, perhaps, the most important piece of vocabulary in matplotlib. Given that matplotlib is a plotting library, colors are associated with everything that is plotted in your figures. Matplotlib supports a [very robust language](http://matplotlib.org/api/colors_api.html#module-matplotlib.colors) for specifying colors that should be familiar to a wide variety of users.\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -71,7 +72,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -100,7 +102,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -147,7 +150,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand All @@ -172,7 +176,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "code",
Expand All @@ -188,7 +193,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand All @@ -214,7 +220,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand All @@ -224,9 +231,7 @@
"\n",
" ax.legend(loc=\"best\")\n",
" \n",
"Also, if you happen to be plotting something that you do not want to appear in the legend, just set the label to \"\\_nolegend\\_\". \n",
"\n",
"On a side note, we're going to briefly introduce the `ax.bar` plotting method here. Unsurprisingly, it draws bars (vertical, by default)."
"Also, if you happen to be plotting something that you do not want to appear in the legend, just set the label to \"\\_nolegend\\_\". "
]
},
{
Expand All @@ -241,17 +246,18 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 2.1\n",
"# Exercise 4.1\n",
"\n",
"Once again, let's use a bit of what we've learned. Try to reproduce the following figure:\n",
"\n",
"<img src=\"images/exercise_2-1.png\">\n",
"<img src=\"images/exercise_4-1.png\">\n",
"\n",
"Hint: You'll need to combine `ax.axis(...)` and `ax.margins(...)`. Here's the data and some code to get you started:"
]
Expand All @@ -274,7 +280,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -312,7 +319,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -340,7 +348,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand All @@ -362,7 +371,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -398,7 +408,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -427,7 +438,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand All @@ -452,7 +464,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -492,7 +505,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": null
},
{
"cell_type": "markdown",
Expand All @@ -502,7 +516,7 @@
"\n",
"This one is a bit trickier. Once again, try to reproduce the figure below:\n",
"\n",
"<img src=\"images/exercise_2-2.png\">\n",
"<img src=\"images/exercise_4-2.png\">\n",
"\n",
"\n",
"A few key hints: The two subplots have no vertical space between them (this means that the `hspace` is `0`). Note that the bottom spine is at 0 in data coordinates and the tick lines are missing from the left and top sides.\n",
Expand All @@ -515,23 +529,15 @@
"cell_type": "code",
"collapsed": false,
"input": [
"# %load exercises/2.2-spines_ticks_and_subplot_spacing.py\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"# Try to reproduce the figure shown in images/exercise_2.2.png\n",
"# This one is a bit trickier!\n",
"\n",
"# Here's the data...\n",
"data = [('dogs', 4, 4), ('frogs', -3, 1), ('cats', 1, 5), ('goldfish', -2, 2)]\n",
"animals, friendliness, popularity = zip(*data)\n"
"%load exercises/4.2-spines_ticks_and_subplot_spacing.py"
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": 5
}
],
"metadata": {}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 4.1\n",
"## Exercise 5.1\n",
"Give yourselves 4 gold stars!\n",
"\n",
"Hint: [StarPolygonCollection](http://matplotlib.org/api/collections_api.html#matplotlib.collections.StarPolygonCollection)"
Expand All @@ -293,7 +293,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"%load exercises/4.1-goldstar.py"
"%load exercises/5.1-goldstar.py"
],
"language": "python",
"metadata": {},
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ for conferences and other opportunities for training.
The tutorial can be viewed on [nbviewer](http://nbviewer.ipython.org):
* [Part 0: Introduction To NumPy]
* [Part 1: Overview of Matplotlib]
* [Part 2: Limits, Legends, and Layouts]
* [Part 2: Plotting Methods]
* [Part 3: How To Speak MPL]
* [Part 4: Artists]
* [Part 5: mpl_toolkits]
* [Part 4: Limits, Legends, and Layouts]
* [Part 5: Artists]
* [Part 6: mpl_toolkits]

Cheers!


[Part 0: Introduction To NumPy]: http://nbviewer.ipython.org/github/WeatherGod/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part0-Intro2NumPy.ipynb
[Part 1: Overview of Matplotlib]: http://nbviewer.ipython.org/github/WeatherGod/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part1-Figures_Subplots_and_layouts.ipynb
[Part 2: Limits, Legends, and Layouts]: http://nbviewer.ipython.org/github/WeatherGod/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part2-Limits_Legends_and_Layouts.ipynb
[Part 2: Plotting Methods]: http://nbviewer.ipython.org/github/WeatherGod/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part2-Plotting_Methods_Overview.ipynb
[Part 3: How To Speak MPL]: http://nbviewer.ipython.org/github/WeatherGod/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part3-HowToSpeakMPL.ipynb
[Part 4: Artists]: http://nbviewer.ipython.org/github/WeatherGod/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part4-Artists.ipynb
[Part 5: mpl_toolkits]: http://nbviewer.ipython.org/github/WeatherGod/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part5-mpl_toolkits.ipynb
[Part 4: Limits, Legends, and Layouts]: http://nbviewer.ipython.org/github/WeatherGod/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part4-Limits_Legends_and_Layouts.ipynb
[Part 5: Artists]: http://nbviewer.ipython.org/github/WeatherGod/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part5-Artists.ipynb
[Part 6: mpl_toolkits]: http://nbviewer.ipython.org/github/WeatherGod/AnatomyOfMatplotlib/blob/master/AnatomyOfMatplotlib-Part6-mpl_toolkits.ipynb
Binary file added examples/.example_utils.py.swo
Binary file not shown.
45 changes: 45 additions & 0 deletions examples/bar_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import numpy as np
import matplotlib.pyplot as plt

import example_utils

def main():
fig, axes = example_utils.setup_axes()

basic_bar(axes[0])
tornado(axes[1])
general(axes[2])

example_utils.title(fig, '"ax.bar(...)": Plot rectangles')
fig.savefig('bar_example.png', facecolor='none')
plt.show()

def basic_bar(ax):
y = [1, 3, 4, 5.5, 3, 2]
err = [0.2, 1, 2.5, 1, 1, 0.5]
x = np.arange(len(y))
ax.bar(x, y, yerr=err, color='lightblue', ecolor='black')
ax.margins(0.05)
ax.set_ylim(bottom=0)
example_utils.label(ax, 'bar(x, y, yerr=e)')

def tornado(ax):
y = np.arange(8)
x1 = y + np.random.random(8) + 1
x2 = y + 3 * np.random.random(8) + 1
ax.barh(y, x1, color='lightblue')
ax.barh(y, -x2, color='salmon')
ax.margins(0.15)
example_utils.label(ax, 'barh(x, y)')

def general(ax):
num = 10
left = np.random.randint(0, 10, num)
bottom = np.random.randint(0, 10, num)
width = np.random.random(num) + 0.5
height = np.random.random(num) + 0.5
ax.bar(left, height, width, bottom, color='salmon')
ax.margins(0.15)
example_utils.label(ax, 'bar(l, h, w, b)')

main()
26 changes: 26 additions & 0 deletions examples/contour_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.cbook import get_sample_data

import example_utils

z = np.load(get_sample_data('axes_grid/bivariate_normal.npy'))

fig, axes = example_utils.setup_axes()

axes[0].contour(z, cmap='gist_earth')
example_utils.label(axes[0], 'contour')

axes[1].contourf(z, cmap='gist_earth')
example_utils.label(axes[1], 'contourf')

axes[2].contourf(z, cmap='gist_earth')
cont = axes[2].contour(z, colors='black')
axes[2].clabel(cont, fontsize=6)
example_utils.label(axes[2], 'contourf + contour\n + clabel')

example_utils.title(fig, '"contour, contourf, clabel": Contour/label 2D data',
y=0.96)
fig.savefig('contour_example.png', facecolor='none')

plt.show()
23 changes: 23 additions & 0 deletions examples/example_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Just a few functions shared between all the examples. Ensures example plots are
all the same size, etc.
"""
import matplotlib.pyplot as plt

def setup_axes():
fig, axes = plt.subplots(ncols=3, figsize=(6.5,3))
for ax in fig.axes:
ax.set(xticks=[], yticks=[])
fig.subplots_adjust(wspace=0, left=0, right=0.93)
return fig, axes

def title(fig, text, y=0.9):
fig.suptitle(text, size=14, y=y, weight='semibold', x=0.98, ha='right',
bbox=dict(boxstyle='round', fc='floralwhite', ec='#8B7E66',
lw=2))

def label(ax, text, y=0):
ax.annotate(text, xy=(0.5, 0.00), xycoords='axes fraction', ha='center',
style='italic',
bbox=dict(boxstyle='round', facecolor='floralwhite',
ec='#8B7E66'))
Loading