python seaborn 入门

来源:互联网 发布:淘宝api 获取订单信息 编辑:程序博客网 时间:2024/06/05 12:06

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 pltimport pandas an pd#initialize figure and axes objectfig,ax = plt.subplots# Load in datatips = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv")# Create violinplotax.violinplot(tips["total_bill"], vert=False)# Show the plotplt.show()
# Import the necessary librariesimport matplotlib.pyplot as pltimport seaborn as sns# Load the datatips = sns.load_dataset("tips")# Create violinplotsns.violinplot(x = "total_bill", data=tips)# Show the plotplt.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 librariesimport seaborn as snsimport matplotlib.pyplot as plt# Load iris datairis = sns.load_dataset("iris")# Construct iris plotsns.swarmplot(x="species", y="petal_length", data=iris)# Show plotplt.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 pltimport seaborn as snstitanic = sns.load_dataset("titanic")# Set up a factorplotg = 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 stylesplt.style.available# Use Matplotlib defaultsplt.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 librariesimport seaborn as snsimport matplotlib.pyplot as pltimport numpy as npfrom matplotlib.colors import ListedColormap# Define a variable NN = 500# Construct the colormapcurrent_palette = sns.color_palette("muted", n_colors=5)cmap = ListedColormap(sns.color_palette(current_palette).as_hex())# Initialize the datadata1 = np.random.randn(N)data2 = np.random.randn(N)# Assume that there are 5 possible labelscolors = np.random.randint(0,5,N)# Create a scatter plotplt.scatter(data1, data2, c=colors, cmap=cmap)# Add a color barplt.colorbar()# Show the plotplt.show()

6.How To Scale Seaborn Plots For Other Contexts

make use of set_context() to control the plot elements

# Import necessary librariesimport matplotlib.pyplot as pltimport seaborn as sns# Reset default params 充值默认参数sns.set()# Set context to `"paper"`sns.set_context("paper")# Load iris datairis = sns.load_dataset("iris")# Construct iris plotsns.swarmplot(x="species", y="petal_length", data=iris)# Show plotplt.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 librariesimport matplotlib.pyplot as pltimport seaborn as sns# Set context to `"paper"`sns.set_context("paper", font_scale=3, rc={"font.size":8,"axes.labelsize":5})# Load iris datairis = sns.load_dataset("iris")# Construct iris plotsns.swarmplot(x="species", y="petal_length", data=iris)# Show plotplt.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 librariesimport matplotlib.pyplot as pltimport seaborn as sns# Reset default paramssns.set(rc={"font.size":8,"axes.labelsize":5})# Load iris datairis = sns.load_dataset("iris")# Construct iris plotsns.swarmplot(x="species", y="petal_length", data=iris)# Show plotplt.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 librariesimport matplotlib.pyplot as pltimport seaborn as sns# Load datairis = sns.load_dataset("iris")tips = sns.load_dataset("tips")# Set axes style to white for first subplotwith sns.axes_style("white"):    plt.subplot(211)    sns.swarmplot(x="species", y="petal_length", data=iris)# Initialize second subplotplt.subplot(212)# Plot violinplotsns.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 librariesimport seaborn as snsimport matplotlib.pyplot as plt# Initialize Figure and Axes object**fig, ax = plt.subplots(figsize=(10,4))**# Load in the datairis = sns.load_dataset("iris")# Create swarmplotsns.swarmplot(x="species", y="petal_length", data=iris, ax=ax)# Show plotplt.show()

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

# Import the librariesimport matplotlib.pyplot as pltimport seaborn as sns # Load datatitanic = sns.load_dataset("titanic")#set up a factorplotg = sns.factorplot("class", "survived", "sex", data=titanic, kind="bar", **size=6, aspect=2**, palette="muted", legend=False)# Show plotplt.show()

9.How To Rotate Label Text in Seaborn

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

# Import the necessary librariesimport matplotlib.pyplot as pltimport seaborn as sns import numpy as npimport pandas as pd# Initialize the datax = 10 ** np.arange(1, 10)y = x * 2data = pd.DataFrame(data={'x': x, 'y': y})# Create an lmplotgrid = 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 plotplt.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 librariesimport seaborn as snsimport matplotlib.pyplot as plt# Load the datatips = sns.load_dataset("tips")# Create the boxplotax = sns.boxplot(x="total_bill", data=tips)# Set the `xlim`**ax.set(xlim=(0, 100))**# Show the plotplt.show()

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

# Import the necessary librariesimport matplotlib.pyplot as pltimport seaborn as sns import numpy as npimport pandas as pd# Initialize the datax = 10 ** np.arange(1, 10)y = x * 2data = pd.DataFrame(data={'x': x, 'y': y})# Create lmplotlm = sns.lmplot('x', 'y', data, size=7, truncate=True, scatter_kws={"s": 100})# Get hold of the `Axes` objectsaxes = lm.ax# Tweak the `Axes` properties**axes.set_ylim(-1000000000,)**# Show the plotplt.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 librariesimport matplotlib.pyplot as pltimport seaborn as sns import numpy as npimport pandas as pd# Create the datax = 10 ** np.arange(1, 10)y = x * 2data = 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 regplotsns.regplot("x", "y", data, ax=ax, scatter_kws={"s": 100})# Show plotplt.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 librariesimport matplotlib.pyplot as pltimport seaborn as sns # Load datatitanic = sns.load_dataset("titanic")# Set up a factorplotg = sns.factorplot("class", "survived", "sex", data=titanic, kind="bar", size=6, palette="muted", legend=False)# Set the `yscale`**g.set(yscale="log")**# Show plotplt.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 librariesimport matplotlib.pyplot as pltimport seaborn as sns tips = sns.load_dataset("tips")# Create the boxplotax = sns.boxplot(x="total_bill", data=tips)# Set title**ax.set_title("boxplot")**# Show the plotplt.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 librariesimport matplotlib.pyplot as pltimport seaborn as sns import numpy as npimport pandas as pd# Load the datatips = sns.load_dataset("tips")# Create scatter plotsg = 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 plotplt.show()
原创粉丝点击