简体   繁体   中英

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

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 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.

在此处输入图片说明

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM