.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/dataset/visualization/matplotlib_dataset_plot.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_examples_dataset_visualization_matplotlib_dataset_plot.py: Customize with matplotlib ========================= In this example, we will see how to modify the matplotlib figures generated by a :class:`.DatasetPlot`. This can be useful to finely tune a graph for a presentation or a paper. .. GENERATED FROM PYTHON SOURCE LINES 24-30 .. code-block:: default from __future__ import annotations from gemseo.datasets.dataset import Dataset from gemseo.post.dataset.yvsx import YvsX from matplotlib import pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 31-34 First, we build a simple :class:`.Dataset` containing a cloud of points ``((1, 1), (2, 0), (3, 1))`` of the tuple ``("a", "b")``. .. GENERATED FROM PYTHON SOURCE LINES 34-38 .. code-block:: default dataset = Dataset() dataset.add_variable("a", [[1], [2], [3]]) dataset.add_variable("b", [[1], [0], [1]]) .. GENERATED FROM PYTHON SOURCE LINES 39-41 Then, we define a :class:`YvsX` chart, which is a particular :class:`.DatasetPlot`: .. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: default yvsx = YvsX(dataset, "a", "b") .. GENERATED FROM PYTHON SOURCE LINES 44-45 and draw the figure. .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: default figures = yvsx.execute(save=False) .. GENERATED FROM PYTHON SOURCE LINES 48-56 By default, the horizontal and vertical labels are the names of the variables, namely ``"a"`` and ``"b"`` in this example. As these labels are not so relevant, we are going to modify this figure directly with matplotlib instead of saving it. For that, we get the matplotlib ``Axes`` of the matplotlib ``Figure``: .. GENERATED FROM PYTHON SOURCE LINES 56-59 .. code-block:: default figure = figures[0] ax = figure.axes[0] .. GENERATED FROM PYTHON SOURCE LINES 60-61 and change the labels: .. GENERATED FROM PYTHON SOURCE LINES 61-64 .. code-block:: default ax.set_xlabel("A relevant x-label") ax.set_ylabel("A relevant y-label") .. GENERATED FROM PYTHON SOURCE LINES 65-66 We can also add a grid: .. GENERATED FROM PYTHON SOURCE LINES 66-68 .. code-block:: default ax.grid() .. GENERATED FROM PYTHON SOURCE LINES 69-71 Lastly, we can save the figure with matplotlib .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: default plt.savefig("foo.png") .. GENERATED FROM PYTHON SOURCE LINES 74-75 and display it: .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: default plt.show() .. GENERATED FROM PYTHON SOURCE LINES 78-85 Note that in this pedagogical example, we modified basic properties of the chart which could have been modified with the attributes of ``DatasetPlot``, e.g. ``yvsx.xlabel = "A relevant x-label"``. But it is impossible to add attributes or methods for all the features of matplotlib. It is therefore advisable to check if an attribute of :class:`.DatasetPlot` can help and if not, access and modify the matplotlib ``Figure`` as shown here. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.000 seconds) .. _sphx_glr_download_examples_dataset_visualization_matplotlib_dataset_plot.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: matplotlib_dataset_plot.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: matplotlib_dataset_plot.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_