其他分享
首页 > 其他分享> > Seaborn风格设置

Seaborn风格设置

作者:互联网

官方网站:seaborn: statistical data visualization — seaborn 0.11.2 documentation (pydata.org)

Seaborn是基于matplotlib的python数据可视化库,提供更高层次的API封装,包括一些高级图表可视化等工具,用于绘制更美观和信息更丰富的统计图表。

导入模块:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

风格设置

全局风格设置

seaborn.set(
    context='notebook',
    style='darkgrid',
    palette='deep',
    font='sans-serif',
    font_scale=1,
    color_codes=True,
    rc=None,
)
Docstring:
Set aesthetic parameters in one step.

Each set of parameters can be set directly or temporarily, see the
referenced functions below for more information.

Parameters
----------
context : string or dict
    Plotting context parameters, see :func:`plotting_context`
style : string or dict
    Axes style parameters, see :func:`axes_style`
palette : string or sequence
    Color palette, see :func:`color_palette`
font : string
    Font family, see matplotlib font manager.
font_scale : float, optional
    Separate scaling factor to independently scale the size of the
    font elements.
color_codes : bool
    If ``True`` and ``palette`` is a seaborn palette, remap the shorthand
    color codes (e.g. "b", "g", "r", etc.) to the colors from this palette.
rc : dict or None
    Dictionary of rc parameter mappings to override the above.
# 创建正弦函数
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()

sns.set()     #设置seaborn默认风格,一旦设置就设置了全局的风格
sinplot()
plt.grid(linestyle = '--')     #设置网格线

局部风格设置

seaborn.set_style(style=None, rc=None)

Docstring:
Set the aesthetic style of the plots.

This affects things like the color of the axes, whether a grid is
enabled by default, and other aesthetic elements.

Parameters
----------
style : dict, None, or one of {darkgrid, whitegrid, dark, white, ticks}
    A dictionary of parameters or the name of a preconfigured set.
rc : dict, optional
    Parameter mappings to override the values in the preset seaborn
    style dictionaries. This only updates parameters that are
    considered part of the style definition.

Examples
--------
>>> set_style("whitegrid")

>>> set_style("ticks", {"xtick.major.size": 8, "ytick.major.size": 8})

See Also
--------
axes_style : return a dict of parameters or use in a ``with`` statement
             to temporarily set the style.
set_context : set parameters to scale plot elements
set_palette : set the default color palette for figures
#set_style()切换图表风格
#风格选择:"white","dark","whitegrid","darkgrid","ticks"

fig = plt.figure(figsize=(6, 6))

style = 'ticks'
ax1 = fig.add_subplot(2,1,1)
sns.set_style(style)
data = np.random.normal(size=(20,6)) + np.arange(6)/2
sns.boxplot(data=data)
plt.title('style-{0}'.format(style))

ax2 = fig.add_subplot(2,1,2)
sinplot()

设置图形坐标轴

seaborn.despine(
    fig=None,
    ax=None,
    top=True,
    right=True,
    left=False,
    bottom=False,
    offset=None,
    trim=False,
)
Docstring:
Remove the top and right spines from plot(s).

fig : matplotlib figure, optional
    Figure to despine all axes of, default uses current figure.
ax : matplotlib axes, optional
    Specific axes object to despine.
top, right, left, bottom : boolean, optional
    If True, remove that spine.
offset : int or dict, optional
    Absolute distance, in points, spines should be moved away
    from the axes (negative values move spines inward). A single value
    applies to all spines; a dict can be used to set offset values per
    side.
trim : bool, optional
    If True, limit spines to the smallest and largest major tick
    on each non-despined axis.
#创建图表
fig = plt.figure(figsize=(6,9))
plt.subplots_adjust(hspace=0.3)
sns.set_style('darkgrid')


ax1 = fig.add_subplot(3,1,1)
sinplot()
sns.despine()      #默认删除上和右坐标轴


ax2 = fig.add_subplot(3,1,2)
sns.violinplot(data=data)
sns.despine(offset=10, trim=True)      #offset: 与坐标轴之间的偏移;trim=True,将坐标轴限制在数据最大和最小值之间

ax3 = fig.add_subplot(3,1,3)
sns.boxplot(data=data, palette='deep')
sns.despine(left=False, right=True)   #隐藏右边坐标轴

设置子图风格

seaborn.axes_style(style=None, rc=None)
Docstring:
Return a parameter dict for the aesthetic style of the plots.

This affects things like the color of the axes, whether a grid is
enabled by default, and other aesthetic elements.

This function returns an object that can be used in a ``with`` statement
to temporarily change the style parameters.

Parameters
----------
style : dict, None, or one of {darkgrid, whitegrid, dark, white, ticks}
    A dictionary of parameters or the name of a preconfigured set.
rc : dict, optional
    Parameter mappings to override the values in the preset seaborn
    style dictionaries. This only updates parameters that are
    considered part of the style definition.
#axes_style()设置局部图表(子图)风格

#与with配合使用,实现局部代码区分
with sns.axes_style('darkgrid'):
    plt.subplot(211)
    sinplot()

#外部图表风格
sns.set_style('whitegrid')
plt.subplot(212)
sinplot()

设置图形显示尺度

seaborn.set_context(context=None, font_scale=1, rc=None)
Docstring:
Set the plotting context parameters.

This affects things like the size of the labels, lines, and other
elements of the plot, but not the overall style. The base context
is "notebook", and the other contexts are "paper", "talk", and "poster",
which are version of the notebook parameters scaled by .8, 1.3, and 1.6,
respectively.

Parameters
----------
context : dict, None, or one of {paper, notebook, talk, poster}
    A dictionary of parameters or the name of a preconfigured set.
font_scale : float, optional
    Separate scaling factor to independently scale the size of the
    font elements.
rc : dict, optional
    Parameter mappings to override the values in the preset seaborn
    context dictionaries. This only updates parameters that are
    considered part of the context definition.

Examples
--------
>>> set_context("paper")

>>> set_context("talk", font_scale=1.4)

>>> set_context("talk", rc={"lines.linewidth": 2})
#set_context()设置图形显示尺度
#尺度类型:"paper","notebook","talk","poster"

sns.set_context('paper')      #默认为notebook
sinplot()

标签:style,set,设置,parameters,Seaborn,None,风格,context,dict
来源: https://www.cnblogs.com/xiqi2018/p/15770595.html