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.
(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).
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.
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.
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:
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.
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:
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.
You can also control which spines are removed with additional arguments to [`despine()`](../generated/seaborn.despine.html#seaborn.despine"seaborn.despine"):
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:
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).
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"):
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.
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.
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).
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.