提交 a6452a85 编写于 作者: Z ZhanPw

Update 8.md

上级 e4818a1d
# Controlling figure aesthetics
Drawing attractive figures is important. When making figures for yourself, as you explore a dataset, it’s nice to have plots that are pleasant to look at. Visualizations are also central to communicating quantitative insights to an audience, and in that setting it’s even more necessary to have figures that catch the attention and draw a viewer in.
> 译者:[P3n9W31](https://github.com/P3n9W31)
Matplotlib is highly customizable, but it can be hard to know what settings to tweak to achieve an attractive plot. Seaborn comes with a number of customized themes and a high-level interface for controlling the look of matplotlib figures.
绘制有吸引力的图像很十分重要的。当你在探索一个数据集并为你自己做图的时候,制作一些让人看了心情愉悦的图像是很好的。可视化对向观众传达量化的简介也是很重要的,在这种情况下制作能够抓住查看者的注意力并牢牢吸引住他们的图像就更有必要了。
Matplotlib是高度可定制的,但是很难知道要如何设置图像才能使得图像更加吸引人。Seaborn提供了许多定制好的主题和高级的接口,用于控制Matplotlib所做图像的外观。
```py
import numpy as np
......@@ -11,7 +13,7 @@ import matplotlib.pyplot as plt
```
Let’s define a simple function to plot some offset sine waves, which will help us see the different stylistic parameters we can tweak.
让我们定义一个简单的函数来绘制一些偏移正弦波,这将帮助我们看到我们可以调整的能够影响图像风格的不同参数。
```py
def sinplot(flip=1):
......@@ -21,7 +23,7 @@ def sinplot(flip=1):
```
This is what the plot looks like with matplotlib defaults:
这是Matplotlib默认情况下的绘图外观:
```py
sinplot()
......@@ -30,7 +32,7 @@ sinplot()
![http://seaborn.pydata.org/_images/aesthetics_7_0.png](img/4784d932a8738cea5085be56ce6f7315.jpg)
To switch to seaborn defaults, simply call the [`set()`](../generated/seaborn.set.html#seaborn.set "seaborn.set") function.
为了将图像的风格转变为seaborn的默认样式,我们可以 [`set()`](../generated/seaborn.set.html#seaborn.set "seaborn.set") 函数。
```py
sns.set()
......@@ -40,15 +42,15 @@ sinplot()
![http://seaborn.pydata.org/_images/aesthetics_9_0.png](img/2422a0c0d2c96ec6397babaa6c842d79.jpg)
(Note that in versions of seaborn prior to 0.8, [`set()`](../generated/seaborn.set.html#seaborn.set "seaborn.set") was called on import. On later versions, it must be explicitly invoked).
(注意,在0.8之前的seaborn版本中, [`set()`](../generated/seaborn.set.html#seaborn.set "seaborn.set") 已经在使用impory语句导入的时候就被调用了。但在以后的版本中,必须要显式调用它)。
Seaborn splits matplotlib parameters into two independent groups. The first group sets the aesthetic style of the plot, and the second scales various elements of the figure so that it can be easily incorporated into different contexts.
Seaborn将matplotlib参数分成两个独立的组。第一组设置了图像的美术风格,第二组则对图像中不同的元素进行了控制,使得图像可以很容易地融入不同的环境中。
The interface for manipulating these parameters are two pairs of functions. To control the style, use the [`axes_style()`](../generated/seaborn.axes_style.html#seaborn.axes_style "seaborn.axes_style") and [`set_style()`](../generated/seaborn.set_style.html#seaborn.set_style "seaborn.set_style") functions. To scale the plot, use the [`plotting_context()`](../generated/seaborn.plotting_context.html#seaborn.plotting_context "seaborn.plotting_context") and [`set_context()`](../generated/seaborn.set_context.html#seaborn.set_context "seaborn.set_context") functions. In both cases, the first function returns a dictionary of parameters and the second sets the matplotlib defaults.
操作这些参数的接口是两对函数。要控制样式,请使用 [`axes_style()`](../generated/seaborn.axes_style.html#seaborn.axes_style "seaborn.axes_style")[`set_style()`](../generated/seaborn.set_style.html#seaborn.set_style "seaborn.set_style") 函数。要对图像中元素的样式进行修改,请使用 [`plotting_context()`](../generated/seaborn.plotting_context.html#seaborn.plotting_context "seaborn.plotting_context")[`set_context()`](../generated/seaborn.set_context.html#seaborn.set_context "seaborn.set_context") 函数。在这两种情况下(控制图像样式与修改元素样式),第一个函数会返回一个参数字典,第二个函数设置matplotlib中相关参数的默认值。
## Seaborn figure styles
## Seaborn图像参数
There are five preset seaborn themes: `darkgrid`, `whitegrid`, `dark`, `white`, and `ticks`. They are each suited to different applications and personal preferences. The default theme is `darkgrid`. As mentioned above, the grid helps the plot serve as a lookup table for quantitative information, and the white-on grey helps to keep the grid from competing with lines that represent data. The `whitegrid` theme is similar, but it is better suited to plots with heavy data elements:
有五个预设的Seaborn主题: `darkgrid``whitegrid``dark``white`以及 `ticks`。它们分别适用于不同的应用程序和个人偏好。默认主题为 `darkgrid`。如上所述,坐标方格有助于将制出的图像用作定量信息的查阅表,灰色背景上的白色有助于防止网格与表示数据的行发生竞争。 `whitegrid` 主题类似,但它更适用于包含大量数据元素的绘图:
```py
sns.set_style("whitegrid")
......@@ -59,7 +61,7 @@ sns.boxplot(data=data);
![http://seaborn.pydata.org/_images/aesthetics_11_0.png](img/ba3b8a1ba98a18cbe9f37192b2338932.jpg)
For many plots, (especially for settings like talks, where you primarily want to use figures to provide impressions of patterns in the data), the grid is less necessary.
对许多的图像而言,(尤其是在你只是想通过图像来提供给人们一个对数据模式的印象时,比如说作报告时)坐标网格都是不必要的。
```py
sns.set_style("dark")
......@@ -77,7 +79,7 @@ sinplot()
![http://seaborn.pydata.org/_images/aesthetics_14_0.png](img/47decb21ce3a9413876a02f2967ef7aa.jpg)
Sometimes you might want to give a little extra structure to the plots, which is where ticks come in handy:
有时,您可能希望为绘图提供一点额外的结构,这正是tick样式的用武之地:
```py
sns.set_style("ticks")
......@@ -87,9 +89,9 @@ sinplot()
![http://seaborn.pydata.org/_images/aesthetics_16_0.png](img/d23eefa56e1ef945c75b43ab4fc651a4.jpg)
## Removing axes spines
## 移除坐标轴
Both the `white` and `ticks` styles can benefit from removing the top and right axes spines, which are not needed. The seaborn function [`despine()`](../generated/seaborn.despine.html#seaborn.despine "seaborn.despine") can be called to remove them:
`white` 样式与 `ticks` 样式的好处是都能删除所不需要的顶部与右部坐标轴。使用seaborn中的函数 [`despine()`](../generated/seaborn.despine.html#seaborn.despine "seaborn.despine") 可以来移除它们:
```py
sinplot()
......@@ -99,7 +101,7 @@ sns.despine()
![http://seaborn.pydata.org/_images/aesthetics_18_0.png](img/b9a7a625477fd6d6c167844f557d14dd.jpg)
Some plots benefit from offsetting the spines away from the data, which can also be done when calling [`despine()`](../generated/seaborn.despine.html#seaborn.despine "seaborn.despine"). When the ticks don’t cover the whole range of the axis, the `trim` parameter will limit the range of the surviving spines.
有些图的好处在于,可以让坐标的主轴随着数据进行偏移,这可以使用 [`despine()`](../generated/seaborn.despine.html#seaborn.despine "seaborn.despine")函数来完成。当刻度无法覆盖轴的整个范围时,`trim`参数将限制不受影响的坐标轴的范围。
```py
f, ax = plt.subplots()
......@@ -110,7 +112,7 @@ sns.despine(offset=10, trim=True);
![http://seaborn.pydata.org/_images/aesthetics_20_0.png](img/7c21c8cb297826c3a45b483039151d9e.jpg)
You can also control which spines are removed with additional arguments to [`despine()`](../generated/seaborn.despine.html#seaborn.despine "seaborn.despine"):
你也可以通过控制 [`despine()`](../generated/seaborn.despine.html#seaborn.despine "seaborn.despine")的额外参数来删除坐标轴:
```py
sns.set_style("whitegrid")
......@@ -121,9 +123,9 @@ sns.despine(left=True)
![http://seaborn.pydata.org/_images/aesthetics_22_0.png](img/4f24815584c7c584945d529f3d9dbfbf.jpg)
## Temporarily setting figure style
## 设置临时图像格式
Although it’s easy to switch back and forth, you can also use the [`axes_style()`](../generated/seaborn.axes_style.html#seaborn.axes_style "seaborn.axes_style") function in a `with` statement to temporarily set plot parameters. This also allows you to make figures with differently-styled axes:
虽然来回切换很容易,但你也可以在`with`语句中使用 [`axes_style()`](../generated/seaborn.axes_style.html#seaborn.axes_style "seaborn.axes_style") 函数来临时设置绘图参数。 这也允许您使用不同风格的坐标轴制作图形:
```py
f = plt.figure()
......@@ -137,11 +139,11 @@ sinplot(-1)
![http://seaborn.pydata.org/_images/aesthetics_24_0.png](img/1b8ec9b9fb72e9193bfefe933d976d37.jpg)
## Overriding elements of the seaborn styles
## 覆盖控制seaborn样式的元素
If you want to customize the seaborn styles, you can pass a dictionary of parameters to the `rc` argument of [`axes_style()`](../generated/seaborn.axes_style.html#seaborn.axes_style "seaborn.axes_style") and [`set_style()`](../generated/seaborn.set_style.html#seaborn.set_style "seaborn.set_style"). Note that you can only override the parameters that are part of the style definition through this method. (However, the higher-level [`set()`](../generated/seaborn.set.html#seaborn.set "seaborn.set") function takes a dictionary of any matplotlib parameters).
如果你想要自己定制seaborn的样式,你可以通过给 [`axes_style()`](../generated/seaborn.axes_style.html#seaborn.axes_style "seaborn.axes_style")[`set_style()`](../generated/seaborn.set_style.html#seaborn.set_style "seaborn.set_style")函数中的 `rc` 参数传递一个参数字典来实现。请注意,您只能通过此方法覆盖作为样式定义一部分的参数。(但是,更高级别的 [`set()`](../generated/seaborn.set.html#seaborn.set "seaborn.set") 函数会获取任何matplotlib参数的字典)。
If you want to see what parameters are included, you can just call the function with no arguments, which will return the current settings:
如果你想看看包含哪些参数,你可以只调用没有参数的函数,这将返回当前设置:
```py
sns.axes_style()
......@@ -183,7 +185,7 @@ sns.axes_style()
```
You can then set different versions of these parameters:
然后,您可以设置这些参数的不同版本:
```py
sns.set_style("darkgrid", {"axes.facecolor": ".9"})
......@@ -193,18 +195,18 @@ sinplot()
![http://seaborn.pydata.org/_images/aesthetics_28_0.png](img/5ebb828c6eed309cb239f2219f35397c.jpg)
## Scaling plot elements
## 缩放图像元素
A separate set of parameters control the scale of plot elements, which should let you use the same code to make plots that are suited for use in settings where larger or smaller plots are appropriate.
一组独立的参数控制绘图元素的比例,这允许您使用相同的代码来制作在适合使用不同大小图片场景下的图片。
First let’s reset the default parameters by calling [`set()`](../generated/seaborn.set.html#seaborn.set "seaborn.set"):
首先,让我们通过调用 [`set()`](../generated/seaborn.set.html#seaborn.set "seaborn.set")来重置默认的参数:
```py
sns.set()
```
The four preset contexts, in order of relative size, are `paper`, `notebook`, `talk`, and `poster`. The `notebook` style is the default, and was used in the plots above.
按照相对大小的顺序排序,四个预设环境是 `paper``notebook``talk``poster``notebook`样式是默认样式,上文中的图就是使用该样式绘制的。
```py
sns.set_context("paper")
......@@ -230,11 +232,11 @@ sinplot()
![http://seaborn.pydata.org/_images/aesthetics_34_0.png](img/5788d7f7d4b9f29480e997849d16ee10.jpg)
Most of what you now know about the style functions should transfer to the context functions.
您现在知道的关于样式函数的大部分内容应该转移到环境函数中。
You can call [`set_context()`](../generated/seaborn.set_context.html#seaborn.set_context "seaborn.set_context") with one of these names to set the parameters, and you can override the parameters by providing a dictionary of parameter values.
你可以通过在调用 [`set_context()`](../generated/seaborn.set_context.html#seaborn.set_context "seaborn.set_context") 时指定环境的名字来设置参数,你也可以通过提供一个参数字典来覆盖原有的参数值。
You can also independently scale the size of the font elements when changing the context. (This option is also available through the top-level [`set()`](../generated/seaborn.set.html#seaborn.set "seaborn.set") function).
你也在转换环境的时候独立地对字符元素的大小进行缩放。(这个操作也能够顶层的 [`set()`](../generated/seaborn.set.html#seaborn.set "seaborn.set") 函数来实现)。
```py
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})
......@@ -244,6 +246,7 @@ sinplot()
![http://seaborn.pydata.org/_images/aesthetics_36_0.png](img/2b8879fda28a3be3acac977e15cbeebb.jpg)
Similarly, you can temporarily control the scale of figures nested under a `with` statement.
同样的,你也可以暂时的通过嵌套在 `with` 语句下的语句来实现图像的缩放。
样式和环境都可以使用 [`set()`](../generated/seaborn.set.html#seaborn.set "seaborn.set") 函数快速配置。 此函数还设置默认调色板,但更详细的介绍将在本教程的 [下一节](color_palettes.html#palette-tutorial) 进行叙述。
Both the style and the context can be quickly configured with the [`set()`](../generated/seaborn.set.html#seaborn.set "seaborn.set") function. This function also sets the default color palette, but that will be covered in more detail in the [next section](color_palettes.html#palette-tutorial) of the tutorial.
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册