'Add dpi to seaborn or export them with a given dpi

I have a visualization code like:

for cluster in ready_couples_2.cluster.unique():
    sns.set(rc={'figure.figsize':(11.7,8.27)})
    # mask the cluster of interest
    is_cluster = ready_couples_2.cluster.eq(cluster)

    ax = ready_couples_2[~is_cluster].plot.scatter(x='longitude',y='latitude', c='gray') 

    ax = sns.scatterplot(data=ready_couples_2[is_cluster],
                    x='longitude', 
                    y='latitude',
                    hue='id_easy',
                    ax=ax)
    ax.legend_.remove()

    figure = ax.get_figure()    
    figure.savefig('test.png', dpi=500)

    plt.show()

But figure is saving only one plot out of given plots. How to save them all OR how to define DPI while plotting?



Solution 1:[1]

Well, it does save all of your figures, but because you don't change the file name the last plot will be the only one you see.

You can for example do

for ind, cluster in enumerate(ready_couples_2.cluster.unique()):
    ...
    figure.savefig('test%d.png' % ind, dpi=500)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hielke Walinga