.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_colored_line.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_plot_colored_line.py: Colored line plotting ===================== An example of the plot_colored_line function, plotting the sine and cosine functions, colored by their derivatives .. GENERATED FROM PYTHON SOURCE LINES 7-58 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_colored_line_001.svg :alt: plot colored line :srcset: /auto_examples/images/sphx_glr_plot_colored_line_001.svg :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_colored_line_002.svg :alt: plot colored line :srcset: /auto_examples/images/sphx_glr_plot_colored_line_002.svg :class: sphx-glr-multi-img .. code-block:: Python # ============================================================================== # Standard Python modules # ============================================================================== # ============================================================================== # External Python modules # ============================================================================== import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import TwoSlopeNorm import niceplots # ============================================================================== # Extension modules # ============================================================================== plt.style.use(niceplots.get_style()) x = np.linspace(0, 2 * np.pi, 100) y = np.sin(x) c = np.cos(x) fig, ax = plt.subplots() niceplots.plot_colored_line( x, y, c, cmap="coolwarm", fig=fig, ax=ax, addColorBar=True, cRange=None, cBarLabel="$dy/dx$", clip_on=False ) niceplots.plot_colored_line(x, c, -y, cmap="coolwarm", fig=fig, ax=ax, clip_on=False) niceplots.adjust_spines(ax) ax.set_xlabel("$x$") ax.set_ylabel("$y$", rotation="horizontal", ha="right") ax.set_xticks(np.linspace(0, 2, 5) * np.pi) ax.set_xticklabels([0, r"$\frac{\pi}{2}$", r"$\pi$", r"$\frac{3\pi}{2}$", r"$2\pi$"]) ax.set_xlim(0, 2 * np.pi) niceplots.save_figs(fig, "colored_line", ["png", "svg"]) # Use a custom norm to specify the colormap range divnorm = TwoSlopeNorm(vmin=-1.0, vcenter=0.8, vmax=1.0) fig, ax = niceplots.plot_colored_line( x, y, c, cmap="coolwarm", norm=divnorm, addColorBar=False, cBarLabel="$dy/dx$", clip_on=False ) niceplots.adjust_spines(ax) ax.set_xlabel("$x$") ax.set_ylabel("$y$", rotation="horizontal", ha="right") ax.set_xticks(np.linspace(0, 2, 5) * np.pi) ax.set_xticklabels([0, r"$\frac{\pi}{2}$", r"$\pi$", r"$\frac{3\pi}{2}$", r"$2\pi$"]) ax.set_xlim(0, 2 * np.pi) niceplots.save_figs(fig, "colored_line_custom_norm", ["png", "svg"]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.266 seconds) .. _sphx_glr_download_auto_examples_plot_colored_line.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_colored_line.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_colored_line.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_colored_line.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_