pythonseaborngoogle-maps-markersecdf

How to use markers with ECDF plot


In order to obtain a ECDF plot with seaborn, one shall do as follows:

sns.ecdfplot(data=myData, x='x', ax=axs, hue='mySeries')

This will give an ECDF plot for each of the series mySeries within myData.

Now, I'd like to use markers for each of these series. I've tried to use the same logic as one would use for example with a sns.lineplot, as follows:

sns.lineplot(data=myData,x='x',y='y',ax=axs,hue='mySeries',markers=True, style='mySeries',)

but, unfortunately, the keywords markers or style are not available for the sns.ecdf plot. I'm using seaborn 0.11.2.

For a reproducible example, the penguins dataset could be used:

import seaborn as sns

penguins = sns.load_dataset('penguins')
sns.ecdfplot(data=penguins, x="bill_length_mm", hue="species")

Solution

  • You could iterate through the generated lines and apply a marker. Here is an example using the penguins dataset, once with the default, then using markers and the third using different linestyles:

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    penguins = sns.load_dataset('penguins')
    
    fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(15, 4))
    
    sns.ecdfplot(data=penguins, x="bill_length_mm", hue="species", ax=ax1)
    ax1.set_title('Default')
    
    sns.ecdfplot(data=penguins, x="bill_length_mm", hue="species", ax=ax2)
    for lines, marker, legend_handle in zip(ax2.lines[::-1], ['*', 'o', '+'], ax2.legend_.legendHandles):
        lines.set_marker(marker)
        legend_handle.set_marker(marker)
    ax2.set_title('Using markers')
    
    sns.ecdfplot(data=penguins, x="bill_length_mm", hue="species", ax=ax3)
    for lines, linestyle, legend_handle in zip(ax3.lines[::-1], ['-', '--', ':'], ax3.legend_.legendHandles):
        lines.set_linestyle(linestyle)
        legend_handle.set_linestyle(linestyle)
    ax3.set_title('Using linestyles')
    
    plt.tight_layout()
    plt.show()
    

    ecdfplot with markers or linestyles