Files
jupyter-collection/scientific-computing-2/auto_examples_jupyter_2/options/plot_ticks.ipynb

43 lines
2.2 KiB
Plaintext
Raw Normal View History

2025-10-21 11:20:44 +08:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Locators for tick on axis\n\nAn example demoing different locators to position ticks on axis for\nmatplotlib.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n\nfrom matplotlib import ticker\nimport matplotlib.pyplot as plt\n\n\ndef tickline():\n plt.xlim(0, 10), plt.ylim(-1, 1), plt.yticks([])\n ax = plt.gca()\n ax.spines[\"right\"].set_color(\"none\")\n ax.spines[\"left\"].set_color(\"none\")\n ax.spines[\"top\"].set_color(\"none\")\n ax.xaxis.set_ticks_position(\"bottom\")\n ax.spines[\"bottom\"].set_position((\"data\", 0))\n ax.yaxis.set_ticks_position(\"none\")\n ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1))\n ax.plot(np.arange(11), np.zeros(11))\n return ax\n\n\nlocators = [\n \"ticker.NullLocator()\",\n \"ticker.MultipleLocator(1.0)\",\n \"ticker.FixedLocator([0, 2, 8, 9, 10])\",\n \"ticker.IndexLocator(3, 1)\",\n \"ticker.LinearLocator(5)\",\n \"ticker.LogLocator(2, [1.0])\",\n \"ticker.AutoLocator()\",\n]\n\nn_locators = len(locators)\n\nsize = 512, 40 * n_locators\ndpi = 72.0\nfigsize = size[0] / float(dpi), size[1] / float(dpi)\nfig = plt.figure(figsize=figsize, dpi=dpi)\nfig.patch.set_alpha(0)\n\n\nfor i, locator in enumerate(locators):\n plt.subplot(n_locators, 1, i + 1)\n ax = tickline()\n ax.xaxis.set_major_locator(eval(locator))\n plt.text(5, 0.3, locator[7:], ha=\"center\")\n\nplt.subplots_adjust(bottom=0.01, top=0.99, left=0.01, right=0.99)\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.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}