Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scatter plot with variable marker size (seaborn)

I am using a seaborn pairplot to plot a scatter plot of different dimensions of my datapoints. However, I want the markers of the datapoints to have a size that corresponds to one of the dimensions of the datapoints. I have the following code:

markersize = 1000* my_dataframe['dim_size'] / sum(my_dataframe['dim_size'])

sns.set_context("notebook", font_scale=1.5, rc={'figure.figsize': [11, 8]})
sns.set_style("darkgrid", {"axes.facecolor": ".9"})

kws = dict(s=markersize, linewidth=.5, edgecolor="w")

sbax = sns.pairplot(my_dataframe, hue='dim_hue' x_vars=['dim_1', 'dim_2'], y_vars=['dim_3', 'dim_4'], size=5, plot_kws=kws)

axes = sbax.axes
for a in axes.flatten():
    a.set_ylim([0,1])
    a.set_xlim([0,1])

If I do print(kws), I see in the dictionary that the sizes are all different and vary from 40 to 2000. However, the markers on the plot are all the same. Is there any way to achieve what I want?

Btw, this works very well with lmplot if I set the parameter scatter_kws={"s": markersize}.

Thanks!

like image 698
aliva7 Avatar asked Feb 07 '23 16:02

aliva7


1 Answers

import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")

size = 100 * (iris.petal_length / iris.petal_length.max())
g = sns.PairGrid(iris, vars=["sepal_length", "sepal_width"], size=5)
g.map(plt.scatter, s=size)

enter image description here

like image 198
mwaskom Avatar answered Feb 13 '23 03:02

mwaskom