Skip to content

Instantly share code, notes, and snippets.

@msund
Last active August 29, 2015 13:57
Show Gist options
  • Save msund/9904323 to your computer and use it in GitHub Desktop.
Save msund/9904323 to your computer and use it in GitHub Desktop.
IPython NB for mpl wrapper--J.R. Johansson examples
{
"metadata": {
"name": "matplotlib - 2D and 3D plotting in Python"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "%pylab inline\nfrom pylab import *\nimport matplotlib.pyplot as plt # so we don't have to look at mpl's backend\nimport matplotlib.gridspec as gridspec # for subplots\nimport matplotlib.cm as cm # for fun-with-colors\nimport numpy as np",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "\nWelcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].\nFor more information, type 'help(pylab)'.\n"
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": "from matplotlylib import fig_to_plotly\nusername = 'IPython.Demo'\napi_key = '1fw3zw2o13'",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": "x = linspace(0, 5, 10)\ny = x ** 2",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig1 = plt.figure()\nsubplot(1,2,1)\nplot(x, y, 'r--')\nsubplot(1,2,2)\nplot(y, x, 'g*-');\nfig_to_plotly(fig1, username, api_key, notebook= True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2429/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 8,
"text": "<IPython.core.display.HTML at 0x10bb87790>"
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig2 = plt.figure()\n\naxes1 = fig2.add_axes([0.1, 0.1, 0.8, 0.8]) # main axes\naxes2 = fig2.add_axes([0.2, 0.5, 0.4, 0.3]) # inset axes\n\n# main figure\naxes1.plot(x, y, 'r')\naxes1.set_xlabel('x')\naxes1.set_ylabel('y')\naxes1.set_title('title')\n\n# insert\naxes2.plot(y, x, 'g')\naxes2.set_xlabel('y')\naxes2.set_ylabel('x')\naxes2.set_title('insert title');\nfig_to_plotly(fig2, username, api_key, notebook= True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2430/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 10,
"text": "<IPython.core.display.HTML at 0x10c02a6d0>"
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig3 = plt.figure()\nfig3, axes = plt.subplots(nrows=1, ncols=2)\n\nfor ax in axes:\n ax.plot(x, y, 'r')\n ax.set_xlabel('x')\n ax.set_ylabel('y')\n ax.set_title('title')\n \nfig3.tight_layout()\nfig_to_plotly(fig3, username, api_key, notebook= True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2431/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 11,
"text": "<IPython.core.display.HTML at 0x10c2e3b50>"
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig4 = plt.figure()\nfig4, ax = plt.subplots()\n\nax.plot(x, x**2, label=r\"$y = \\alpha^2$\")\nax.plot(x, x**3, label=r\"$y = \\alpha^3$\")\nax.legend(loc=2) # upper left corner\nax.set_xlabel(r'$\\alpha$', fontsize=18)\nax.set_ylabel(r'$y$', fontsize=18)\nax.set_title('title');\nfig_to_plotly(fig4, username, api_key, notebook= True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2432/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 12,
"text": "<IPython.core.display.HTML at 0x10c3f9d90>"
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": "matplotlib.rcParams.update({'font.size': 18, 'font.family': 'serif'})",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig5 = plt.figure()\n\nfig5, ax = plt.subplots()\n\nax.plot(x, x**2, label=r\"$y = \\alpha^2$\")\nax.plot(x, x**3, label=r\"$y = \\alpha^3$\")\nax.legend(loc=2) # upper left corner\nax.set_xlabel(r'$\\alpha$')\nax.set_ylabel(r'$y$')\nax.set_title('title');\nfig_to_plotly(fig5, username, api_key, notebook= True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2434/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 15,
"text": "<IPython.core.display.HTML at 0x10c52ed10>"
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": "# MATLAB style line color and style \nax.plot(x, x**2, 'b.-') # blue line with dots\nax.plot(x, x**3, 'g--') # green dashed line",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 21,
"text": "[<matplotlib.lines.Line2D at 0x10c217dd0>]"
}
],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig6 = plt.figure()\nfig6, ax = plt.subplots()\n\nax.plot(x, x+1, color=\"red\", alpha=0.5) # half-transparant red\nax.plot(x, x+2, color=\"#1155dd\") # RGB hex code for a bluish color\nax.plot(x, x+3, color=\"#15cc55\") # RGB hex code for a greenish color\nfig_to_plotly(fig6, username, api_key, notebook= True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2437/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 23,
"text": "<IPython.core.display.HTML at 0x10c507fd0>"
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig7 = plt.figure()\nfig7, axes = plt.subplots(1, 2, figsize=(10,3))\n\n# default grid appearance\naxes[0].plot(x, x**2, x, x**3, lw=2)\naxes[0].grid(True)\n\n# custom grid appearance\naxes[1].plot(x, x**2, x, x**3, lw=2)\naxes[1].grid(color='b', alpha=0.5, linestyle='dashed', linewidth=0.5)\nfig_to_plotly(fig7, username, api_key, notebook= True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2439/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 26,
"text": "<IPython.core.display.HTML at 0x10cfb6990>"
}
],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": "n = array([0,1,2,3,4,5])",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 29
},
{
"cell_type": "code",
"collapsed": false,
"input": "fig8 = plt.figure()\nfig8, ax = plt.subplots()\n\nax.plot(xx, xx**2, xx, xx**3)\n\nax.text(0.15, 0.2, r\"$y=x^2$\", fontsize=20, color=\"blue\")\nax.text(0.65, 0.1, r\"$y=x^3$\", fontsize=20, color=\"green\");\nfig_to_plotly(fig8, username, api_key, notebook= True)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/2446/600/450\" width=\"650\"></iframe>",
"output_type": "pyout",
"prompt_number": 35,
"text": "<IPython.core.display.HTML at 0x10df53fd0>"
}
],
"prompt_number": 35
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment