Open In App

Matplotlib.pyplot.rc_context() in Python

Improve
Improve
Like Article
Like
Save
Share
Report

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.

matplotlib.pyplot.rc_context() Function

The rc_context() function in pyplot module of matplotlib library is used to return a context manager for managing rc settings.

Syntax: matplotlib.pyplot.rc_context(rc=None, fname=None)

Parameters:

  • 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.

Returns: This method return a context manager for managing rc settings.

Below examples illustrate the matplotlib.pyplot.rcdefaults() function in matplotlib.pyplot:

Example 1:




# 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()


Output:

Example 2:




# 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()


Output:



Last Updated : 19 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads