python seaborn 入门

This Seaborn tutorial introduces you to the basics of statistical data visualization

Seaborn: Python’s Statistical Data Visualization Library

1.Seaborn vs Matplotlib?
seaborn:
-complimentary to Matplotlib
-specifically targets statistical data visualization
-tries to make a well-defined set of hard things easy .

Compare the following plots:

import matplotlib.pyplot as plt
import pandas an pd
#initialize figure and axes object
fig,ax = plt.subplots
# Load in data
tips = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv")
# Create violinplot
ax.violinplot(tips["total_bill"], vert=False)
# Show the plot
plt.show()
# Import the necessary libraries
import matplotlib.pyplot as plt
import seaborn as sns
# Load the data
tips = sns.load_dataset("tips")
# Create violinplot
sns.violinplot(x = "total_bill", data=tips)
# Show the plot
plt.show()

The Matplotlib defaults that usually don’t speak to users are the colors, the tick marks on the upper and right axes, the style,…
-working with DataFrames doesn’t go quite as smoothly with Matplotlib.
-can be annoying if you’re doing exploratory analysis with Pandas

Seaborn addresses:
plotting functions operate on DataFrames and arrays that contain a whole dataset.

How To Load Data To Construct Seaborn Plots

1.Loading A Built-in Seaborn Data Set 内置数据
make use of the load_dataset() function.

# Import necessary libraries
import seaborn as sns
import matplotlib.pyplot as plt
# Load iris data
iris = sns.load_dataset("iris")
# Construct iris plot
sns.swarmplot(x="species", y="petal_length", data=iris)
# Show plot
plt.show()

2.Loading Your Pandas DataFrame Getting Your Data

-Seaborn works best with Pandas DataFrames and arrays that contain a whole data set.
labels from DataFrames are automatically propagated to plots 自动将列表的标签设置为作图标签

3.How To Show Seaborn Plots

-use plt.show() to make the image appear to you

import matplotlib.pyplot as plt
import seaborn as sns
titanic = sns.load_dataset("titanic")
# Set up a factorplot
g = sns.factorplot("class", "survived", "sex", data=titanic, kind="bar", palette="muted", legend=False)
plt.show()
  • A factorplot is a categorical plot 分类图表
    -which in this case is a bar plot. That’s because you have set the kind argument to “bar”. Also, you set which colors should be displayed with the palette argument and that you set the legend to False.

4.How To Use Seaborn With Matplotlib Defaults

#

call set() or set_style(), or set_context() or set_palette()
to get either Seaborn or Matplotlib defaults for plotting.

import matplotlib.pyplot as plt

# Check the available styles
plt.style.available

# Use Matplotlib defaults
plt.style.use("classic")

5.How To Use Seaborn’s Colors As A colormap in Matplotlib?

how to bring in Seaborn colors into Matplotlib plots?
answer : make use of color_palette() to define a color map
the number of colors with the argument n_colors.

example : assume that there are 5 labels assigned to the data points that are defined in data1 and data2, so that’s why you pass 5 to this argument and you also make a list with length equal to N where 5 integers vary in the variable colors.

# Import the necessary libraries
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap

# Define a variable N
N = 500

# Construct the colormap
current_palette = sns.color_palette("muted", n_colors=5)
cmap = ListedColormap(sns.color_palette(current_palette).as_hex())

# Initialize the data
data1 = np.random.randn(N)
data2 = np.random.randn(N)
# Assume that there are 5 possible labels
colors = np.random.randint(0,5,N)

# Create a scatter plot
plt.scatter(data1, data2, c=colors, cmap=cmap)

# Add a color bar
plt.colorbar()

# Show the plot
plt.show()

6.How To Scale Seaborn Plots For Other Contexts

make use of set_context() to control the plot elements

# Import necessary libraries
import matplotlib.pyplot as plt
import seaborn as sns
# Reset default params 充值默认参数
sns.set()
# Set context to `"paper"`
sns.set_context("paper")
# Load iris data
iris = sns.load_dataset("iris")
# Construct iris plot
sns.swarmplot(x="species", y="petal_length", data=iris)
# Show plot
plt.show()

The four predefined contexts are “paper”, “notebook”, “talk” and “poster”
画出图的大小排序:paper < notebook < talk < poster

passing more arguments to set_context() to scale more plot elements,
such as font_scale or
more parameter mappings that can override the values that are preset in the Seaborn context dictionaries.
font.size and axes.labelsize:

# Import necessary libraries
import matplotlib.pyplot as plt
import seaborn as sns

# Set context to `"paper"`
sns.set_context("paper", font_scale=3, rc={"font.size":8,"axes.labelsize":5})

# Load iris data
iris = sns.load_dataset("iris")

# Construct iris plot
sns.swarmplot(x="species", y="petal_length", data=iris)

# Show plot
plt.show()

Additionally, it’s good to keep in mind that you can use the higher-level set() function instead of set_context() to adjust other plot elements:

# Import necessary libraries
import matplotlib.pyplot as plt
import seaborn as sns

# Reset default params
sns.set(rc={"font.size":8,"axes.labelsize":5})

# Load iris data
iris = sns.load_dataset("iris")

# Construct iris plot
sns.swarmplot(x="species", y="petal_length", data=iris)

# Show plot
plt.show()

As for Seaborn, two types of functions: axes-level functions(轴水平功能 and figure-level functions(图像功能).
operate on the Axes level are, for example, regplot(), boxplot(), kdeplot(), operate on the Figure level are lmplot(), factorplot(), jointplot() ,

first group:
by taking an explicit ax argument and returning an Axes object
second group:
create plots that potentially include Axes which are always organized in a“meaningful” way.

example between a boxplot and an lmplot :

sns.boxplot(x="total_bill", data=tips)
sns.lmplot('x', 'y', data, size=7, truncate=True, scatter_kws={"s": 100})

7.How To Temporarily Set The Plot Style

use axes_style() in a with statement to temporarily set the plot style.
This, in addition to the use of plt.subplot(), will allow you to make figures that have differently-styled axes
example :

# Import necessary libraries
import matplotlib.pyplot as plt
import seaborn as sns

# Load data
iris = sns.load_dataset("iris")
tips = sns.load_dataset("tips")

# Set axes style to white for first subplot
with sns.axes_style("white"):
    plt.subplot(211)
    sns.swarmplot(x="species", y="petal_length", data=iris)

# Initialize second subplot
plt.subplot(212)

# Plot violinplot
sns.violinplot(x = "total_bill", data=tips)

# Show the plot                   
plt.show()

8.How To Set The Figure Size in Seaborn

For axes level functions:
make use of the plt.subplots() function to which you pass the figsize argument.

# Import necessary libraries
import seaborn as sns
import matplotlib.pyplot as plt

# Initialize Figure and Axes object
**fig, ax = plt.subplots(figsize=(10,4))**

# Load in the data
iris = sns.load_dataset("iris")

# Create swarmplot
sns.swarmplot(x="species", y="petal_length", data=iris, ax=ax)

# Show plot
plt.show()

For figure-level functions:
rely on two parameters to set the Figure size, namely, size and aspect

# Import the libraries
import matplotlib.pyplot as plt
import seaborn as sns 
# Load data
titanic = sns.load_dataset("titanic")
#set up a factorplot
g = sns.factorplot("class", "survived", "sex", data=titanic, kind="bar", **size=6, aspect=2**, palette="muted", legend=False)
# Show plot
plt.show()

9.How To Rotate Label Text in Seaborn

-work on the Figure level
-set_xticklabels, to rotate the text label:

# Import the necessary libraries
import matplotlib.pyplot as plt
import seaborn as sns 
import numpy as np
import pandas as pd

# Initialize the data
x = 10 ** np.arange(1, 10)
y = x * 2
data = pd.DataFrame(data={'x': x, 'y': y})

# Create an lmplot
grid = sns.lmplot('x', 'y', data, size=7, truncate=True, scatter_kws={"s": 100})

# Rotate the labels on x-axis
**grid.set_xticklabels(rotation=30)**

# Show the plot
plt.show()

10.How To Set xlim or ylim in Seaborn

object at Axes level, you can make use of the set() function to set xlim, ylim
example:

# Import necessary libraries
import seaborn as sns
import matplotlib.pyplot as plt
# Load the data
tips = sns.load_dataset("tips")
# Create the boxplot
ax = sns.boxplot(x="total_bill", data=tips)
# Set the `xlim`
**ax.set(xlim=(0, 100))**
# Show the plot
plt.show()

note that can also use ax.set_xlim(10,100) to limit the x-axis.
For functions at Figure-level:

# Import the necessary libraries
import matplotlib.pyplot as plt
import seaborn as sns 
import numpy as np
import pandas as pd

# Initialize the data
x = 10 ** np.arange(1, 10)
y = x * 2
data = pd.DataFrame(data={'x': x, 'y': y})

# Create lmplot
lm = sns.lmplot('x', 'y', data, size=7, truncate=True, scatter_kws={"s": 100})

# Get hold of the `Axes` objects
axes = lm.ax

# Tweak the `Axes` properties
**axes.set_ylim(-1000000000,)**

# Show the plot
plt.show()

11.How To Set Log Scale

-use the logarithmic scale on one or both axes

For a simple regression with regplot(), you can set the scale with the help of the Axes object.

# Import the necessary libraries
import matplotlib.pyplot as plt
import seaborn as sns 
import numpy as np
import pandas as pd

# Create the data
x = 10 ** np.arange(1, 10)
y = x * 2
data = pd.DataFrame(data={'x': x, 'y': y})

# Initialize figure and ax  初始化
fig, ax = plt.subplots()

# Set the scale of the x-and y-axes
**ax.set(xscale="log", yscale="log")**

# Create a regplot
sns.regplot("x", "y", data, ax=ax, scatter_kws={"s": 100})

# Show plot
plt.show()

For Figure level functions,you can set the xscale and yscale properties with the help of the set() method of the FacetGrid object:

# Import the libraries
import matplotlib.pyplot as plt
import seaborn as sns 
# Load data
titanic = sns.load_dataset("titanic")
# Set up a factorplot
g = sns.factorplot("class", "survived", "sex", data=titanic, kind="bar", size=6, palette="muted", legend=False)
# Set the `yscale`
**g.set(yscale="log")**
# Show plot
plt.show()

12.How To Add A Title

For Axes-level functions:
you’ll adjust the title on the Axes level itself with the help of set_title()

# Import the libraries
import matplotlib.pyplot as plt
import seaborn as sns 

tips = sns.load_dataset("tips")

# Create the boxplot
ax = sns.boxplot(x="total_bill", data=tips)

# Set title
**ax.set_title("boxplot")**

# Show the plot
plt.show()

For Figure-level functions:
you can go via fig, just like in the factorplot that you have made in one of the previous sections, or you can also work via the Axes

# Import the necessary libraries
import matplotlib.pyplot as plt
import seaborn as sns 
import numpy as np
import pandas as pd

# Load the data
tips = sns.load_dataset("tips")

# Create scatter plots
g = sns.FacetGrid(tips, col="sex", row="smoker", margin_titles=True)
g.map(sns.plt.scatter, "total_bill", "tip")

# Add a title to the figure
**g.fig.suptitle("this is a title")**

# Show the plot
plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值