| 1 |
""" |
| 2 |
.. _chart_overlays_example: |
| 3 |
|
| 4 |
Chart Overlays |
| 5 |
~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
This example shows how you can combine multiple charts as overlays in |
| 8 |
the same renderer. For an overview of the different chart types you |
| 9 |
can use, please refer to :ref:`chart_basics_example`. Interaction with |
| 10 |
a chart can be enabled by a double left click on top of it. Note that this |
| 11 |
will disable interaction with the 3D scene. To stop interacting with |
| 12 |
the chart, perform another double left click. This will either enable |
| 13 |
interaction with another chart (if clicked on top of it) or re-enable |
| 14 |
interaction with the 3D scene. |
| 15 |
|
| 16 |
""" |
| 17 |
|
| 18 |
import matplotlib.pyplot as plt |
| 19 |
import numpy as np |
| 20 |
import pyvista as pv |
| 21 |
|
| 22 |
# sphinx_gallery_start_ignore |
| 23 |
# chart background images do not seem to work in interactive mode |
| 24 |
PYVISTA_GALLERY_FORCE_STATIC_IN_DOCUMENT = True |
| 25 |
# sphinx_gallery_end_ignore |
| 26 |
|
| 27 |
# %% |
| 28 |
# Data to display |
| 29 |
t = np.linspace(0, 5, 50) |
| 30 |
h = np.sin(t) |
| 31 |
v = np.cos(t) |
| 32 |
|
| 33 |
# %% |
| 34 |
# Define a Matplotlib figure. |
| 35 |
# Use a tight layout to keep axis labels visible on smaller figures. |
| 36 |
|
| 37 |
f, ax = plt.subplots(tight_layout=True) |
| 38 |
h_line = ax.plot(t[:1], h[:1])[0] |
| 39 |
ax.set_ylim([-1, 1]) |
| 40 |
ax.set_xlabel('Time (s)') |
| 41 |
_ = ax.set_ylabel('Height (m)') |
| 42 |
# sphinx_gallery_defer_figures |
| 43 |
|
| 44 |
# %% |
| 45 |
# Define plotter, add the created matplotlib figure as the first (left) chart |
| 46 |
# to the scene, and define a second (right) chart. |
| 47 |
|
| 48 |
pl = pv.Plotter() |
| 49 |
h_chart = pv.ChartMPL(f, size=(0.46, 0.25), loc=(0.02, 0.06)) |
| 50 |
h_chart.background_color = (1.0, 1.0, 1.0, 0.4) |
| 51 |
pl.add_chart(h_chart) |
| 52 |
v_chart = pv.Chart2D( |
| 53 |
size=(0.46, 0.25), |
| 54 |
loc=(0.52, 0.06), |
| 55 |
x_label='Time (s)', |
| 56 |
y_label='Velocity (m/s)', |
| 57 |
) |
| 58 |
v_line = v_chart.line(t[:1], v[:1]) |
| 59 |
v_chart.y_range = (-1, 1) |
| 60 |
v_chart.background_color = (1.0, 1.0, 1.0, 0.4) |
| 61 |
pl.add_chart(v_chart) |
| 62 |
pl.add_mesh(pv.Sphere(radius=1), name='sphere', render=False) |
| 63 |
pl.show(auto_close=False, interactive=True, interactive_update=True) |
| 64 |
|
| 65 |
|
| 66 |
# Method and slider to update all visuals based on the time selection |
| 67 |
def update_time(time): |
| 68 |
k = np.count_nonzero(t < time) |
| 69 |
h_line.set_xdata(t[: k + 1]) |
| 70 |
h_line.set_ydata(h[: k + 1]) |
| 71 |
v_line.update(t[: k + 1], v[: k + 1]) |
| 72 |
pl.add_mesh(pv.Sphere(radius=1, center=(0, 0, h[k])), name='sphere', render=False) |
| 73 |
pl.update() |
| 74 |
|
| 75 |
|
| 76 |
time_slider = pl.add_slider_widget( |
| 77 |
update_time, |
| 78 |
rng=[np.min(t), np.max(t)], |
| 79 |
value=0, |
| 80 |
title='Time', |
| 81 |
pointa=(0.25, 0.9), |
| 82 |
pointb=(0.75, 0.9), |
| 83 |
interaction_event='always', |
| 84 |
) |
| 85 |
|
| 86 |
# Start incrementing time automatically |
| 87 |
for i in range(1, 50): |
| 88 |
ax.set_xlim([0, t[i]]) |
| 89 |
time_slider.GetSliderRepresentation().SetValue(t[i]) |
| 90 |
update_time(t[i]) |
| 91 |
|
| 92 |
pl.show() # Keep plotter open to let user play with time slider |
| 93 |
# %% |
| 94 |
# .. tags:: plot |