Matplotlib Tutorial for Beginners 📈
Basic methods you need to know to draw perfect plots with Matplotlib.
Before analyzing the data, you should understand the dataset. Data visualization is the best way to understand data. Matplotlib is one of the most widely used libraries in Python to visualize data. In this post, I’ll cover the following topics:
- How to draw a plot?
- Setting color, markings, and line styles
- Adding a legend
- Adjusting thickness, label, and legend
- Adding text and symbols to the plot
- Saving the plot
Let’s dive in!
Hi, welcome to Tirendaz Academy. Tirendaz Academy is an online education platform that produces videos and writes blog…
Creating a Graphics Object
Before drawing a plot, you can create a graphic object. To show this, let me import Matplotlib and Numpy libraries.
You can find the notebook and datasets here.
If you want to see the plots interactively, let’s use the % matplotlib notebook magic command.
In Matplotlib, plots are designed with a Figure object. First, let’s create a new figure object.
Let’s create subplots using the add_subplot method.
So we created one of 4 subplots. Let’s add the other three subplots.
Because the plot is interactive, every code we run is displayed on the plot. For example, let’s generate random data from the normal distribution and use the function that finds the cumulative sum of these values.
Let’s plot the data on the first subplot.
Here, the “k --” option refers to the plot being a dashed line. Now let’s plot the same data with other subplots in different formats.
Colors & Markings
You can set the colors and marking in the plot method. To illustrate this, let’s create two data named x and y.
Let’s create a graph object named fig.
Let me draw a plot inside this graph object.
Let’s draw the x and y data on this plot. To show this, I’m going to use red dashes.
You can also set these options separately as follows:
To see line markers and color styles, you can use the following code:
To show line styles, I’m going to use the data again. Let’s draw a line plot.
As you can see, marks can be added to line graphs that show the actual data points. Notice that the data points were plotted linearly by default. You can change this style with the drawstyle parameter. To show this, let’s use %matplotlib inline command to see this graph between cells statically.
Now let’s draw a line plot.
Let’s add a style to the plot using the drawstyle parameter.
Let’s use the legend method to display the labels in the plot.
Thickness, Label, and Legend
You can use methods such as the xlim for the range of the plot, xticks for the location of the markers, and xticklabels for labeling the marks. To illustrate these methods, let’s use the %matplotlib notebook command to plot interactive graphics.
Let’s create a graph object now.
Let’s add a subplot.
Now, let’s draw the plot of the data.
You can use the set_xticks and set_xticklabels methods to change the signs of the x-axis. With the rotation option, you can adjust the degree of mark labels on the x-axis.
You can use the set_title method for the title of the plot.
Let’s name the x and y axes.
The axes class has a set method. This method is used to write plot features. For example, let’s take a feature variable which is a dictionary structure.
Now let’s pass this variable to the set method.
Another important component of the plot is the legend. Legend defines plot elements. The easiest way to add a legend is to use the label parameter for each part of the plot. Let’s create a graph object for example.
Let’s add a graphics area to this object.
Let me create data now. I’m going to generate random numbers from a normal distribution and assign cumulative sums of these numbers to variables.
Now let’s set the labels for legend with the label parameter.
To add a legend, you can use the legend method. If you don’t use any parameter in the legend method, automatic legends are found and added to the plot.
You can add text and arrows to the plot. To show this, let’s draw a plot using the real dataset. The dataset shows Facebook’s stock market values. You can find the dataset here. To read the dataset, first, let’s import the Pandas.
Let’s read the dataset and take a look at the first five rows.
Let’s set dates as an index and convert these dates to the DateTime format.
Now let’s see the first rows of the dataset again.
Let’s select the column that shows the closing prices in this dataset.
Now let’s draw a line plot of this variable. First, let’s create a graph object and add a plot to this graph object.
Now let’s draw the line plot of the closing prices.
Let’s add a title to the plot.
Now let’s show the smallest value on the plot with an arrow. Let me find the minimum closing price.
The date index of this value is 2018–12–24. Let’s check it out.
Let’s assign the date and text to the variables.
Now let’s add text to the plot with an arrow using the annotate method. In this method, the xy argument location is used for the location of the text, while the xytext argument is used for the location of the text.
Saving the Plot
You can save the plot with the savefig method. For example, let’s save the plot in pdf format.
You can also save the plot in another format. Let’s say png format.
The resolution comes to 100 by default. You can also change the resolution with the dpi argument.
To cut the blank areas around the plot, you can use the bbox_inches parameter.
Data visualization is one of the most important states of data analysis. In this post, I talked about how to do data visualization with Matplotlib. That’s it. I hope you enjoy it. Thank you for reading. You can find this notebook here.
10 Best Python Libraries for Data Science
Libraries that data scientists should know and top 5 books to learn them.
8 Best Seaborn Visualizations
Hands-on statistical plots with Seaborn using the penguin dataset.
If this post was helpful, please click the clap 👏 button below a few times to show me your support 👇