Python3 - seaborn: lmplot(), hue, scattr_kws{}, aspect, height

本博客是在Jupyter Notebooks上测试能通过,未在IDE上测试过。
如果想了解如何创建Jupyter, 请点击这里

先提供这次使用的dataset:

import seaborn as sns
tips = sns.load_dataset('tips')
tips.head()

结果如下:
在这里插入图片描述
使用lmplot():

# seaborn.lmplot(): Plot data and regression model fits across a FacetGrid
sns.lmplot(x='total_bill', y='tip', data=tips)

结果如下:
在这里插入图片描述
使用hue来分类:

# Variables that define subsets of the data, which will be drawn on separate facets in the grid. 
# hue: 用来分类
sns.lmplot(x='total_bill', y='tip', data=tips, hue='sex')

结果如下:
在这里插入图片描述

# Variables that define subsets of the data, which will be drawn on separate facets in the grid. 
# hue: 用来分类
sns.lmplot(x='total_bill', y='tip', data=tips, hue='smoker')

结果如下:
在这里插入图片描述

# Variables that define subsets of the data, which will be drawn on separate facets in the grid. 
# hue: 用来分类
sns.lmplot(x='total_bill', y='tip', data=tips, hue='time')

结果如下:
在这里插入图片描述
使用markers:
要想使用各种各样的markers, 请点击这里
markers库的网址:https://matplotlib.org/3.3.3/api/markers_api.html

# Markers for the scatterplot. If a list, each marker in the list will be used for each level of the hue variable.
sns.lmplot(x='total_bill', y='tip', data=tips, hue='sex', markers=['o','v'])

结果如下:
在这里插入图片描述
博主也设计一些测试案例来测试marker, 仅供参考:

# marker = ['s','X'] 可以用
# marker = [1,2] 也可以用这个
# marker = [3,4] 可以用
# marker = [5,6] 可以用
# marker = [5,'o'] 可以用
## marker = [+,-] 不能用 ##
# marker = ['.',','] 可以用
marker = ['+','x']
sns.lmplot(x='total_bill', y='tip', data=tips, hue='sex', markers=marker)

结果如下:
在这里插入图片描述
使用scatter_kws{}:

# scatter_kws{}: Additional keyword arguments to pass to plt.scatter and plt.plot.
# s for size
sns.lmplot(x='total_bill', y='tip', data=tips, hue='sex', markers=['o','v'], scatter_kws={'s':100})

结果如下:
在这里插入图片描述
如果要想改变scatter_kws字典里的内容,可以尝试这样:(仅供参考)

import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns
 
# Create data frame. The last column is the needed color
df = pd.DataFrame(np.random.random((100,2)), columns=["x","y"])
 
# Add a column: the color depends of x and y values, but you can use whatever function.
value=(df['x']>0.2) & (df['y']>0.4)
df['color']= np.where( value==True , "#9b59b6", "#3498db")
 
# plot
sns.regplot(data=df, x="x", y="y", fit_reg=False, scatter_kws={'facecolors':df['color']})

结果如下:
在这里插入图片描述
使用col:

# col: 根据所指定属性在列上分类
sns.lmplot(x='total_bill', y='tip', data=tips, col='sex')

结果如下:
在这里插入图片描述
使用row:

# row: 根据所指定属性在行上分类
sns.lmplot(x='total_bill', y='tip', data=tips, col='sex', row='time')

结果如下:
在这里插入图片描述
使用col和row:

sns.lmplot(x='total_bill', y='tip', data=tips, col='day', row='time', hue='sex')

结果如下:
在这里插入图片描述
使用aspect 和 height来控制图形的清晰度:

这个案例的aspect * height = 4.8

# aspect: Aspect ratio of each facet, so that aspect * height gives the width of each facet in inches
# height: Height (in inches) of each facet.
# aspect * height 所得到的结果越大,图形越模糊
sns.lmplot(x='total_bill', y='tip', data=tips, col='day', hue='sex', aspect=0.6, height=8)

结果如下:
在这里插入图片描述
这个案例的aspect * height = 3.2:

sns.lmplot(x='total_bill', y='tip', data=tips, col='day', hue='sex', aspect=0.8, height=4)

结果如下:
在这里插入图片描述
这个案例使用aspect * height = 7:

sns.lmplot(x='total_bill', y='tip', data=tips, col='day', hue='sex', aspect=0.7, height=10)

结果如下:
在这里插入图片描述
如果觉得不错,就点赞或者关注或者留言~~
谢谢~ ~

  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值