Sunday, June 9, 2024

Basic Structure of a Pyplot Script

The basic structure of Pyplot script consist of the following components:
  1. Importing the Matplotlib library
  2. Preparing the data
  3. Creating the Plot
  4. Customizing the plot
  5. Displaying or Saving the Plot

Importing the Matplotlib library

This is basic and mandatory step. Without importing the matplotlib library there are is not showing any plots. However, the goal is to import the pyplot module from the matplotlib library.
import matplotlib.pyplot as plt

Preparing the Data

In order to plot your data you need to prepare the data. This can be done using simple Python lists, numpy arrays or pandas dataframes. As you probably know the the lists are very easy to create.
x = [i for i in range(0, 20, 1)]
y = [i**2 for i in range(0,20,1)]
The previous example is very simple. In one line we have created a list with numbers in range from 0 to 19 and store this list to variable named x. The second variable is the y variable that contains numbers from o to 19**2. Creating variables using numpy arrays is also pretty simple. All you have to do is to use np.arange function and specify range.
x = np.arange(0,20,1)
y = np.arange(0,363,19)
The definition of x is very simple i.e. use numpy function arange, define the initial value, ending value, and the step. The numpy array will contain the numbers from 0 up to 19. The initial step in y variable is from 0 up to 363 and the step is 19. The last number in the list will be 19**2 which is 361.
Regarding the pandas data frame first it is required to import the pandas library.
import pandas as pd
The pandas data frame is usually created from multiple lists or numpy array using the pd.DataFrame function.
data = pd.DataFrame([list1,list2,list3])
On the other hand the dataset used for analysis is usually stored somewhere in the .csv file. So to open it and use it simply type the pd.read_csv() function.
data=pd.read_csv("file_name.csv")

Creating the Plot

With Pyplot functions you will create the plot or graphical representation of your data.
plt.plot(x,y)

Plot Customization

Customize your plot by adding the title, x and y axis labels, plot grid, and legend.
plt.title("This is new plot")
plt.xlabel("X-Ax")
plt.ylabel("Y-Ax")
plt.grid(True)
plt.legend(["The first plot function"])

Displaying or Saving the Plot

Using the plt.show() function you can display the plot, while using the plt.savefig() you can save your figure. However, if you want to save the figure make sure that plt.show() function is commented if it is decleare before the plt.savefig function. Otherwise the plot will be blank.
plt.show() # to display the plot
plt.savefig() # To save the figure

Simple example Script

In this example we will use the previous step by step element and create a new graph. The plot will show the y versus x variable defined as lists from 0 to 19. The x variable will contain numbers from 0 to 19 including 19. While the y variable will contain numbers from 0 to 19**2.
import matplotlib.pyplot as plt
x = [i for i in range(0,20,1)]
y = [i**2 for i in range(0,20,1)]
plt.plot(x,y)
plt.title("simple plot")
plt.xlabel("X-Axsis")
plt.ylabel("Y-Axis")
plt.legend(['Simple Legend'])
The result is shown in the following figure.
2024-06-10T00:48:21.468785 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/
Figure 1 - Basic plot using matplotlib.pyplot module.

No comments:

Post a Comment