.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/post_process/matplotlib_opt_post_processor.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_post_process_matplotlib_opt_post_processor.py: Customize with matplotlib ========================= In this example, we will see how to modify the matplotlib figures generated by an :class:`.BasePost`. This can be useful to finely tune a graph for a presentation or a paper. .. GENERATED FROM PYTHON SOURCE LINES 25-34 .. code-block:: Python from __future__ import annotations from matplotlib import pyplot as plt from gemseo import create_design_space from gemseo import create_discipline from gemseo import create_scenario .. GENERATED FROM PYTHON SOURCE LINES 35-37 We consider a minimization problem over the interval :math:`[0,1]` of the :math:`f(x)=x^2` objective function: .. GENERATED FROM PYTHON SOURCE LINES 37-46 .. code-block:: Python discipline = create_discipline("AnalyticDiscipline", expressions={"y": "x**2"}) design_space = create_design_space() design_space.add_variable("x", lower_bound=0.0, upper_bound=1.0) scenario = create_scenario( [discipline], "y", design_space, formulation_name="DisciplinaryOpt" ) .. GENERATED FROM PYTHON SOURCE LINES 47-48 We solve this optimization problem with the gradient-free algorithm COBYLA: .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: Python scenario.execute(algo_name="NLOPT_COBYLA", max_iter=10) .. GENERATED FROM PYTHON SOURCE LINES 51-53 Then, we can post-process this :class:`.MDOScenario` with an :class:`.OptHistoryView`. .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: Python opt_post_processor = scenario.post_process( post_name="OptHistoryView", show=True, save=False ) .. GENERATED FROM PYTHON SOURCE LINES 58-61 Instead of saving or showing this :class:`.OptHistoryView`, and so the associated :attr:`.OptHistoryView.figures`, we will slightly modify the latter which are matplotlib figures by default. .. GENERATED FROM PYTHON SOURCE LINES 61-64 .. code-block:: Python figures = opt_post_processor.figures print(figures.keys()) .. GENERATED FROM PYTHON SOURCE LINES 65-71 By default, the color bar representing the evolution of the optimization variables does not use labels. To add a custom label indicating that the optimization variables are scaled in :math:`[0,1]`, we can get the matplotlib ``Axes`` of the matplotlib figures: .. GENERATED FROM PYTHON SOURCE LINES 71-74 .. code-block:: Python figure = figures["variables"] axes = figure.axes .. GENERATED FROM PYTHON SOURCE LINES 75-76 and change the y-label of the color bar: .. GENERATED FROM PYTHON SOURCE LINES 76-78 .. code-block:: Python axes[1].set_ylabel("Optimization variables scales in [0,1]") .. GENERATED FROM PYTHON SOURCE LINES 79-80 We can also change the y-label of the graph: .. GENERATED FROM PYTHON SOURCE LINES 80-82 .. code-block:: Python axes[0].set_ylabel("Optimization variables") .. GENERATED FROM PYTHON SOURCE LINES 83-85 Lastly, we can plot all the figures: .. GENERATED FROM PYTHON SOURCE LINES 85-87 .. code-block:: Python plt.show() .. GENERATED FROM PYTHON SOURCE LINES 88-89 or save the modified figure: .. GENERATED FROM PYTHON SOURCE LINES 89-90 .. code-block:: Python figure.savefig("variables.png") .. _sphx_glr_download_examples_post_process_matplotlib_opt_post_processor.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: matplotlib_opt_post_processor.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: matplotlib_opt_post_processor.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: matplotlib_opt_post_processor.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_