Saturday, August 27, 2022

How to show matplotlib plot?

Two main options can be used to show the matplotlib plot: show them at the end or show them as soon as they are created.

How to create plots and show them at the end?

The structure of the program code in which you create plots and show them at the end is very simple and it consists of the following parts:
  • import matplotlib library,
  • use plot functions to create plots
  • use plt.show() command to show the generated plots.
Example 1 Create three plots using the matplotlib library and show all of them at the end using plt.show() function.
Solution: The first step is to import the matplotlib library, pyplot sub-module.
import matplotlib.pyplot as plt
In this example we will plot three functions sin(x), cos(x) and sin(x)/2. To generate this data we will import the numpy library.
import numpy as np
Now we have to generate the coordinates on the x-axis. To do this we will use the numpy arange function and generate numbers from 0 to 10 with step 0.1.
x = np.arange(0,10,0.1)
The next step is to define these three plots using matplotlib.pyplot.plot function. But before that, it is a good idea to define the size of the matplotlib figure. The matplotlib figure will be defined using the pyplot figure function using the figsize attribute with width and height equal to 12 and 8 inches, respectively.
plt.figure(figsize=(12,8))
plt.plot(x,np.sin(x))
plt.plot(x,np.cos(x))
plt.plot(x,np.sin(x)/2)
And finally to show all three plots on the same figure type in the following command.
plt.show()
The entire code created in this example is shown below.
import numpy as np
x=np.arange(0,10,0.1)
plt.figure(figsize=(12,8))
plt.plot(x,np.sin(x))
plt.plot(x,np.cos(x))
plt.plot(x,np.sin(x)/2)
plt.show()
The output is shown in Figure 1.
Figure 1 - Plot showing functions \(\sin(x), \cos(x), \frac{\sin(x)}{2}\)

How to show and save the matplotlib figure?

The plot can be shown and saved using matplotlib.pyplot savefig function. The general "useful" form of the savefig function
plt.savefig(fname, dpi = 'figure', format = None, 
bbox_inches = None)
The useful form of the savefig function is to show only the most important arguments. The fname argument is a path or a Python file-like object. Usually the default format is .png so it can be defined in fname is "name.png". The dpi represents the resolution in dots per inch value. The default value is set in matplotlib.pyplot.figure function. The format argument represents the file format (png, pdf, svg). The bbox_inches is the bounding box in inches. If set to None then only the given portion of the figure is saved. It is a usually good idea to set the bbox_inches set to "tight" to try to figure out the tight bbox of the figure. Example 2 - save the plot from the Example 1 with dpi of 300.
Solution: The code is the same as in previous example but instead of plt.plot() we will use plt.savefig(). The name of savefig function is "ShowPlot" with a dpi of 300.
Figure 2 - Plot showing functions \(\sin(x), \cos(x), \frac{\sin(x)}{2}\)

No comments:

Post a Comment