{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Format ticks using engineering notation\n\nUse of the engineering Formatter.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom matplotlib.ticker import EngFormatter\n\n# Fixing random state for reproducibility\nprng = np.random.RandomState(19680801)\n\n# Create artificial data to plot.\n# The x data span over several decades to demonstrate several SI prefixes.\nxs = np.logspace(1, 9, 100)\nys = (0.8 + 0.4 * prng.uniform(size=100)) * np.log10(xs)**2\n\n# Figure width is doubled (2*6.4) to display nicely 2 subplots side by side.\nfig, (ax0, ax1) = plt.subplots(nrows=2, figsize=(7, 9.6))\nfor ax in (ax0, ax1):\n ax.set_xscale('log')\n\n# Demo of the default settings, with a user-defined unit label.\nax0.set_title('Full unit ticklabels, w/ default precision & space separator')\nformatter0 = EngFormatter(unit='Hz')\nax0.xaxis.set_major_formatter(formatter0)\nax0.plot(xs, ys)\nax0.set_xlabel('Frequency')\n\n# Demo of the options `places` (number of digit after decimal point) and\n# `sep` (separator between the number and the prefix/unit).\nax1.set_title('SI-prefix only ticklabels, 1-digit precision & '\n 'thin space separator')\nformatter1 = EngFormatter(places=1, sep=\"\\N{THIN SPACE}\") # U+2009\nax1.xaxis.set_major_formatter(formatter1)\nax1.plot(xs, ys)\nax1.set_xlabel('Frequency [Hz]')\n\nplt.tight_layout()\nplt.show()" ] } ], "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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }