Seaborn.plotting_context() method



The Seaborn.plotting_context() method gets the parameters that control the scaling of plot elements. The scaling does not effect the overall style of the plot but it does affect things like labels, lines and other elements of the plot. It is done using the matplotlib rcParams system.

For example, if the base context is “notebook” and other contexts are “poster”, “paper” and “information”. These other contexts are the notebook parameters scaled by different values. Font elements in a context can also be scaled independently.

This function can also be used to alter the global default values.

Syntax

Following is the syntax of the plotting_context() method −

seaborn.plotting_context(context=None, font_scale=1, rc=None)

Parameters

Following are the parameters of this method −

S.No Parameter and Description
1 context

Takes the following as input none, dict, or one of {paper, notebook, talk, poster} and determines a dictionary of parameters or the name of a preconfigured set.

2 Rc

Takes rcdict as value and is an optional parameter that performs Parameter mappings to override the values in the preset seaborn style dictionaries. This only updates parameters that are considered part of the style definition.

3 Font_scale

Takes a floating point value as input, and is optional parameter. It separate scaling factor to independently scale the size of the font elements.

Example 1

When we will call the function by itself without any arguments., the scaling parameters' current defaults will be returned.

import seaborn as sns
import matplotlib.pyplot as plt
tips=sns.load_dataset("tips")
tips.head()
sns.plotting_context()
plt.show()

Output

the output obtained is a list of all the current defaults and can be seen below.

{'axes.labelsize': 12.0, 'axes.linewidth': 1.25, 'axes.titlesize': 12.0, 'font.size': 12.0, 'grid.linewidth': 1.0, 'legend.fontsize': 11.0, 'legend.title_fontsize': 12.0, 'lines.linewidth': 1.5, 'lines.markersize': 6.0, 'patch.linewidth': 1.0, 'xtick.labelsize': 11.0, 'xtick.major.size': 6.0, 'xtick.major.width': 1.25, 'xtick.minor.size': 4.0, 'xtick.minor.width': 1.0, 'ytick.labelsize': 11.0, 'ytick.major.size': 6.0, 'ytick.major.width': 1.25, 'ytick.minor.size': 4.0, 'ytick.minor.width': 1.0}

Example 2

When we call the function with a name of a predefined style, the avlues for this style will be shown. In this example, we will call the talk style and see the values for it.

import seaborn as sns
import matplotlib.pyplot as plt
tips=sns.load_dataset("tips")
tips.head()
sns.plotting_context("talk")
plt.show()

Output

now the output obtained is below,

{'axes.labelsize': 18.0, 'axes.linewidth': 1.875, 'axes.titlesize': 18.0, 'font.size': 18.0, 'grid.linewidth': 1.5, 'legend.fontsize': 16.5, 'legend.title_fontsize': 18.0, 'lines.linewidth': 2.25, 'lines.markersize': 9.0, 'patch.linewidth': 1.5, 'xtick.labelsize': 16.5, 'xtick.major.size': 9.0, 'xtick.major.width': 1.875, 'xtick.minor.size': 6.0, 'xtick.minor.width': 1.5, 'ytick.labelsize': 16.5, 'ytick.major.size': 9.0, 'ytick.major.width': 1.875, 'ytick.minor.size': 6.0, 'ytick.minor.width': 1.5}

Example 3

We will call the function along with a plot and see the difference in the plot. The below code can be used to do so.

import seaborn as sns
import matplotlib.pyplot as plt
tips=sns.load_dataset("tips")
tips.head()
with sns.plotting_context("talk"):
sns.barplot(x=["A", "B", "C"], y=[10, 32, 67])
plt.show()

Output

The output obtained is as follows −

seaborn_plotting_context_method
seaborn_themeing_introduction.htm
Advertisements