{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# 3D voxel / volumetric plot with RGB colors\n\nDemonstrates using `.Axes3D.voxels` to visualize parts of a color space.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\n\ndef midpoints(x):\n sl = ()\n for _ in range(x.ndim):\n x = (x[sl + np.index_exp[:-1]] + x[sl + np.index_exp[1:]]) / 2.0\n sl += np.index_exp[:]\n return x\n\n# prepare some coordinates, and attach rgb values to each\nr, g, b = np.indices((17, 17, 17)) / 16.0\nrc = midpoints(r)\ngc = midpoints(g)\nbc = midpoints(b)\n\n# define a sphere about [0.5, 0.5, 0.5]\nsphere = (rc - 0.5)**2 + (gc - 0.5)**2 + (bc - 0.5)**2 < 0.5**2\n\n# combine the color components\ncolors = np.zeros(sphere.shape + (3,))\ncolors[..., 0] = rc\ncolors[..., 1] = gc\ncolors[..., 2] = bc\n\n# and plot everything\nax = plt.figure().add_subplot(projection='3d')\nax.voxels(r, g, b, sphere,\n facecolors=colors,\n edgecolors=np.clip(2*colors - 0.5, 0, 1), # brighter\n linewidth=0.5)\nax.set(xlabel='r', ylabel='g', zlabel='b')\nax.set_aspect('equal')\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags::\n plot-type: 3D,\n styling: color\n\n" ] } ], "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 }