Debian Codegraph @

examples/02-plot/color_cycler.py

raw
1 """
2 .. _color_cycler_example:
3
4 Color Cycling
5 ~~~~~~~~~~~~~
6
7 Cycle through colors when sequentially adding meshes to a plotter.
8 """
9
10 # %%
11 # Many plotting libraries like Matplotlib cycle through a predefined list of
12 # colors to colorize the data being added to the graphic. PyVista supports
13 # this in much the same way as Matplotlib.
14
15 # sphinx_gallery_thumbnail_number = 1
16 import pyvista as pv
17
18 # %%
19 # Turn on color cycling in PyVista's theme and set it to use the default
20 # cycler.
21 pv.global_theme.color_cycler = 'default'
22
23 # %%
24 # List the available colors in the cycler
25 pv.global_theme.color_cycler
26
27 # %%
28 # Create a plotter and add data to the scene. You'll notice that each
29 # ``add_mesh`` call iterates over the colors in ``pv.global_theme.color_cycler``
30 pl = pv.Plotter()
31 pl.add_mesh(pv.Cone(center=(0, 0, 0)))
32 pl.add_mesh(pv.Cube(center=(1, 0, 0)))
33 pl.add_mesh(pv.Sphere(center=(1, 1, 0)))
34 pl.add_mesh(pv.Cylinder(center=(0, 1, 0)))
35 pl.show()
36
37 # %%
38 # Reset the theme to not use a cycler and instead set on individual plotters.
39 pv.global_theme.color_cycler = None
40
41 # %%
42 # If you do not want to set a global color cycler but instead just want to
43 # use a cycler for a single plotter, you can set this on with
44 # :func:`set_color_cycler() <pyvista.Plotter.set_color_cycler>`.
45 pl = pv.Plotter()
46
47 # Set to iterate over Red, Green, and Blue
48 pl.set_color_cycler(['red', 'green', 'blue'])
49
50 pl.add_mesh(pv.Cone(center=(0, 0, 0))) # red
51 pl.add_mesh(pv.Cube(center=(1, 0, 0))) # green
52 pl.add_mesh(pv.Sphere(center=(1, 1, 0))) # blue
53 pl.add_mesh(pv.Cylinder(center=(0, 1, 0))) # red again
54 pl.show()
55
56 # %%
57 # Further, you can control this on a per-renderer basis by calling
58 # :func:`set_color_cycler() <pyvista.Renderer.set_color_cycler>` on the active
59 # ``renderer``.
60 pl = pv.Plotter(shape=(1, 2))
61
62 pl.subplot(0, 0)
63 pl.renderer.set_color_cycler('default')
64 pl.add_mesh(pv.Cone(center=(0, 0, 0)))
65 pl.add_mesh(pv.Cube(center=(1, 0, 0)))
66 pl.add_mesh(pv.Sphere(center=(1, 1, 0)))
67 pl.add_mesh(pv.Cylinder(center=(0, 1, 0)))
68
69 pl.subplot(0, 1)
70 pl.renderer.set_color_cycler(['magenta', 'seagreen', 'aqua', 'orange'])
71 pl.add_mesh(pv.Cone(center=(0, 0, 0)))
72 pl.add_mesh(pv.Cube(center=(1, 0, 0)))
73 pl.add_mesh(pv.Sphere(center=(1, 1, 0)))
74 pl.add_mesh(pv.Cylinder(center=(0, 1, 0)))
75
76 pl.link_views()
77 pl.view_isometric()
78 pl.show()
79
80
81 # %%
82 # You can also change the colors of actors after they are added to the scene.
83 #
84 # ProTip: you could place the for-loop below in an event callback for a key
85 # event to cycle through the colors on-demand. Or better yet, have your cycler
86 # randomly select colors.
87 from cycler import cycler
88
89 pl = pv.Plotter()
90 pl.add_mesh(pv.Cone(center=(0, 0, 0)))
91 pl.add_mesh(pv.Cube(center=(1, 0, 0)))
92 pl.add_mesh(pv.Sphere(center=(1, 1, 0)))
93 pl.add_mesh(pv.Cylinder(center=(0, 1, 0)))
94
95 colors = cycler('color', ['lightcoral', 'seagreen', 'aqua', 'firebrick'])()
96
97 for actor in pl.renderer.actors.values():
98 if isinstance(actor, pv.Actor):
99 actor.prop.color = next(colors)['color']
100
101 pl.show()
102 # %%
103 # .. tags:: plot