Skip to content

Instantly share code, notes, and snippets.

@tiwo
Created April 27, 2017 23:26
Show Gist options
  • Save tiwo/cd2d310d531356449f357879de5ad9f5 to your computer and use it in GitHub Desktop.
Save tiwo/cd2d310d531356449f357879de5ad9f5 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python builtin \"plotting\" 🐍📈🔨😋\n",
"```\n",
" x f(x)\n",
" -2.00 0.20 \n",
" -1.64 0.27 ######\n",
" -1.27 0.38 ##############\n",
" -0.91 0.55 ###########################\n",
" -0.55 0.77 #############################################\n",
" -0.18 0.97 ############################################################\n",
" 0.18 0.97 ############################################################\n",
" 0.55 0.77 #############################################\n",
" 0.91 0.55 ###########################\n",
" 1.27 0.38 ##############\n",
" 1.64 0.27 ######\n",
" 2.00 0.20 \n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def plot(f, left, right, nsteps=20, width=30):\n",
" T = range(nsteps)\n",
" X = [left + t * (right-left) / (nsteps-1) for t in T]\n",
" Y = [f(x) for x in X]\n",
" miny, maxy = min(Y), max(Y)\n",
" print('%6s %6s' % ('x', 'f(x)'))\n",
" for x, y in zip(X, Y):\n",
" y_ = int(.5 + width * (y - miny) / (maxy - miny))\n",
" bar = '#' * y_\n",
" print('%6.2f %6.2f %s' % ( x, y, bar))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def f(x):\n",
" return 1 / (1 + x*x)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" x f(x)\n",
" -2.00 0.20 \n",
" -1.79 0.24 #\n",
" -1.58 0.29 ###\n",
" -1.37 0.35 ######\n",
" -1.16 0.43 #########\n",
" -0.95 0.53 ############\n",
" -0.74 0.65 #################\n",
" -0.53 0.78 ######################\n",
" -0.32 0.91 ###########################\n",
" -0.11 0.99 ##############################\n",
" 0.11 0.99 ##############################\n",
" 0.32 0.91 ###########################\n",
" 0.53 0.78 ######################\n",
" 0.74 0.65 #################\n",
" 0.95 0.53 ############\n",
" 1.16 0.43 #########\n",
" 1.37 0.35 ######\n",
" 1.58 0.29 ###\n",
" 1.79 0.24 #\n",
" 2.00 0.20 \n"
]
}
],
"source": [
"plot(f, -2, 2)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment