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?
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.
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.
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})
sns.regplot(x='total_bill', y='tip', data=tips,
marker='o', color='red', scatter_kws={'s':20})
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.
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')
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With