.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plotting/_scatter.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plotting__scatter.py: =================== 6.2 scatter plot =================== .. GENERATED FROM PYTHON SOURCE LINES 7-29 .. code-block:: default # sphinx_gallery_thumbnail_number = 4 import numpy as np import pandas as pd import matplotlib.pyplot as plt from easy_mpl.utils import add_cbar from matplotlib.lines import Line2D from easy_mpl.utils import version_info from easy_mpl.utils import map_array_to_cmap from easy_mpl import scatter f = "https://raw.githubusercontent.com/AtrCheema/AI4Water/master/ai4water/datasets/arg_busan.csv" dataframe = pd.read_csv(f, index_col='index') dataframe = dataframe[['tide_cm', 'pcp_mm', 'sal_psu', 'pcp12_mm', 'sul1_coppml', 'tetx_coppml', 'blaTEM_coppml', 'aac_coppml']] version_info() # print version information of all the packages being used .. rst-class:: sphx-glr-script-out .. code-block:: none {'easy_mpl': '0.21.4', 'matplotlib': '3.8.4', 'numpy': '1.26.4', 'pandas': '1.5.3', 'scipy': '1.13.1'} .. GENERATED FROM PYTHON SOURCE LINES 30-35 .. code-block:: default x = np.linspace(0, 10, 100) y = np.sin(x) _ = scatter(x, y) .. image-sg:: /auto_examples/plotting/images/sphx_glr__scatter_001.png :alt: scatter :srcset: /auto_examples/plotting/images/sphx_glr__scatter_001.png, /auto_examples/plotting/images/sphx_glr__scatter_001_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 36-39 Instead of drawing all the markers in same color, we can make the color to show something useful. Below, the color represents ``y`` values. .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: default _ = scatter(x, y, c=y) .. image-sg:: /auto_examples/plotting/images/sphx_glr__scatter_002.png :alt: scatter :srcset: /auto_examples/plotting/images/sphx_glr__scatter_002.png, /auto_examples/plotting/images/sphx_glr__scatter_002_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 42-47 As the value of ``y`` goes higher, the color of marker becomes yellowish. On the other hand, as the value of ``y`` goes lower, the color becomes bluish. Instead of making the color to show values of `y`, can use another array for the color. .. GENERATED FROM PYTHON SOURCE LINES 47-51 .. code-block:: default z = np.arange(100) _ = scatter(x, y, c=z) .. image-sg:: /auto_examples/plotting/images/sphx_glr__scatter_003.png :alt: scatter :srcset: /auto_examples/plotting/images/sphx_glr__scatter_003.png, /auto_examples/plotting/images/sphx_glr__scatter_003_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 52-55 Now the color of marker changes from left to right instead of from bottom to top. We can show the colorbar by setting the ``colorbar`` to True. .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: default _ = scatter(x, y, c=y, colorbar=True) .. image-sg:: /auto_examples/plotting/images/sphx_glr__scatter_004.png :alt: scatter :srcset: /auto_examples/plotting/images/sphx_glr__scatter_004.png, /auto_examples/plotting/images/sphx_glr__scatter_004_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 59-61 The function ``scatter`` returns a tuple. The first argument is a matplotlib axes which can be used for further processing .. GENERATED FROM PYTHON SOURCE LINES 61-65 .. code-block:: default axes, _ = scatter(x, y, show=False) assert isinstance(axes, plt.Axes) .. image-sg:: /auto_examples/plotting/images/sphx_glr__scatter_005.png :alt: scatter :srcset: /auto_examples/plotting/images/sphx_glr__scatter_005.png, /auto_examples/plotting/images/sphx_glr__scatter_005_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 66-67 We can provide the actual values of rbg as list/array to color/c argument. .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: default colors, _ = map_array_to_cmap(y, "Blues") _ = scatter(x, y, color=colors, colorbar=True) .. image-sg:: /auto_examples/plotting/images/sphx_glr__scatter_006.png :alt: scatter :srcset: /auto_examples/plotting/images/sphx_glr__scatter_006.png, /auto_examples/plotting/images/sphx_glr__scatter_006_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 71-73 However, if we show the colorbar, the colorbar in such a case will be `wrong `_. .. GENERATED FROM PYTHON SOURCE LINES 73-79 .. code-block:: default colors, mapper = map_array_to_cmap(y, "Blues") ax, sc = scatter(x, y, color=colors, show=False) plt.colorbar(mapper, ax=ax) # we must provide the mapper to ``colorbar`` otherwise colorbar will be wrong plt.show() .. image-sg:: /auto_examples/plotting/images/sphx_glr__scatter_007.png :alt: scatter :srcset: /auto_examples/plotting/images/sphx_glr__scatter_007.png, /auto_examples/plotting/images/sphx_glr__scatter_007_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-81 The properties of the markers can be manipulated. .. GENERATED FROM PYTHON SOURCE LINES 81-84 .. code-block:: default _ = scatter(x, y, edgecolors='black', linewidth=0.5) .. image-sg:: /auto_examples/plotting/images/sphx_glr__scatter_008.png :alt: scatter :srcset: /auto_examples/plotting/images/sphx_glr__scatter_008.png, /auto_examples/plotting/images/sphx_glr__scatter_008_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 85-92 We can use any color map to show the marker colors. A complete list of valid matplotlib colormaps can be found `here `_ The size of the marker can be tuned using ``s`` keyword. If a single value is provided, all markers will be of single size. We can however make each maker of variable size by passing an array to ``s`` keyword. .. GENERATED FROM PYTHON SOURCE LINES 92-106 .. code-block:: default time = [1, 2, 3, 4, 5, 7, 5.9, 5.5] parameters = [100, 200, 300, 400, 500, 350, 450, 800] performance = [0.1, 0.2, 0.3, 0.4, 0.5, 0.4, 0.52, 0.76] year = [2011, 2012, 2013, 2014, 2015, 2016, 2015, 2014] _ = scatter(time, year, c=performance, s=parameters, colorbar=True, edgecolors='black', linewidth=1.0, cmap="RdBu", ax_kws={"xlabel":"Computation Time (hours)", 'ylabel': "Publish Year", 'xlabel_kws': {"fontsize": 14},'ylabel_kws': {"fontsize": 14}, 'top_spine': False, 'right_spine': False}) .. image-sg:: /auto_examples/plotting/images/sphx_glr__scatter_009.png :alt: scatter :srcset: /auto_examples/plotting/images/sphx_glr__scatter_009.png, /auto_examples/plotting/images/sphx_glr__scatter_009_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 107-108 We can also annotate the markers by providing ``marker_labels`` argument. .. GENERATED FROM PYTHON SOURCE LINES 108-126 .. code-block:: default time = [1, 2, 3, 4, 5, 7, 5.9, 5.5] parameters = [100, 200, 300, 400, 500, 350, 450, 800] performance = [0.1, 0.2, 0.3, 0.4, 0.5, 0.4, 0.52, 0.76] year = [2011, 2012, 2013, 2014, 2015, 2016, 2015, 2014], names = ["A", "B", "C", "D", "E", "F", "G", "H"] _ = scatter(time, year, c=performance, s=parameters, colorbar=True, edgecolors='black', linewidth=1.0, cmap="RdBu", marker_labels=names, yoffset=0.2, xoffset=0.3, ax_kws={"xlabel":"Computation Time (hours)", 'ylabel': "Publish Year", 'xlabel_kws': {"fontsize": 14},'ylabel_kws': {"fontsize": 14}, 'top_spine': False, 'right_spine': False, 'tight_layout': True}) .. image-sg:: /auto_examples/plotting/images/sphx_glr__scatter_010.png :alt: scatter :srcset: /auto_examples/plotting/images/sphx_glr__scatter_010.png, /auto_examples/plotting/images/sphx_glr__scatter_010_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 127-128 unique colors for group of values .. GENERATED FROM PYTHON SOURCE LINES 128-166 .. code-block:: default df = dataframe.dropna().reset_index(drop=True) tide = df['tide_cm'] tetx = df['tetx_coppml'] colors = np.full(len(tide), fill_value="#E69F00") colors[np.argwhere(tide.values<0.0)] = "#56B4E9" ax, pc = scatter(np.arange(len(tide)), tetx, ax_kws=dict(logy=True, ylabel="tetx coppml", ylabel_kws={"fontsize": 16}, top_spine=False, right_spine=False), color=colors, alpha=0.5, zorder=10) fig = ax.get_figure() # Create handles for lines. handles = [ Line2D( [], [], label=label, lw=0, # there's no line added, just the marker marker="o", # circle marker markersize=10, markerfacecolor=colors[idx], # marker fill color ) for idx, label in enumerate(['Positive', 'Negative']) ] # Add legend ----------------------------------------------------- legend = fig.legend( handles=handles, bbox_to_anchor=[0.5, 0.9], # Located in the top-mid of the figure. fontsize=12, handletextpad=0.6, # Space between text and marker/line handlelength=1.4, columnspacing=1.4, loc="center", ncol=6, title_fontsize=16, title="Tide Type" ) fig.show() .. image-sg:: /auto_examples/plotting/images/sphx_glr__scatter_011.png :alt: scatter :srcset: /auto_examples/plotting/images/sphx_glr__scatter_011.png, /auto_examples/plotting/images/sphx_glr__scatter_011_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 167-168 marker style for group of values .. GENERATED FROM PYTHON SOURCE LINES 168-211 .. code-block:: default colors = "#009E73", "#E69F00" def make_color(array): clrs = np.full(len(array), fill_value=colors[0]) clrs[np.argwhere(array < 0.0)] = colors[1] return clrs markers = ["o", "^", "s"] # circle, triangle, square labels = ["No Rain", "Low Rain", "High Rain"] Y = [df.loc[df['pcp_mm']<=0.0], df.loc[(df['pcp_mm']>0.0) & (df['pcp_mm']<=1.0)], df.loc[df['pcp_mm']>1.0]] X = [df.loc[df['pcp_mm']<=0.0].index, df.loc[(df['pcp_mm']>0.0) & (df['pcp_mm']<=1.0)].index, df.loc[df['pcp_mm']>1.0].index] _ = ax = plt.subplots() for label, marker, x, y in zip(labels, markers, X, Y): color = make_color(y['tide_cm'].values) axes, pc = scatter(x=x, y=y['tetx_coppml'], marker=marker, ax_kws=dict(logy=True, ylabel="tetx coppml", ylabel_kws={"fontsize": 16}, top_spine=False, right_spine=False), color=color, alpha=0.5, zorder=10, label=label, show=False) handles = [Line2D([], [], label=label, marker="o", markersize=10, lw=0, markerfacecolor=colors[idx]) for idx, label in enumerate(['Positive', 'Negative']) ] fig = axes.get_figure() legend = fig.legend(handles=handles, bbox_to_anchor=[0.5, 0.9], title_fontsize=16,title="Tide Type", loc="center") leg = plt.legend(bbox_to_anchor=[0.8, 0.85], title="Rainfall", title_fontsize=16) for h in leg.legendHandles: h.set_facecolor('white') h.set_edgecolor('k') h.set_linewidth(2.0) plt.show() .. image-sg:: /auto_examples/plotting/images/sphx_glr__scatter_012.png :alt: scatter :srcset: /auto_examples/plotting/images/sphx_glr__scatter_012.png, /auto_examples/plotting/images/sphx_glr__scatter_012_2_0x.png 2.0x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/dev/scripts/plotting/_scatter.py:205: MatplotlibDeprecationWarning: The legendHandles attribute was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use legend_handles instead. for h in leg.legendHandles: .. GENERATED FROM PYTHON SOURCE LINES 212-235 .. code-block:: default df = dataframe.dropna().reset_index(drop=True) def draw_scatter(target, ax): #``visible`` argument for ``ax.grid`` not available in # matplotlib version 3.3 ax.grid(visible=True, ls='--', color='lightgrey') c, mapper = map_array_to_cmap(df['pcp12_mm'].values, "inferno") ax_, _ = scatter(np.arange(len(df)), df[target], color=c, alpha=0.5, s=40, ec="grey", zorder=10, ax_kws=dict(logy=True, ylabel=target, ylabel_kws={"fontsize": 12}, top_spine=False, right_spine=False, bottom_spine=False), ax=ax, show=False) add_cbar(ax_, mappable=mapper, orientation="horizontal", pad=0.3, border=False, title="Precipitation", title_kws=dict(fontsize=12)) return f, all_axes = plt.subplots(2,2, sharex="all", facecolor="#EFE9E6", figsize=(9, 6)) targets = ["tetx_coppml", "sul1_coppml", "aac_coppml", "blaTEM_coppml"] for col, axes in zip(targets, all_axes.flatten()): draw_scatter(col, axes) plt.show() .. image-sg:: /auto_examples/plotting/images/sphx_glr__scatter_013.png :alt: scatter :srcset: /auto_examples/plotting/images/sphx_glr__scatter_013.png, /auto_examples/plotting/images/sphx_glr__scatter_013_2_0x.png 2.0x :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 5.734 seconds) .. _sphx_glr_download_auto_examples_plotting__scatter.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/plotting/_scatter.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: _scatter.py <_scatter.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: _scatter.ipynb <_scatter.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_