本博客是在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)
结果如下:
如果觉得不错,就点赞或者关注或者留言~~
谢谢~ ~