Python,Python Seaborn教程

 2023-11-19 阅读 29 评论 0

摘要:Python Seaborn教程 (Python Seaborn Tutorial) Seaborn is a library for making statistical infographics in Python. It is built on top of matplotlib and also supports numpy and pandas data structures. It also supports statistical units from SciPy. Seaborn是

Python Seaborn教程 (Python Seaborn Tutorial)

Seaborn is a library for making statistical infographics in Python. It is built on top of matplotlib and also supports numpy and pandas data structures. It also supports statistical units from SciPy.

Seaborn是一個使用Python制作統計信息圖表的庫。 它基于matplotlib構建,還支持numpy和pandas數據結構。 它還支持SciPy的統計單位。

Python、Visualization plays an important role when we try to explore and understand data, Seaborn is aimed to make it easier and the centre of the process. To put in perspective, if we say matplotlib makes things easier and hard things possible, seaborn tries to make that hard easy too, that too in a well-defined way. But seaborn is not an alternative to matplotlib, think of it as a complement to the previous.

當我們嘗試探索和理解數據時,可視化起著重要作用,Seaborn的目標是使其變得更容易并且成為過程的中心。 放眼來看,如果我們說matplotlib使事情變得容易和困難的事情成為可能,seaborn也會嘗試以明確的方式使困難變得容易。 但是seaborn并不是matplotlib的替代品,可以認為它是對前者的補充。

As it is built on top of matplotlib, we will often invoke matplotlib functions directly for simple plots at matplotlib has already created highly efficient programs for it.

boost python、 由于它是基于matplotlib構建的,因此我們經常會直接調用matplotlib函數以獲取簡單的圖,因為matplotlib已經為其創建了高效程序。

The high-level interface of seaborn and customizability and variety of backends for matplotlib combined together makes it easy to generate publication-quality figures.

python seaborn tutorial

完美的,可定制的高級界面以及各種matplotlib后端組合在一起,使生成出版物質量數據變得容易。

為什么選擇Seaborn? (Why Seaborn?)

python3基礎教程、Seaborn offers a variety of functionality which makes it useful and easier than other frameworks. Some of these functionalities are:

Seaborn提供了多種功能,使其比其他框架更有用和更容易。 其中一些功能是:

  • A function to plot statistical time series data with flexible estimation and representation of uncertainty around the estimate

    繪制具有靈活估計的統計時間序列數據并在估計周圍表示不確定性的功能
  • Functions for visualizing univariate and bivariate distributions or for comparing them between subsets of data

    用于可視化單變量和雙變量分布或在數據子集之間進行比較的函數
  • Functions that visualize matrices of data and use clustering algorithms to discover structure in those matrices

    可視化數據矩陣并使用聚類算法發現這些矩陣中的結構的函數
  • High-level abstractions for structuring grids of plots that let you easily build complex visualizations

    用于構建圖網格的高級抽象,可讓您輕松構建復雜的可視化文件
  • Several built-in themes for styling matplotlib graphics

    Matplotlib圖形樣式的幾個內置主題
  • Tools for choosing color palettes to make beautiful plots that reveal patterns in your data

    用于選擇調色板的工具,以繪制精美的圖表以顯示數據中的圖案
  • Tools that fit and visualize linear regression models for different kinds of independent and dependent variables

    擬合和可視化線性回歸模型的工具,用于不同種類的自變量和因變量

Seaborn入門 (Getting Started with Seaborn)

To get started with Seaborn, we will install it on our machines.

python的安裝教程、 要開始使用Seaborn,我們將其安裝在我們的計算機上。

安裝Seaborn (Install Seaborn)

Seaborn assumes you have a running Python 2.7 or above platform with NumPY (1.8.2 and above), SciPy(0.13.3 and above) and pandas packages on the devices.

Seaborn假定您在設備上運行的Python 2.7或更高版本平臺具有NumPY(1.8.2和更高版本),SciPy(0.13.3和更高版本)和pandas軟件包。

編程python教程、Once we have these python packages installed we can proceed with the installation. For pip installation, run the following command in the terminal:

一旦安裝了這些python軟件包,我們就可以繼續安裝。 對于pip安裝,請在終端中運行以下命令:

pip install seaborn

If you like conda, you can also use conda for package installation, run the following command:

python基礎教程廖雪, 如果您喜歡conda,也可以使用conda進行軟件包安裝,請運行以下命令:

conda install seaborn

Alternatively, you can use pip to install the development version directly from GitHub:

另外,您可以使用pip直接從GitHub安裝開發版本:

pip install git+https://github.com/mwaskom/seaborn.git

使用Seaborn (Using Seaborn)

python3.7.0安裝教程?Once you are done with the installation, you can use seaborn easily in your Python code by importing it:

安裝完成后,可以通過導入在Python代碼中輕松使用seaborn:

import seaborn

控制人物美學 (Controlling figure aesthetics)

When it comes to visualization drawing attractive figures is important.

在可視化方面,吸引人的圖形很重要。

Matplotlib is highly customizable, but it can be complicated at the same time as it is hard to know what settings to tweak to achieve a good looking plot. Seaborn comes with a number of themes and a high-level interface for controlling the look of matplotlib figures. Let’s see it working:

Matplotlib是高度可定制的,但同時又可能會很復雜,因為很難知道要調整哪些設置才能獲得美觀的圖形。 Seaborn具有許多主題和用于控制matplotlib圖形外觀的高級界面。 讓我們看看它的工作原理:

#matplotlib inline
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as snsnp.random.seed(sum(map(ord, "aesthetics")))#Define a simple plot function, to plot offset sine waves
def sinplot(flip=1):x = np.linspace(0, 14, 100)for i in range(1, 7):plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
sinplot()

This is what the plot looks like with matplotlib defaults:

seaborn plot defaults

這是使用matplotlib默認值時的圖:

If you want to switch to seaborn defaults, simply call ‘set’ function:

如果要切換到默認值,只需調用“設置”功能:

sns.set()
sinplot()

This is how the plot look now:

seaborn plotting example

現在是這樣的樣子:

Seaborn圖樣式 (Seaborn figure styles)

Seaborn provides five preset themes: white grid, dark grid, white, dark, and ticks, each suited to different applications and also personal preferences.

Seaborn提供了五個預設主題:白色網格,深色網格,白色,深色和刻度,每個主題都適合不同的應用程序以及個人喜好。

Darkgrid is the default one. The White grid theme is similar but better suited to plots with heavy data elements, to switch to white grid:

Darkgrid是默認選項。 白色網格主題類似,但更適合于具有大量數據元素的圖,以切換到白色網格:

sns.set_style("whitegrid")
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2
sns.boxplot(data=data)

The output will be:

seaborn white grid plot

輸出將是:

For many plots, the grid is less necessary. Remove it by adding this code snippet:

對于許多地塊來說,網格是不必要的。 通過添加以下代碼片段將其刪除:

sns.set_style("dark")
sinplot()

The plot looks like:

seaborn dark plot

情節看起來像:

Or try the white background:

或嘗試白色背景:

sns.set_style("white")
sinplot()

This time, the background looks like:

seaborn plot white

這次,背景看起來像:

Sometimes you might want to give a little extra structure to the plots, which is where ticks come in handy:

有時,您可能想給繪圖添加一些額外的結構,這是刻度線派上用場的地方:

sns.set_style("ticks")
sinplot()

The plot looks like:

seaborn tutorial white ticks

情節看起來像:

去除軸刺 (Removing axes spines)

You can call despine function to remove them:

您可以調用despine函數將其刪除:

sinplot()
sns.despine()

The plot looks like:

seaborn plot without axes spines

情節看起來像:

Some plots benefit from offsetting the spines away from the data. When the ticks don’t cover the whole range of the axis, the trim parameter will limit the range of the surviving spines:

有些圖可以從數據中抵消尖峰。 當刻度不覆蓋軸的整個范圍時,trim參數將限制尚存的刺的范圍:

f, ax = plt.subplots()
sns.violinplot(data=data)
sns.despine(offset=10, trim=True)

The plot looks like:

seaborn plot offspring axes

情節看起來像:

You can also control which spines are removed with additional arguments to despine:

您還可以使用despine的附加參數來控制刪除哪些刺:

sns.set_style("whitegrid")
sns.boxplot(data=data, palette="deep")
sns.despine(left=True)

The plot looks like:

情節看起來像:

臨時設置圖形樣式 (Temporarily setting figure style)

axes_style() comes to help when you need to set figure style, temporarily:

當您需要臨時設置圖形樣式時, axes_style()會為您提供幫助:

with sns.axes_style("darkgrid"):plt.subplot(211)sinplot()
plt.subplot(212)
sinplot(-1)

The plot looks like:

seaborn style temporary settings

情節看起來像:

海洋風格的主要元素 (Overriding elements of the seaborn styles)

A dictionary of parameters can be passed to the rc argument of axes_style() and set_style() in order to customize figures.

可以將參數字典傳遞給axes_style()set_style()rc參數,以便自定義圖形。

Note: Only the parameters that are part of the style definition through this method can be overridden. For other purposes, you should use set() as it takes all the parameters.

注意:僅覆蓋通過此方法作為樣式定義一部分的參數。 出于其他目的,應使用set()因為它需要所有參數。

In case you want to see what parameters are included, just call the function without any arguments, an object is returned:

如果要查看包含哪些參數,只需調用不帶任何參數的函數,就會返回一個對象:

sns.axes_style(){'axes.axisbelow': True,
'axes.edgecolor': '.8',
'axes.facecolor': 'white',
'axes.grid': True,
'axes.labelcolor': '.15',
'axes.linewidth': 1.0,
'figure.facecolor': 'white',
'font.family': [u'sans-serif'],
'font.sans-serif': [u'Arial',u'DejaVu Sans',u'Liberation Sans',u'Bitstream Vera Sans',u'sans-serif'],
'grid.color': '.8',
'grid.linestyle': u'-',
'image.cmap': u'rocket',
'legend.frameon': False,
'legend.numpoints': 1,
'legend.scatterpoints': 1,
'lines.solid_capstyle': u'round',
'text.color': '.15',
'xtick.color': '.15',
'xtick.direction': u'out',
'xtick.major.size': 0.0,
'xtick.minor.size': 0.0,
'ytick.color': '.15',
'ytick.direction': u'out',
'ytick.major.size': 0.0,
'ytick.minor.size': 0.0}

You can then set different versions of these parameters:

然后,您可以設置這些參數的不同版本:

sns.set_style("darkgrid", {"axes.facecolor": ".9"})
sinplot()

The plot looks like:

seaborn example setting style

情節看起來像:

縮放圖元素 (Scaling plot elements)

Let’s try to manipulate scale of the plot. We can reset the default parameters by calling set():

讓我們嘗試操縱繪圖的比例。 我們可以通過調用set()來重置默認參數:

sns.set()

The four preset contexts are – paper, notebook, talk and poster. The notebook style is the default, and was used in the plots above:

四個預設上下文是–紙,筆記本,談話和海報。 筆記本樣式是默認樣式,并在上面的圖中使用:

sns.set_context("paper")
sinplot()

The plot looks like:

seaborn paper style

情節看起來像:

sns.set_context("talk")
sinplot()

The plot looks like:

seaborn talk style

情節看起來像:

結論 (Conclusion)

In this lesson, we have seen that Seaborn makes it easy to manipulate different graph plots. We have seen examples of scaling and changing context.

在本課程中,我們看到了Seaborn可以輕松操縱不同的圖形。 我們已經看到了擴展和更改上下文的示例。

Seaborn makes it easy to visualize data in an attractive manner and make it easier to read and understand.

通過Seaborn,可以輕松以有吸引力的方式可視化數據,并使其更易于閱讀和理解。

翻譯自: https://www.journaldev.com/18583/python-seaborn-tutorial

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/182976.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息