Simple Plot in Python using Matplotlib
Matplotlib is a Python library that helps in visualizing and analyzing the data and helps in better understanding of the data with the help of graphical, pictorial visualizations that can be simulated using the matplotlib library. Matplotlib is a comprehensive library for static, animated and interactive visualizations.
Installation of matplotlib library
Step 1: Open command manager (just type “cmd” in your windows start search bar)
Step 2: Type the below command in the terminal.
Step 3: Then type the following command.
pip install matplotlib
Creating a Simple Plot
The code seems self-explanatory. Following steps were followed:
- Define the x-axis and corresponding y-axis values as lists.
- Plot them on canvas using .plot() function.
- Give a name to x-axis and y-axis using .xlabel() and .ylabel() functions.
- Give a title to your plot using .title() function.
- Finally, to view your plot, we use .show() function.
Let’s have a look at some of the basic functions that are often used in matplotlib.
|plot()||it creates the plot at the background of computer, it doesn’t displays it. We can also add a label as it’s argument that by what name we will call this plot – utilized in legend()|
|show()||it displays the created plots|
|xlabel()||it labels the x-axis|
|ylabel()||it labels the y-axis|
|title()||it gives the title to the graph|
|gca()||it helps to get access over the all the four axes of the graph|
|gca().spines[‘right/left/top/bottom’].set_visible(True/False)||it access the individual spines or the individual boundaries and helps to change theoir visibility|
|xticks()||it decides how the markings are to be made on the x-axis|
|yticks()||it decides how the markings are to be made on the y-axis|
|gca().legend()||pass a list as it’s arguments of all the plots made, if labels are not explicitly specified then add the values in the list in the same order as the plots are made|
|annotate()||it is use to write comments on the graph at the specified position|
|figure(figsize = (x, y))||whenever we want the result to be displayed in a separate window we use this command, and figsize argument decides what will be the initial size of the window that will be displayed after the run|
|subplot(r, c, i)||it is used to create multiple plots in the same figure with r signifies the no of rows in the figure, c signifies no of columns in a figure and i specifies the positioning of the particular plot|
|set_xticks||it is used to set the range and the step size of the markings on x – axis in a subplot|
|set_yticks||it is used to set the range and the step size of the markings on y – axis in a subplot|
Note: Try removing the features added one by one and understand how does the output result changes
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course