In matplotlib, the new figure can be defined using plt.figure(). The general form is given below.
From all previous function arguments, the most commonly used are figsize and dpi. The most basic definition of the figure is typing out
Example 1 Generate the plot of function \(y(x) = \sin(x)\) using default figure size and \(12\times8[\mathrm{in}]\).
Solution: To generate a plot of a function \(y(x) = \sin(x)\) first we need to create \(x\) and then calculate the \(y(x)\). To generate \(x\) we will use numpy and create a numpy array in the range from 0 to 20.1 with step 0.1. To do this we will need the numpy library so we have to import the library. After the \(x\) is created we can calculate the \(y(x)\) using numpy sin function.
Figure 1 - The plot figure with default figure size.
The next step in this example is to enlarge the figsize from default \(6.4\times 4.8 [\mathrm{in}]\) to \(12\times 8[\mathrm{in}]\).
Figure 2 - The plot figure with figsize \(12\times 8 [\mathrm{in}]\)
Example 2 Plot the function \(y(x) = \cos(x)\) for x in range from 0 to 20 using matplotlib library, setting the figsize to \(12\times8 [\mathrm{in}]\) and set the dpi to 300.
Solution: The only new parameter in this example has to be set to 300. First of all the dpi stands for the dots-per-inch and determines the number of pixels the figure contains. The default value of this parameter is matplotlib.pyplot.figure function is 100 dots-per-inch. So if the figure size is \(12\times 8[\mathrm{in}]\) then the number of pixels are \(12 \times 100 = 1200\), \(8 \times 100 = 800\) i.e. \(1200 \times 800\) pixels. In our case, the dpi has to be set to 300, and the figure size or figsize is (12,8). The number of pixels is \(3600\times 2400\) pixels.
\begin{eqnarray}
12 \cdot 300 &=& 3600,\\
8 \cdot 300 &=& 2400.
\end{eqnarray}
To plot the \(y(x) = \cos(x)\) we have to generate the \(x\) and then calculate the \(y\). This will be done using the numpy library. After the numpy is imported the \(x\) will be created using numpy.arange function in the range from 0 to 20.1 with the step of 0.1. Then the \(y\) will be created as a list in one line of code.
Figurer 3 - The plot figure with figsize set to \(12\times 8 [\mathrm{in}]\) and dpi to 300.
Example 3 - Create a plot of a function \(y(x) = |\sin(x)|\) using matplotlib. Set the figsize to \(12\times 8 [\mathrm{in}]\), facecolor to grey and edge color to red.
Solution: The face color is the background color of the matplotlib figure. The default value of the facecolor is white so we have to change it to grey. The edgecolor is defined as the border color and will be set to "red" however, to make it visible we will introduce the linewidth parameter and set its value to 5. The linewidth parameter is self-explanatory and in this case, represents the line width of the edge.
To generate the data that will be plotted the numpy library is imported. The \(x\) is created using numpy.arange function in the range from 0 to 20.1 with the step of 0.1. The \(y\) is a list created in one line of code using two functions absolute value and sine function from the numpy library. This can also be created using the math library.
Figure 4 the plot of the function \(y(x) = |\sin(x)|\) with grey face color and red edge color with linewidth equal to 5.
matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None,With num argument, a unique identifier for the figure is defined. If the identifier exists, the figure is made active and returned. The num can be int/str / Figure. If num is int it will be used for the Figure.number attribute. Otherwise, an auto-generated integer value is used. If num is a string, the figure label and the window are set to this value. The argument figsize is the size of the figure in inches i.e. a tuple with width and height has to be defined. The dpi is the resolution of the figure in dots-per-inch. The dpi is a type of float value; the default value is 100.0. The facecolor is a float-type argument with which the background color is defined. The EdgeColor is the border color and by default, the color is "white".
edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>,
clear=False, **kwargs)
From all previous function arguments, the most commonly used are figsize and dpi. The most basic definition of the figure is typing out
plt.figure(figsize=(12,8))
Example 1 Generate the plot of function \(y(x) = \sin(x)\) using default figure size and \(12\times8[\mathrm{in}]\).
Solution: To generate a plot of a function \(y(x) = \sin(x)\) first we need to create \(x\) and then calculate the \(y(x)\). To generate \(x\) we will use numpy and create a numpy array in the range from 0 to 20.1 with step 0.1. To do this we will need the numpy library so we have to import the library. After the \(x\) is created we can calculate the \(y(x)\) using numpy sin function.
import numpy as npTo plot this data we have to import the matplotlib library, and then plot the function using plot() command. To show the plot we will use the matplotlib.pyplot function show().
x=np.arange(0,20.1,0.1)
y = []
for i in range(len(x)):
y.append(np.sin(x[i]))
import matplotlib.pyplot as pltThe entire code used so far in this example is shown below.
plt.plot(x,y)
plt.show()
import numpy as npThe previous block of code will generate the figure with a default figsize of \(6.4\times 4.8 [\mathrm{in}]\). The result is shown in Figure 1.
import matplotlib.pyplot as plt
x=np.arange(0,20.1,0.1)
y=[]
for i in range(len(x)):
y.append(np.sin(x[i]))
plt.plot(x,y)
plt.show()
plt.figure(figsize=(12,8))The entire code of Example 1 with figsize (12,8) is given below.
import numpy as npThe result is shown in the following figure.
import matplotlib.pyplot as plt
t=np.arange(0,20.1,0.1)
y=[]
for i in range(len(x)):
y.append(np.sin(x[i]))
plt.figure(figsize=(12,8))
plt.plot(x,y)
plt.show()
import numpy as npAfter data is generated the figsize and dpi will be set using matplotlib.pyplot.figure function.
x = np.arange(0,20.1,0.1)
y = [np.cos(x[i]) for i in range(len(x))]
plt.figure(figsize=(12,8), dpi = 300)The entire code as well as the result in this example are given below.
plt.plot(x,y)
plt.show()
import numpy as npThe result is shown in Figure 3.
x = np.arange(0,20.1,0.1)
y = [np.cos(x[i]) for i in range(len(x))]
plt.figure(figsize=(12,8), dpi = 300)
plt.plot(x,y)
plt.show()
Solution: The face color is the background color of the matplotlib figure. The default value of the facecolor is white so we have to change it to grey. The edgecolor is defined as the border color and will be set to "red" however, to make it visible we will introduce the linewidth parameter and set its value to 5. The linewidth parameter is self-explanatory and in this case, represents the line width of the edge.
To generate the data that will be plotted the numpy library is imported. The \(x\) is created using numpy.arange function in the range from 0 to 20.1 with the step of 0.1. The \(y\) is a list created in one line of code using two functions absolute value and sine function from the numpy library. This can also be created using the math library.
import numpy as npTo plot the figure import the matplotlib library, and define the figure parameters including face color and edge color.
x = np.arange(0,20.1,0.1)
y = [np.abs(np.sin(x[i])) for i in range(len(x))]
import matplotlib.pyplot as pltThe entire code for this example is given below.
plt.figure(figsize=(12,8),facecolor="grey",edgecolor="red",linewidth=5)
plt.plot(x,y)
plt.show()
import numpy as npThe output of the previous code is shown in Figure 4.
x = np.arange(0,20.1,0.1)
y = [np.abs(np.sin(x[i])) for i in range(len(x))]
import matplotlib.pyplot as plt
plt.figure(figsize=(12,8),facecolor="grey",edgecolor="red",linewidth=5)
plt.plot(x,y)
plt.show()