简体   繁体   English

如何更改 regplot() 的点大小,seaborn 的散点图函数 (python)

[英]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:为此,您可以像这样为regplot()函数提供scatter_kws参数:

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.此代码使用与 OP 相同的数据,但将其包装在 DataFrame 中(因为 seaborn 就是为此而设计的)并添加了第三维 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.我想补充 mburke05 的答案,似乎可以将类似数组的数据传递到scatter_kws. For example, if you wanted the size attribute in the tips dataset to determine the size of a point you can write:例如,如果您希望tips 数据集中的size属性来确定一个点的大小,您可以这样写:

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 .您不能像设置xy那样简单地使用列名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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