.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/visualization/plot_timeseries_graphs.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_visualization_plot_timeseries_graphs.py: .. _ex-draw-timeseries-graphs: ================================================== Drawing timeseries graphs and setting their layout ================================================== One can draw timeseries graphs without setting the ``pos`` argument, in that case graphviz will choose how to place the nodes. This can look rather messy in the final output. In this example, we demonstrate how to visualize timeseries graphs effectively. For comparing different graphs with the same layout, see :ref:`ex-draw-graphs` .. GENERATED FROM PYTHON SOURCE LINES 16-32 .. code-block:: Python from pywhy_graphs import StationaryTimeSeriesDiGraph from pywhy_graphs.viz import draw, timeseries_layout # create some dummy graphs: G, admg, cpdag, and pag # this code is borrowed from the other example: intro_causal_graphs.py ;) G = StationaryTimeSeriesDiGraph(max_lag=3) ts_edges = [ (("x1", -1), ("x1", 0)), (("x1", -1), ("x2", 0)), (("x3", -1), ("x2", 0)), (("x3", -1), ("x3", 0)), (("x1", -3), ("x3", 0)), ] G.add_edges_from(ts_edges) .. GENERATED FROM PYTHON SOURCE LINES 33-34 First we will draw the graph without setting positions .. GENERATED FROM PYTHON SOURCE LINES 34-39 .. code-block:: Python dot_G = draw(G) # render the graphs using graphviz render() function dot_G.render(outfile="default_G.png", view=True) .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_timeseries_graphs_001.png :alt: plot timeseries graphs :srcset: /auto_examples/visualization/images/sphx_glr_plot_timeseries_graphs_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 'default_G.png' .. GENERATED FROM PYTHON SOURCE LINES 40-42 Next, we will set positions using the `pywhy_graphs.viz.timeseries_layout` function, which creates a nice default layout for time-series graphs. .. GENERATED FROM PYTHON SOURCE LINES 42-57 .. code-block:: Python # get the layout position for the graph G using networkx pos_G = timeseries_layout(G, variable_order=["x1", "x2", "x3"], scale=10) # let us inspect the positions. # Notice that networkx and graphviz related software store positions as # a dictionary keyed by node with (x, y) coordinates as values. print(pos_G) # draw the graphs (i.e., generate a graphviz object that can be rendered) # each time we call draw() we pass the layout position of G dot_G = draw(G, pos=pos_G) # render the graphs using graphviz render() function dot_G.render(outfile="G.png", view=True, engine="neato") .. image-sg:: /auto_examples/visualization/images/sphx_glr_plot_timeseries_graphs_002.png :alt: plot timeseries graphs :srcset: /auto_examples/visualization/images/sphx_glr_plot_timeseries_graphs_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none {('x1', 0): array([10. , -7.5]), ('x1', -1): array([ 3.33333333, -7.5 ]), ('x1', -2): array([-3.33333333, -7.5 ]), ('x1', -3): array([-10. , -7.5]), ('x2', 0): array([10., 0.]), ('x2', -1): array([3.33333333, 0. ]), ('x2', -2): array([-3.33333333, 0. ]), ('x2', -3): array([-10., 0.]), ('x3', 0): array([10. , 7.5]), ('x3', -1): array([3.33333333, 7.5 ]), ('x3', -2): array([-3.33333333, 7.5 ]), ('x3', -3): array([-10. , 7.5])} 'G.png' .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.982 seconds) **Estimated memory usage:** 166 MB .. _sphx_glr_download_auto_examples_visualization_plot_timeseries_graphs.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_timeseries_graphs.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_timeseries_graphs.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_timeseries_graphs.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_