From 8d820f2e81a360105a4db79998dca3af4c5b05f2 Mon Sep 17 00:00:00 2001 From: Walter Simson Date: Wed, 10 Jan 2024 11:51:06 -0800 Subject: [PATCH] update colorbar --- .../at_array_as_sensor.ipynb | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/examples/at_array_as_sensor/at_array_as_sensor.ipynb b/examples/at_array_as_sensor/at_array_as_sensor.ipynb index 516d3798..e8426e91 100644 --- a/examples/at_array_as_sensor/at_array_as_sensor.ipynb +++ b/examples/at_array_as_sensor/at_array_as_sensor.ipynb @@ -16,6 +16,7 @@ "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", + "import matplotlib as mpl\n", "import numpy as np\n", "\n", "from kwave.data import Vector\n", @@ -176,11 +177,23 @@ " 3: 'PML Mask',\n", "}\n", "\n", - "colorbar = plt.colorbar(c, ticks=list(labels.keys()), ax=ax)\n", - "colorbar.ax.set_yticklabels(list(labels.values()))\n", + "bounds = np.linspace(0, len(labels), len(labels)+1)\n", + "norm = mpl.colors.BoundaryNorm(bounds, cmap.N)\n", "\n", - "ax.set_xticks([])\n", - "ax.set_yticks([])\n", + "ax2 = fig.add_axes([0.95, 0.1, 0.03, 0.8])\n", + "cb = mpl.colorbar.ColorbarBase(ax2, cmap=cmap, norm=norm,\n", + " spacing='proportional', ticks=bounds, boundaries=bounds, format='%1i')\n", + "\n", + "# Update the title and label as before\n", + "ax.set_title('Simulation Layout')\n", + "ax2.set_ylabel('Simulation Components [-]', size=12)\n", + "\n", + "# Calculate the middle points for each segment of the colorbar\n", + "mid_points = [(bounds[i] + bounds[i+1])/2 for i in range(len(bounds)-1)]\n", + "\n", + "# Set the new tick positions and labels\n", + "ax2.set_yticks(mid_points)\n", + "ax2.set_yticklabels(list(labels.values()))\n", "\n", "plt.show()" ] @@ -239,7 +252,15 @@ "name": "python3" }, "language_info": { - "name": "python" + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3" } }, "nbformat": 4,