{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Color Cycling {#color_cycler_example}\n\nCycle through colors when sequentially adding meshes to a plotter.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many plotting libraries like Matplotlib cycle through a predefined list\nof colors to colorize the data being added to the graphic. PyVista\nsupports this in much the same way as Matplotlib.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pyvista as pv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Turn on color cycling in PyVista\\'s theme and set it to use the default\ncycler.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pv.global_theme.color_cycler = 'default'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List the available colors in the cycler\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pv.global_theme.color_cycler" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a plotter and add data to the scene. You\\'ll notice that each\n`add_mesh` call iterates over the colors in\n`pv.global_theme.color_cycler`\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pl = pv.Plotter()\npl.add_mesh(pv.Cone(center=(0, 0, 0)))\npl.add_mesh(pv.Cube(center=(1, 0, 0)))\npl.add_mesh(pv.Sphere(center=(1, 1, 0)))\npl.add_mesh(pv.Cylinder(center=(0, 1, 0)))\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reset the theme to not use a cycler and instead set on individual\nplotters.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pv.global_theme.color_cycler = None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you do not want to set a global color cycler but instead just want to\nuse a cycler for a single plotter, you can set this on with\n`set_color_cycler() <pyvista.Plotter.set_color_cycler>`{.interpreted-text\nrole=\"func\"}.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pl = pv.Plotter()\n\n# Set to iterate over Red, Green, and Blue\npl.set_color_cycler(['red', 'green', 'blue'])\n\npl.add_mesh(pv.Cone(center=(0, 0, 0))) # red\npl.add_mesh(pv.Cube(center=(1, 0, 0))) # green\npl.add_mesh(pv.Sphere(center=(1, 1, 0))) # blue\npl.add_mesh(pv.Cylinder(center=(0, 1, 0))) # red again\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Further, you can control this on a per-renderer basis by calling\n`set_color_cycler() <pyvista.Renderer.set_color_cycler>`{.interpreted-text\nrole=\"func\"} on the active `renderer`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pl = pv.Plotter(shape=(1, 2))\n\npl.subplot(0, 0)\npl.renderer.set_color_cycler('default')\npl.add_mesh(pv.Cone(center=(0, 0, 0)))\npl.add_mesh(pv.Cube(center=(1, 0, 0)))\npl.add_mesh(pv.Sphere(center=(1, 1, 0)))\npl.add_mesh(pv.Cylinder(center=(0, 1, 0)))\n\npl.subplot(0, 1)\npl.renderer.set_color_cycler(['magenta', 'seagreen', 'aqua', 'orange'])\npl.add_mesh(pv.Cone(center=(0, 0, 0)))\npl.add_mesh(pv.Cube(center=(1, 0, 0)))\npl.add_mesh(pv.Sphere(center=(1, 1, 0)))\npl.add_mesh(pv.Cylinder(center=(0, 1, 0)))\n\npl.link_views()\npl.view_isometric()\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also change the colors of actors after they are added to the\nscene.\n\nProTip: you could place the for-loop below in an event callback for a\nkey event to cycle through the colors on-demand. Or better yet, have\nyour cycler randomly select colors.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from cycler import cycler\n\npl = pv.Plotter()\npl.add_mesh(pv.Cone(center=(0, 0, 0)))\npl.add_mesh(pv.Cube(center=(1, 0, 0)))\npl.add_mesh(pv.Sphere(center=(1, 1, 0)))\npl.add_mesh(pv.Cylinder(center=(0, 1, 0)))\n\ncolors = cycler('color', ['lightcoral', 'seagreen', 'aqua', 'firebrick'])()\n\nfor actor in pl.renderer.actors.values():\n if isinstance(actor, pv.Actor):\n actor.prop.color = next(colors)['color']\n\npl.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.2" } }, "nbformat": 4, "nbformat_minor": 0 }