.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/contours.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_gallery_contours.py: Countour plots ========================= Generating contour plots directly from data. .. GENERATED FROM PYTHON SOURCE LINES 8-13 Support for contour plots directly from a table of precomputed data is relatively limited in `pgfplots`. [Matplotlib](https://matplotlib.org/) uses `contourpy` to generate contours out of a matrix of `z`-values. Using the same underlying engine, `pykz` can bring this same type of plots directly to pgfplots. .. GENERATED FROM PYTHON SOURCE LINES 15-16 We import the usual libraries. .. GENERATED FROM PYTHON SOURCE LINES 16-19 .. code-block:: Python import numpy as np import pykz .. GENERATED FROM PYTHON SOURCE LINES 20-22 Let's generate some data. We do this in exactly the same way as you would using Matplotlib. .. GENERATED FROM PYTHON SOURCE LINES 22-34 .. code-block:: Python x = np.linspace(0, 1, 50) y = np.linspace(0, 1, 50) def f(x, y): return np.sin(2 * np.pi * x) * np.cos(2 * np.pi * y) X, Y = np.meshgrid(x, y) Z = f(X, Y) .. GENERATED FROM PYTHON SOURCE LINES 35-37 By default, `pykz` picks the contour levels to draw by dividing the range of `z`-values into equally spaced bins. .. GENERATED FROM PYTHON SOURCE LINES 37-47 .. code-block:: Python contours = pykz.contour(X, Y, Z) pykz.xlabel("$x$") pykz.ylabel("$y$") # Export your tex code as a standalone file pykz.save("contour_plot.tex", standalone=True) # Build the pdf pykz.io.export_pdf_from_file("contour_plot.tex") .. image-sg:: /gallery/images/sphx_glr_contours_001.png :alt: contours :srcset: /gallery/images/sphx_glr_contours_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Removing contour_plot.log Removing contour_plot.aux PosixPath('contour_plot.pdf') .. GENERATED FROM PYTHON SOURCE LINES 48-51 Of course you can also customize the plot in all kinds of ways. For instance, let us change the colormap and visualize the z-values with a colorbar. Also, let us set the x and y limits to be tight. .. GENERATED FROM PYTHON SOURCE LINES 51-62 .. code-block:: Python pykz.colorbar() pykz.gca().set_option("colormap name", "viridis") pykz.gca().enlarge_limits(0) # Export your tex code as a standalone file pykz.save("contour_plot_fancy.tex", standalone=True) # Optionally, build the pdf pykz.io.export_pdf_from_file("contour_plot_fancy.tex") .. image-sg:: /gallery/images/sphx_glr_contours_002.png :alt: contours :srcset: /gallery/images/sphx_glr_contours_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Removing contour_plot_fancy.log Removing contour_plot_fancy.aux PosixPath('contour_plot_fancy.pdf') .. GENERATED FROM PYTHON SOURCE LINES 63-68 Similarly, we can create filled contours. Let's get rid of the existing contour, and replace it with a filled contour. Alternatively, we could of course just create a new figure as well. This time, let's also specify the contour levels we want to show, .. GENERATED FROM PYTHON SOURCE LINES 68-79 .. code-block:: Python for contour in contours: pykz.gca().remove(contour) levels = np.linspace(-1, 1, 20) contourfs = pykz.contourf(X, Y, Z, levels=levels) # Export your tex code as a standalone file pykz.save("contour_plot_filled.tex", standalone=True) # Optionally, build the pdf pykz.io.export_pdf_from_file("contour_plot_filled.tex") .. image-sg:: /gallery/images/sphx_glr_contours_003.png :alt: contours :srcset: /gallery/images/sphx_glr_contours_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Removing contour_plot_filled.log Removing contour_plot_filled.aux PosixPath('contour_plot_filled.pdf') .. GENERATED FROM PYTHON SOURCE LINES 80-84 Or, we could go even further in customizing the appearance of the end result, using options that get passed to the generated `pgfplots` `addplot` command. Essentially any option that is recognized by `pgfplots` can be directly added here. .. GENERATED FROM PYTHON SOURCE LINES 84-95 .. code-block:: Python for contour in contourfs: pykz.gca().remove(contour) levels = np.linspace(-1, 1, 20) contourfs = pykz.contourf(X, Y, Z, levels=levels, draw=True, fill_opacity=0.6) # Export your tex code as a standalone file pykz.save("contour_plot_filled_custom.tex", standalone=True) # Optionally, build the pdf path_png = pykz.io.export_png_from_file("contour_plot_filled_custom.tex") print(path_png) .. image-sg:: /gallery/images/sphx_glr_contours_004.png :alt: contours :srcset: /gallery/images/sphx_glr_contours_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Removing contour_plot_filled_custom.log Removing contour_plot_filled_custom.aux contour_plot_filled_custom.png .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 7.206 seconds) .. _sphx_glr_download_gallery_contours.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: contours.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: contours.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: contours.zip ` .. include:: contours.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_