DataMan
DataMan

Reputation: 3455

seaborn scatterplot marker size for ALL markers

I can't find out anywhere how to change the marker size on seaborn scatterplots. There is a size option listed in the documentation but it is only for when you want variable size across points. I want the same size for all points but larger than the default!

I tried making a new column of integers in my dataframe and set that as the size, but it looks like the actual value doesn't matter, it changes the marker size on a relative basis, so in this case all the markers were still the same size as the default.

Here's some code:

ax = sns.scatterplot(x="Data Set Description", y="R Squared", data=mean_df)
plt.show()

I just tried something and it worked, not sure if it's the best method though. I added size=[1, 1, 1, 1, 1, 1] and sizes=(500, 500). So essentially I'm setting all sizes to be the same, and the range of sizes to be only at 500.

Upvotes: 123

Views: 189521

Answers (3)

cottontail
cottontail

Reputation: 23151

If you want to change the marker size for all plots, you can modify the marker size in matplotlib.rcParams. The markersize is under the key 'lines.markersize'. This value is 6.0 by default and whatever is passed to it is equal to the square root of the value passed to s= in plt.scatter.

import seaborn as sns
import matplotlib as mpl
mpl.rcParams['lines.markersize'] = 10              # <---- set markersize here

df = sns.load_dataset('tips')
sns.scatterplot(x='tip', y='total_bill', data=df);

You could also set the markersize after the plot by setting its size in the ax.collections. Scatter plot markers are stored in Axes.collections, which is a list of scatter plots in that Axes. It's possible to set a uniform marker size there using the set_sizes() method.

# for a single scatter plot
ax = sns.scatterplot(x='tip', y='total_bill', data=df);
ax.collections[0].set_sizes([100])                 # <---- reset markersize here

result

ax = sns.scatterplot(x='tip', y='total_bill', data=df, label='tip');
sns.scatterplot(x='size', y='total_bill', data=df, ax=ax, label='size');

# for multiple scatter plots, could use a loop
for scatters in ax.collections:
    scatters.set_sizes([100])

Upvotes: 3

Rian.Lopes
Rian.Lopes

Reputation: 71

About the update of the legend size, I got it by the attribute 'markerscale' from matplotlib.pyplot.legend

markerscalefloat, default: rcParams["legend.markerscale"] (default: 1.0) The relative size of legend markers compared with the originally drawn ones.

plt.legend(markerscale=2)

Upvotes: 7

Xiaoyu Lu
Xiaoyu Lu

Reputation: 3570

You can do so by giving a value to the s argument to change the marker size.

Example:

ax = sns.scatterplot(x="Data Set Description", y="R Squared", data=mean_df, s=10)

Upvotes: 200

Related Questions