{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Custom Colorbar Tickmarks"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2023-10-10T14:58:56.999636Z",
"iopub.status.busy": "2023-10-10T14:58:56.999166Z",
"iopub.status.idle": "2023-10-10T14:58:58.065204Z",
"shell.execute_reply": "2023-10-10T14:58:58.064231Z"
}
},
"outputs": [],
"source": [
"import yt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2023-10-10T14:58:58.070148Z",
"iopub.status.busy": "2023-10-10T14:58:58.069791Z",
"iopub.status.idle": "2023-10-10T14:59:01.392896Z",
"shell.execute_reply": "2023-10-10T14:59:01.392140Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"
"
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds = yt.load(\"IsolatedGalaxy/galaxy0030/galaxy0030\")\n",
"slc = yt.SlicePlot(ds, \"x\", (\"gas\", \"density\"))\n",
"slc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`PlotWindow` plots are containers for plots, keyed to field names. Below, we get a copy of the plot for the `Density` field."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2023-10-10T14:59:01.438584Z",
"iopub.status.busy": "2023-10-10T14:59:01.437938Z",
"iopub.status.idle": "2023-10-10T14:59:01.442600Z",
"shell.execute_reply": "2023-10-10T14:59:01.441616Z"
}
},
"outputs": [],
"source": [
"plot = slc.plots[(\"gas\", \"density\")]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The plot has a few attributes that point to underlying `matplotlib` plot primitives. For example, the `colorbar` object corresponds to the `cb` attribute of the plot."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2023-10-10T14:59:01.447040Z",
"iopub.status.busy": "2023-10-10T14:59:01.446682Z",
"iopub.status.idle": "2023-10-10T14:59:01.451023Z",
"shell.execute_reply": "2023-10-10T14:59:01.450140Z"
}
},
"outputs": [],
"source": [
"colorbar = plot.cb"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we call `_setup_plots()` to ensure the plot is properly initialized. Without this, the custom tickmarks we are adding will be ignored."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true,
"execution": {
"iopub.execute_input": "2023-10-10T14:59:01.457227Z",
"iopub.status.busy": "2023-10-10T14:59:01.456851Z",
"iopub.status.idle": "2023-10-10T14:59:01.461370Z",
"shell.execute_reply": "2023-10-10T14:59:01.460511Z"
}
},
"outputs": [],
"source": [
"slc._setup_plots()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To set custom tickmarks, simply call the `matplotlib` [`set_ticks`](https://matplotlib.org/stable/api/colorbar_api.html#matplotlib.colorbar.ColorbarBase.set_ticks) and [`set_ticklabels`](https://matplotlib.org/stable/api/colorbar_api.html#matplotlib.colorbar.ColorbarBase.set_ticklabels) functions."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2023-10-10T14:59:01.467299Z",
"iopub.status.busy": "2023-10-10T14:59:01.466936Z",
"iopub.status.idle": "2023-10-10T14:59:01.821505Z",
"shell.execute_reply": "2023-10-10T14:59:01.820538Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"
"
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"colorbar.set_ticks([1e-28])\n",
"colorbar.set_ticklabels([\"$10^{-28}$\"])\n",
"slc"
]
}
],
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
}