Skip to content

Updated the categorical example #23

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

Closed
wants to merge 1 commit into from
Closed
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
115 changes: 46 additions & 69 deletions AnatomyOfMatplotlib-Part4-Limits_Legends_and_Layouts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"from __future__ import print_function\n",
Expand Down Expand Up @@ -38,9 +36,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=plt.figaspect(0.5))\n",
Expand All @@ -65,9 +61,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig, (ax1, ax2) = plt.subplots(1, 2, figsize=plt.figaspect(0.5))\n",
Expand Down Expand Up @@ -108,9 +102,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig, axes = plt.subplots(nrows=3)\n",
Expand Down Expand Up @@ -141,9 +133,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# Good -- setting limits after plotting is done\n",
Expand All @@ -158,9 +148,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# Bad -- Setting limits before plotting is done\n",
Expand All @@ -186,9 +174,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
Expand All @@ -213,9 +199,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots(1, 1)\n",
Expand All @@ -241,20 +225,33 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"%load exercises/4.1-legends_and_scaling.py"
"# %load exercises/4.1-legends_and_scaling.py\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"plt.style.use('classic')\n",
"\n",
"# Try to reproduce the figure shown in images/exercise_4-1.png\n",
"# Here's the data and colors used.\n",
"\n",
"t = np.linspace(0, 2 * np.pi, 150)\n",
"x1, y1 = np.cos(t), np.sin(t)\n",
"x2, y2 = 2 * x1, 2 * y1\n",
"\n",
"colors = ['darkred', 'darkgreen']\n",
"\n",
"# Try to plot the two circles, scale the axes as shown and add a legend\n",
"# Hint: it's easiest to combine `ax.axis(...)` and `ax.margins(...)` to scale\n",
"# the axes\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for example, this shouldn't be here

]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
Expand Down Expand Up @@ -294,9 +291,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
Expand All @@ -315,28 +310,24 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"A commonly-asked question is \"How do I plot non-numerical categories?\"\n",
"A commonly-asked question is \"How do I plot categories?\"\n",
" \n",
"Currently, the easiest way to do this is to \"fake\" the x-values and then change the tick labels to reflect the category.\n",
"Starting in version 2.0, just like any other data. \n",
"\n",
"For example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"data = [('apples', 2), ('oranges', 3), ('peaches', 1)]\n",
"fruit, value = zip(*data)\n",
"\n",
"fig, ax = plt.subplots()\n",
"x = np.arange(len(fruit))\n",
"ax.bar(x, value, align='center', color='gray')\n",
"ax.set(xticks=x, xticklabels=fruit)\n",
"ax.bar(fruit, value, align='center', color='gray')\n",
"plt.show()"
]
},
Expand All @@ -351,9 +342,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig, axes = plt.subplots(2, 2, figsize=(9, 9))\n",
Expand All @@ -377,9 +366,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"def example_plot(ax):\n",
Expand Down Expand Up @@ -419,9 +406,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)\n",
Expand All @@ -441,9 +426,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig, ax1 = plt.subplots(1, 1)\n",
Expand All @@ -466,9 +449,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
Expand Down Expand Up @@ -516,9 +497,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"%load exercises/4.2-spines_ticks_and_subplot_spacing.py"
Expand All @@ -527,9 +506,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
Expand All @@ -546,23 +523,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
}