Skip to content

Instantly share code, notes, and snippets.

@jakevdp
Last active April 7, 2024 18:40
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jakevdp/54972317e9cd28e45df37ef6aff13555 to your computer and use it in GitHub Desktop.
Save jakevdp/54972317e9cd28e45df37ef6aff13555 to your computer and use it in GitHub Desktop.
Creating the Python Logo in Matplotlib
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Recreating the Python Logo in Matplotlib\n",
"\n",
"*Jake VanderPlas*"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"%config InlineBackend.figure_format = 'svg'\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib.path import Path\n",
"from matplotlib.patches import PathPatch"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# define shape of one half of the logo\n",
"vertices = np.array([\n",
" [ 3, 480],\n",
" [ 3, 550],\n",
" [ -260, 550],\n",
" [ -480, 550],\n",
" [ -480, 790],\n",
" [ -480, 1080],\n",
" [ 440, 1080],\n",
" [ 486, 790],\n",
" [ 486, 270],\n",
" [ 470, 40],\n",
" [ 230, 30],\n",
" [ -275, 30],\n",
" [ -540, 20],\n",
" [ -550, -240],\n",
" [ -550, -495],\n",
" [ -730, -495],\n",
" [-1080, -480],\n",
" [-1080, 460],\n",
" [ -730, 480],\n",
" [ 3, 480]\n",
"])\n",
"\n",
"codes = ([Path.MOVETO]\n",
" + 4 * [Path.LINETO]\n",
" + 3 * [Path.CURVE4]\n",
" + [Path.LINETO]\n",
" + 2 * [Path.CURVE3]\n",
" + [Path.LINETO]\n",
" + 2 * [Path.CURVE3]\n",
" + 2 * [Path.LINETO]\n",
" + 3 * [Path.CURVE4]\n",
" + [Path.CLOSEPOLY])\n",
"\n",
"# Add circular \"eye\"\n",
"circ = Path.unit_circle()\n",
"vertices = np.vstack([vertices, 88 * circ.vertices + [-266, 760]])\n",
"codes = np.concatenate([codes, circ.codes])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Created with matplotlib (http://matplotlib.org/) -->\n",
"<svg height=\"322pt\" version=\"1.1\" viewBox=\"0 0 346 322\" width=\"346pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
" <defs>\n",
" <style type=\"text/css\">\n",
"*{stroke-linecap:butt;stroke-linejoin:round;}\n",
" </style>\n",
" </defs>\n",
" <g id=\"figure_1\">\n",
" <g id=\"patch_1\">\n",
" <path d=\"M 0 322.578125 \n",
"L 346.729687 322.578125 \n",
"L 346.729687 0 \n",
"L 0 0 \n",
"z\n",
"\" style=\"fill:none;\"/>\n",
" </g>\n",
" <g id=\"axes_1\">\n",
" <g id=\"patch_2\">\n",
" <path clip-path=\"url(#pf5a90f8fe7)\" d=\"M 192.393324 96.518182 \n",
"L 192.393324 88.033333 \n",
"L 160.514536 88.033333 \n",
"L 133.847869 88.033333 \n",
"L 133.847869 58.942424 \n",
"C 133.847869 23.790909 245.363021 23.790909 250.938778 58.942424 \n",
"L 250.938778 121.972727 \n",
"Q 248.999384 149.851515 219.908475 151.063636 \n",
"L 158.696354 151.063636 \n",
"Q 126.575142 152.275758 125.363021 183.790909 \n",
"L 125.363021 214.7 \n",
"L 103.544839 214.7 \n",
"C 61.120597 212.881818 61.120597 98.942424 103.544839 96.518182 \n",
"z\n",
"M 159.787263 73.245455 \n",
"C 162.616096 73.245455 165.329449 72.121547 167.329736 70.12126 \n",
"C 169.330023 68.120973 170.45393 65.407621 170.45393 62.578788 \n",
"C 170.45393 59.749955 169.330023 57.036603 167.329736 55.036316 \n",
"C 165.329449 53.036029 162.616096 51.912121 159.787263 51.912121 \n",
"C 156.95843 51.912121 154.245078 53.036029 152.244791 55.036316 \n",
"C 150.244504 57.036603 149.120597 59.749955 149.120597 62.578788 \n",
"C 149.120597 65.407621 150.244504 68.120973 152.244791 70.12126 \n",
"C 154.245078 72.121547 156.95843 73.245455 159.787263 73.245455 \n",
"z\n",
"\" style=\"fill:#3773a4;\"/>\n",
" </g>\n",
" <g id=\"patch_3\">\n",
" <path clip-path=\"url(#pf5a90f8fe7)\" d=\"M 191.666051 212.881818 \n",
"L 191.666051 221.366667 \n",
"L 223.544839 221.366667 \n",
"L 250.211506 221.366667 \n",
"L 250.211506 250.457576 \n",
"C 250.211506 285.609091 138.696354 285.609091 133.120597 250.457576 \n",
"L 133.120597 187.427273 \n",
"Q 135.059991 159.548485 164.1509 158.336364 \n",
"L 225.363021 158.336364 \n",
"Q 257.484233 157.124242 258.696354 125.609091 \n",
"L 258.696354 94.7 \n",
"L 280.514536 94.7 \n",
"C 322.938778 96.518182 322.938778 210.457576 280.514536 212.881818 \n",
"z\n",
"M 224.272112 236.154545 \n",
"C 221.443279 236.154545 218.729926 237.278453 216.729639 239.27874 \n",
"C 214.729352 241.279027 213.605445 243.992379 213.605445 246.821212 \n",
"C 213.605445 249.650045 214.729352 252.363397 216.729639 254.363684 \n",
"C 218.729926 256.363971 221.443279 257.487879 224.272112 257.487879 \n",
"C 227.100945 257.487879 229.814297 256.363971 231.814584 254.363684 \n",
"C 233.814871 252.363397 234.938778 249.650045 234.938778 246.821212 \n",
"C 234.938778 243.992379 233.814871 241.279027 231.814584 239.27874 \n",
"C 229.814297 237.278453 227.100945 236.154545 224.272112 236.154545 \n",
"z\n",
"\" style=\"fill:#ffd343;\"/>\n",
" </g>\n",
" </g>\n",
" </g>\n",
" <defs>\n",
" <clipPath id=\"pf5a90f8fe7\">\n",
" <rect height=\"288\" width=\"288\" x=\"48.029687\" y=\"10.7\"/>\n",
" </clipPath>\n",
" </defs>\n",
"</svg>\n"
],
"text/plain": [
"<matplotlib.figure.Figure at 0x1044e6cc0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig, ax = plt.subplots(figsize=(4, 4))\n",
"fig.subplots_adjust(left=0, right=1, bottom=0, top=1)\n",
"\n",
"ax.add_patch(PathPatch(Path(vertices, codes), facecolor='#3773A4', edgecolor='none'))\n",
"ax.add_patch(PathPatch(Path(-vertices, codes), facecolor='#FFD343', edgecolor='none'))\n",
"\n",
"ax.axis('off')\n",
"ax.axis('equal')\n",
"ax.axis('tight');"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3.6",
"language": "python",
"name": "python3.6"
},
"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.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment