Matplotlib.pyplot.rc_context() in Python
Last Updated :
19 Apr, 2020
Improve
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, etc.
The rc_context() function in pyplot module of matplotlib library is used to return a context manager for managing rc settings.
Python3 1==
Output:
Example 2:
Python3 1==
Output:
matplotlib.pyplot.rc_context() Function
Syntax: matplotlib.pyplot.rc_context(rc=None, fname=None) Parameters:Below examples illustrate the matplotlib.pyplot.rcdefaults() function in matplotlib.pyplot: Example 1:Returns: This method return a context manager for managing rc settings.
- rc: This parameter is a dictionary can also be passed to the context manager.
- fname: This parameter contains the name of file which is to be called.
# implementation of the matplotlib function
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
dots = np.arange(20)
x, y = np.meshgrid(dots, dots)
data = [x.ravel(), y.ravel()]
with plt.rc_context({'axes.xmargin': .2,
'axes.ymargin': .4}):
plt.scatter(*data, c = data[1])
plt.grid(True)
plt.title('matplotlib.pyplot.rc_context()\
Example')
plt.show()

# implementation of the matplotlib function
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
fig, ax = plt.subplots()
dots = np.arange(100)
x, y = np.meshgrid(dots, dots)
data = [x.ravel(), y.ravel()]
ax.scatter(*data, c = data[1])
with plt.rc_context({'axes.autolimit_mode': 'round_numbers',
'axes.xmargin': .8,
'axes.ymargin': .8}):
fig, ax = plt.subplots()
ax.scatter(*data, c = data[1])
plt.grid(True)
plt.title('matplotlib.pyplot.rc_context() Example')
plt.show()

