.. 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-32 .. code-block:: Python from __future__ import annotations from matplotlib import pyplot as plt from gemseo.datasets.dataset import Dataset from gemseo.post.dataset.yvsx import YvsX .. GENERATED FROM PYTHON SOURCE LINES 33-36 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 36-40 .. code-block:: Python dataset = Dataset() dataset.add_variable("a", [[1], [2], [3]]) dataset.add_variable("b", [[1], [0], [1]]) .. GENERATED FROM PYTHON SOURCE LINES 41-43 Then, we define a :class:`YvsX` chart, which is a particular :class:`.DatasetPlot`: .. GENERATED FROM PYTHON SOURCE LINES 43-45 .. code-block:: Python yvsx = YvsX(dataset, "a", "b") .. GENERATED FROM PYTHON SOURCE LINES 46-47 and draw the figure. .. GENERATED FROM PYTHON SOURCE LINES 47-49 .. code-block:: Python figures = yvsx.execute(save=False) .. GENERATED FROM PYTHON SOURCE LINES 50-58 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 58-61 .. code-block:: Python figure = figures[0] ax = figure.axes[0] .. GENERATED FROM PYTHON SOURCE LINES 62-63 and change the labels: .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: Python ax.set_xlabel("A relevant x-label") ax.set_ylabel("A relevant y-label") .. GENERATED FROM PYTHON SOURCE LINES 67-68 We can also add a grid: .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: Python ax.grid() .. GENERATED FROM PYTHON SOURCE LINES 71-73 Lastly, we can save the figure with matplotlib .. GENERATED FROM PYTHON SOURCE LINES 73-75 .. code-block:: Python plt.savefig("foo.png") .. GENERATED FROM PYTHON SOURCE LINES 76-77 and display it: .. GENERATED FROM PYTHON SOURCE LINES 77-79 .. code-block:: Python plt.show() .. GENERATED FROM PYTHON SOURCE LINES 80-87 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. .. _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-jupyter :download:`Download Jupyter notebook: matplotlib_dataset_plot.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: matplotlib_dataset_plot.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_