Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the point size for regplot(), seaborn's scatter plot function (python)

Tags:

python

seaborn

I want to be able to set the point size when plotting like this:

sns.regplot(y=[1,3,4,2,5], x=[range(5)], data=df,
            marker='o', color='red')
plt.show()

Do you guys know how?

like image 984
AlexG Avatar asked Apr 28 '16 17:04

AlexG


People also ask

How do you change the size of a point in Seaborn?

To set the size of markers, we can use the s parameter. This parameter can be used since seaborn is built on the matplotlib module. We can specify this argument in the scatterplot() function and set it to some value. Alternatively, we can control the size of the points based on some variables.

How do I change the shape of scatter points in Seaborn?

In Seaborn's scatterplot() function, we can change the shape of markers by a variable using style argument. In this example, we have changed the marker's shape based on the value of the variable, “sex” in the dataframe. Notice that data points corresponding to males are different from females.


3 Answers

To do this you can feed the regplot() function the scatter_kws arg like so:

import seaborn as sns
tips = sns.load_dataset('tips')
sns.regplot(x='total_bill', y='tip', data=tips,
            marker='o', color='red', scatter_kws={'s':2})

small points

sns.regplot(x='total_bill', y='tip', data=tips,
            marker='o', color='red', scatter_kws={'s':20})

big points

like image 158
mburke05 Avatar answered Oct 03 '22 08:10

mburke05


You could even make the points dynamically sized to represent a third dimension. This code uses the same data as the OP, but wraps it in a DataFrame (as seaborn is designed for that) and also adds a third dimension, z.

import seaborn as sns
import pandas as pd

data = pd.DataFrame({
    'x': [x for x in range(5)],
    'y': [1, 3, 4, 2, 5],
    'z': [14, 14, 100, 16, 36]
})
sns.regplot(x='x', y='y', data=data, marker='o', color='red',
    scatter_kws={'s': data['z']})

You can probably imagine how you could also manipulate the list/array of sizes programatically, giving you a lot of power to convey extra information.

straightforward version

If you wanted to split these up into two groups, like in a FacetGrid, regplot doesn't handle that keyword mapping cleanly. But scatterplot does. Just plot regplot underneath for the fit line, then scatterplot over it for the points. Below, I've added a grp variable and plotted it onto a FacetGrid.

import seaborn as sns
import pandas as pd

data = pd.DataFrame({
    'x': [x for x in range(5)],
    'y': [1, 3, 4, 2, 5],
    'z': [14, 14, 100, 16, 36],
    'grp': ['a', 'a', 'b', 'b', 'a'],
})
g = sns.FacetGrid(data=data, col='grp', margin_titles=True)
g.map_dataframe(sns.regplot, 'x', 'y', color='red', scatter=False)
g.map_dataframe(sns.scatterplot, 'x', 'y', size='z', marker='o', color='red')

facetgrid version

like image 21
mightypile Avatar answered Oct 03 '22 08:10

mightypile


I would add to mburke05's answer that it appears possible to pass array-like data into scatter_kws. For example, if you wanted the size attribute in the tips dataset to determine the size of a point you can write:

sns.regplot(
    x="total_bill", y="tip", data=tips,
    marker='o', color='red', scatter_kws={'s':tips['size']})

However, you must explicitly lookup that attribute in the dataframe (as above); you cannot simply use the column name as you would when setting x and y.

like image 32
Dustin Michels Avatar answered Oct 03 '22 10:10

Dustin Michels