Skip to content

Instantly share code, notes, and snippets.

@dpsanders
Created July 21, 2013 00:37
Show Gist options
  • Save dpsanders/6047005 to your computer and use it in GitHub Desktop.
Save dpsanders/6047005 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Interrogating matplotlib parameter settings in matplotlib.mpl_params\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The styling of `matplotlib` plots is highly configurable via parameter settings stored in the data structure `matplotlib.mpl_params` inside `matplotlib` (formerly called `matplotlib.rcParams`).\n",
"\n",
"`mpl_params` behaves similarly to a dictionary, i.e. it is a collection of key-value pairs, but it also has *validation*,\n",
"meaning that the possible values associated to a given key may be restricted.\n",
"\n",
"For a key `key`, querying `mpl_params[key]` returns the current value associated to the key `key`, while\n",
"\n",
" mpl_params[key] = value\n",
" \n",
"associates the new value `value` to the key `key`.\n",
"\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%matplotlib\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"using matplotlib backend: MacOSX\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib as mpl\n",
"import numpy as np\n",
"\n",
"from matplotlib import pyplot as plt\n",
"from matplotlib import rcParams\n",
"\n",
"mpl_params = rcParams"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since there are many configurable options in `mpl.parameters`,\n",
"it is useful to restrict attention to a subset of interest.\n",
"For example, we can list all of the keys containing the string `font` as follows:\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"font_keys = [k for k in mpl_params.keys() if 'font' in k]\n",
"print font_keys"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['font.cursive', 'font.family', 'font.fantasy', 'font.monospace', 'font.sans-serif', 'font.serif', 'font.size', 'font.stretch', 'font.style', 'font.variant', 'font.weight', 'legend.fontsize', 'mathtext.fontset', 'pdf.fonttype', 'pdf.use14corefonts', 'pgf.rcfonts', 'ps.fonttype', 'svg.fonttype']\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We could define a function for this:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def get_keys_containing(word):\n",
" ''' \n",
" Returns the `matplotlib.rcParams` keys which contain the string `word`.\n",
" '''\n",
" \n",
" keys = [k for k in mpl_params.keys() if word in k]\n",
" return keys"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and use it like so:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"font_keys = get_keys_containing('font')\n",
"print font_keys"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['font.cursive', 'font.family', 'font.fantasy', 'font.monospace', 'font.sans-serif', 'font.serif', 'font.size', 'font.stretch', 'font.style', 'font.variant', 'font.weight', 'legend.fontsize', 'mathtext.fontset', 'pdf.fonttype', 'pdf.use14corefonts', 'pgf.rcfonts', 'ps.fonttype', 'svg.fonttype']\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we define a function to make a string with key-value pairs:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def format_key_value_pairs(word):\n",
" \n",
" output = \"\"\n",
" \n",
" keys = get_keys_containing(word)\n",
" \n",
" for item in keys:\n",
" key = \"'{}'\".format(item)\n",
" value = mpl_params[item]\n",
" \n",
" pair = (\"{:>40}:{}\".format(key, value))\n",
" \n",
" output += pair + \"\\n\"\n",
" \n",
" return output"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"font_keys = format_key_value_pairs(\"font\")\n",
"edge_keys = format_key_value_pairs(\"edge\")\n",
"\n",
"print font_keys, edge_keys\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 'font.cursive':['Apple Chancery', 'Textile', 'Zapf Chancery', 'Sand', 'cursive']\n",
" 'font.family':sans-serif\n",
" 'font.fantasy':['Comic Sans MS', 'Chicago', 'Charcoal', 'ImpactWestern', 'fantasy']\n",
" 'font.monospace':['Bitstream Vera Sans Mono', 'DejaVu Sans Mono', 'Andale Mono', 'Nimbus Mono L', 'Courier New', 'Courier', 'Fixed', 'Terminal', 'monospace']\n",
" 'font.sans-serif':['Bitstream Vera Sans', 'DejaVu Sans', 'Lucida Grande', 'Verdana', 'Geneva', 'Lucid', 'Arial', 'Helvetica', 'Avant Garde', 'sans-serif']\n",
" 'font.serif':['Bitstream Vera Serif', 'DejaVu Serif', 'New Century Schoolbook', 'Century Schoolbook L', 'Utopia', 'ITC Bookman', 'Bookman', 'Nimbus Roman No9 L', 'Times New Roman', 'Times', 'Palatino', 'Charter', 'serif']\n",
" 'font.size':12\n",
" 'font.stretch':normal\n",
" 'font.style':normal\n",
" 'font.variant':normal\n",
" 'font.weight':normal\n",
" 'legend.fontsize':large\n",
" 'mathtext.fontset':cm\n",
" 'pdf.fonttype':3\n",
" 'pdf.use14corefonts':False\n",
" 'pgf.rcfonts':True\n",
" 'ps.fonttype':3\n",
" 'svg.fonttype':path\n",
" 'axes.edgecolor':k\n",
" 'figure.edgecolor':w\n",
" 'lines.markeredgewidth':0.5\n",
" 'patch.edgecolor':k\n",
" 'savefig.edgecolor':w\n",
"\n"
]
}
],
"prompt_number": 9
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment