Python

Matplotlib Retina

Matplotlib is a great resource for Python-based plotting. However, if we have to generate plots inline within a Spyder application, we have certainly observed that the default resolution of the plot image is rather low, making our graphs look blurred and difficult to read. This can be tricky, especially if you are going to display the plot or convert it to some other format.

This article will show you how to modify the default settings to improve the resolution of Matplotlib plots for both inline and exported. We have to import the command of the retina in the code that will enhance the display of the plot as in retina quality. The figures will seem better on any display with retina resolution; but, if your display’s resolution is sub-retina, the improvement will be less obvious.

Retina Displays with Inline Backend in Python

The default visuals sometimes appear blurry on higher resolution screens such as the Retina displays, but we can view high-resolution plot results if we have the latest MacBook with a Retina display.

All we have to do now is add the following command to our code. Note that we have to save our file in the “.ipy” extension if we are using matplotlib.pyplot interface.

# %config InlineBackend.figure_format = 'retina'

Example 1:

We have an example here to show how we can display the Matplotlib graph by using a retina high-resolution format. First, we have imported a command “%matplotlib inline” which will help us to output the plotting commands to be presented inline within the frontends after enabling the ‘inline’ Matplotlib backend.

It means that the plot will appear below the cell where we have given the commands and the output plot will also be included in our python notebook document. Then, we have the NumPy library as “np” alias and the matplotlib.pyplot as “plt”. After importing the important libraries, we have given a retina command that will enhance the resolution of the graph. The size of the figure is also given here which will display the figure in a bigger size.

The variable is defined as “i” and set a numpy linspace function in it. It is used to make a series that’s uniformly spaced in a certain interval. At last, we have used the pyplot package from Matplotlib and passed the variable “i” to it. The numpy sine function is also used for generating the sine plot.

%matplotlib inline

import numpy as np

import matplotlib.pyplot as plt

%config InlineBackend.figure_format = "retina"

plt.rcParams["figure.figsize"] = (9.0,3.0)

i = np.linspace(-2*np.pi,2*np.pi,200)

plt.plot(i,np.sin(i)/i);

The output of the retina high resolution is displayed in the image below.

Example 2:

We have used a two lines plot that uses the inline backend figure format option as “retina”. The resultant lines will have a retina quality of high resolution. First of all, we have to use the command “%matplotlib inline” in the prompt shell of Spyder. The command enables the plot generated inside the prompt shell.

Here, we have configured the retina display command in our code after importing all the essential python libraries for using trigonometric functions and generating the plot. We have also set the matplotlib runtime configuration (rc) as “rcParams” that contains the figure size for the plot element, we have generated when it loads.

The variable “a” is declared and uses the NumPy linspace function for creating the series of certain intervals. Then, using the Matplotlib pyplot package in which we have passed the variable “a” and the trigonometric sine function. The “plot. Plot” function will generate the two lines of sine functions as we have used two sine functions with slightly different calculations.

%matplotlib inline

import numpy as np

import matplotlib.pyplot as plt

%config InlineBackend.figure_format = "retina"

plt.rcParams["figure.figsize"] = (5.0,3.0)

a = np.linspace(-4*np.pi,4*np.pi,50)

plt.plot(a,np.sin(a)/a,a,np.sin(2*a)/a);

The Matplotlib retina has a high-resolution plot in the below image.

Example 3:

We have displayed the plot by using different colors and also using different symbols for pointing to the specific point in the plot given. The plot will be rendered in the Matplotlib retina quality. In the code, we have provided the inline command. We have also imported the NumPy library and Matplotlib library.

The retina display option is configured in the command. Then, we have set the size of the plot and declared a variable “i” that will create the series of intervals by using the NumPy linspace function. Note that here we have called the plt. Plot function in which we have generated a cosine graph. The cosine graph line is given a green color with the star symbol “*” and the other cosine line has a color red which is using the bullet symbol.

%matplotlib inline

import numpy as np

import matplotlib.pyplot as plt

%config InlineBackend.figure_format = "retina"

plt.rcParams["figure.figsize"] = (7, 9)

i = np.linspace(-6*np.pi,6*np.pi,50)

plt.plot(i,np.cos(i)/i, 'g-*',i,np.cos(3*i)/i, 'r-o');

As you can see, the graph below is so fine and has a clear vision of the Matplotlib retina display.

Example 4:

We have created a color-mapped scatter plot in a retina display mode. The command of the Matplotlib retina option is given in the “InlineBackend.figure_format”. The variable is defined as “thetaValue” and initialized with the NumPy linspace function that has taken “60” steps from “0” to “6” PI. The array size is also set to “10” for “60” values by using the NumPy ones function. It will return the given array shape with ones.

Then, we have a variable as “a” which is using the np.random.rand function and passed a value “60” in it. The function is taking the “60” random value in [0, 1] and specifying the output size. The variables “b” and “c” are declared and passed the “thetaValue” as a parameter. The variable “b” is using the NumPy cosine function and the variable “c” is using the NumPy sine function.

At last, we use the scatter() method in pyplot which is used to create a scatter plot. For each observation, the scatter() function draws a single dot. It requires two identical-length arrays, one for x-axis values and the other for y-axis values.

%matplotlib inline

import numpy as np

import matplotlib.pyplot as plt

%config InlineBackend.figure_format = "retina"

thetaValue= np.linspace(0,4*np.pi,60)

arrSize = 10*np.ones(60)

a = np.random.rand(60)

b = thetaValue*np.cos(thetaValue)

c = thetaValue*np.sin(thetaValue)

plt.scatter(b,c,arrSize,a)

The below image is the color-mapped scatter plot.

Example 5:

Here, we have created multiple plots by using the subplots. The plot is rendered in the retina quality. Initially, we have given an inline command in our prompt shell. After that, we have implemented the code which uses the NumPy library and the Matplotlib pyplot package. The Matplotlib retina display command is also given as we want a plot of high resolution.

Then, we have set the series of intervals for the graph by using the NumPy linspace function and stored this in the variable “thetaIs’ ‘. The two variables are also declared here as “i” and “j”. These variables are using NumPy cosine and sine function respectively. With the pyplot, we have called the subplot method which is creating 1 row, 2 columns, and active as plot number 2.

The plt.plot function is taking “i” and setting the line as blue color and pointing with the plus symbol “+”. There is a second subplot which is creating 1 row, 2 columns, and is active as plot number 1. The plt.plot is taking “j” and setting its color to red with the triangle symbol “^”.

%matplotlib inline

import numpy as np

import matplotlib.pyplot as plt

%config InlineBackend.figure_format = "retina"

thetaIs = np.linspace(0,1*np.pi,10)

i = thetaIs*np.sin(thetaIs)

j = thetaIs*np.cos(thetaIs)

plt.subplot(1,2,2)

plt.plot(i,'b-+')

plt.subplot(1,2,1)

plt.plot(j,'r-^');

The subplots below are very clear as we have a Matplotlib retina display.

Conclusion

As we have discussed the usage of Matplotlib retina high-resolution basic function in creating a plot. We have shown you a different example of plotting different graphs that are using retina commands in the code implementation. You can now figure out how the retina display will be enhanced by the default low-quality figure in the Spyder application. The Matplotlib retina display will surely help you in generating a high-quality graph.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content