{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Chart Overlays {#chart_overlays_example}\n==============\n\nThis example shows how you can combine multiple charts as overlays in\nthe same renderer. For an overview of the different chart types you can\nuse, please refer to `chart_basics_example`{.interpreted-text\nrole=\"ref\"}. Interaction with a chart can be enabled by a double left\nclick on top of it. Note that this will disable interaction with the 3D\nscene. To stop interacting with the chart, perform another double left\nclick. This will either enable interaction with another chart (if\nclicked on top of it) or re-enable interaction with the 3D scene.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport pyvista as pv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Data to display\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t = np.linspace(0, 5, 50)\nh = np.sin(t)\nv = np.cos(t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a Matplotlib figure. Use a tight layout to keep axis labels\nvisible on smaller figures.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "f, ax = plt.subplots(tight_layout=True)\nh_line = ax.plot(t[:1], h[:1])[0]\nax.set_ylim([-1, 1])\nax.set_xlabel('Time (s)')\n_ = ax.set_ylabel('Height (m)')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define plotter, add the created matplotlib figure as the first (left)\nchart to the scene, and define a second (right) chart.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = pv.Plotter()\nh_chart = pv.ChartMPL(f, size=(0.46, 0.25), loc=(0.02, 0.06))\nh_chart.background_color = (1.0, 1.0, 1.0, 0.4)\np.add_chart(h_chart)\nv_chart = pv.Chart2D(\n size=(0.46, 0.25), loc=(0.52, 0.06), x_label=\"Time (s)\", y_label=\"Velocity (m/s)\"\n)\nv_line = v_chart.line(t[:1], v[:1])\nv_chart.y_range = (-1, 1)\nv_chart.background_color = (1.0, 1.0, 1.0, 0.4)\np.add_chart(v_chart)\np.add_mesh(pv.Sphere(1), name=\"sphere\", render=False)\np.show(auto_close=False, interactive=True, interactive_update=True)\n\n\n# Method and slider to update all visuals based on the time selection\ndef update_time(time):\n k = np.count_nonzero(t < time)\n h_line.set_xdata(t[: k + 1])\n h_line.set_ydata(h[: k + 1])\n v_line.update(t[: k + 1], v[: k + 1])\n p.add_mesh(pv.Sphere(1, center=(0, 0, h[k])), name=\"sphere\", render=False)\n p.update()\n\n\ntime_slider = p.add_slider_widget(\n update_time,\n [np.min(t), np.max(t)],\n 0,\n \"Time\",\n (0.25, 0.9),\n (0.75, 0.9),\n interaction_event='always',\n)\n\n# Start incrementing time automatically\nfor i in range(1, 50):\n ax.set_xlim([0, t[i]])\n time_slider.GetSliderRepresentation().SetValue(t[i])\n update_time(t[i])\n\np.show() # Keep plotter open to let user play with time slider" ] } ], "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 }