Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()

博主都是在Jupyter Notebooks上进行练习的,如有想要使用Jupyter Notebooks, 请点击这里

直接上代码:

import seaborn as sns

tips = sns.load_dataset('tips')
print(tips.head())

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()
使用distplot():

print(sns.distplot(tips['total_bill']))

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()

# kde: Whether to plot a gaussian kernel density estimate
# bins: Specification of hist bins. If unspecified, as reference rule is used that tries to find a useful default
print(sns.distplot(tips['total_bill'],kde=False, bins=30)) 

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()

print(sns.distplot(tips['total_bill'],hist=False)) # hist: Whether to plot a (normed) histogram

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()
使用jointplot()

# Draw a plot of two variables with bivariate and univariate graphs
# x,y: variables that specify positions on the x and y axes
# data: Input data structure. Either a long-form collection of vectors that can be assigned to named variables or a wide-form dataset that will be internally reshaped.
# kind: Kind of plot to draw. 有几种可供选择:"scatter","kde","hist","hex","reg","resid"
print(sns.jointplot(x='total_bill', y='tip', data=tips, kind='hex'))

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()

print(sns.jointplot(x='total_bill', y='tip', data=tips))

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()

print(sns.jointplot(x='total_bill', y='tip', data=tips, kind='reg'))

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()

print(sns.jointplot(x='total_bill', y='tip', data=tips, kind='kde'))

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()
使用pairplot():

# Plot pairwise relationships in a dataset
# By default, this function will create a grid of Axes such that each numeric variable in data will by shared across the y-axes across a single row and the x-axes across a single column. 
#     The diagonal plots are treated differently: a univariate distribution plot is drawn to show the marginal distribution of the data in each column.
# data: Tidy (long-form) dataframe where each column is a variable and each row is an observation
print(sns.pairplot(tips))

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()

# hue: name of variable in data (tips)
# Variable in data to map plot aspects to different colors
print(sns.pairplot(tips, hue='sex'))

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()

# palette: dict or seaborn color palette
# Set of colors for mapping the hue variable. If a dict, keys should be values in the hue variable
print(sns.pairplot(tips, hue='sex', palette='coolwarm'))

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()
使用rugplot():

# Plot marginal distributions by drawing ticks along the x and y axes
# This function is intended to complement other plots by showing the location of individual observations in an unobstrusive way.
print(sns.rugplot(tips['total_bill']))

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

#Create dataset
dataset = np.random.randn(25)

# Create another rugplot
sns.rugplot(dataset);

# Set up the x-axis for the plot
x_min = dataset.min() - 2
x_max = dataset.max() + 2

# 100 equally spaced points from x_min to x_max
# Return evenly spaced numbers over a specified interval.
# Returns num evenly spaced samples, calculated over the interval [start, stop].
x_axis = np.linspace(x_min,x_max,100)

bandwidth = ((4*dataset.std()**5)/(3*len(dataset)))**.2

# Create an empty kernel list
kernel_list = []

# Plot each basis function
for data_point in dataset:
    
    # Create a kernel for each point and append to list
    kernel = stats.norm(data_point,bandwidth).pdf(x_axis)
    kernel_list.append(kernel)
    
    #Scale for plotting
    kernel = kernel / kernel.max()
    kernel = kernel * .4
    plt.plot(x_axis,kernel,color = 'grey',alpha=0.5)

plt.ylim(0,1)

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()

# To get the kde plot that can sum these basis functions.

# Plot the sum of the basis function
sum_of_kde = np.sum(kernel_list,axis=0)

# Plot figure
fig = plt.plot(x_axis,sum_of_kde,color='indianred')

# Add the initial rugplot
sns.rugplot(dataset,c = 'indianred')

# Get rid of y-tick marks
plt.yticks([])

# Set title
plt.suptitle("Sum of the Basis Functions")

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()

使用scatterplot():

import seaborn as sns; 
tips = sns.load_dataset("tips")
print(sns.scatterplot(data=tips, x="total_bill", y="tip"))

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()
使用kdeplot()

# Plot univariate or bivariate distributions using kernel density estimation.
# A kernel density estimate (KDE) plot is a method for visualizing the distribution of observations in a dataset, analagous to a histogram. 
# KDE represents the data using a continuous probability density curve in one or more dimensions.
print(sns.kdeplot(tips['total_bill']))

结果如下:
Python3 - seaborn的使用:distplot(), jointplot(), pairplot(), rugplot(), scatterplot(), kdeplot()
如果觉得不错,就点赞或者关注或者留言~~
谢谢~ ~

上一篇:精要主义:如何应对拥挤不堪的工作与生活


下一篇:[Tips] 本地局域网内pc和nas的简单备份方式