Matplotlib Basics
Tags: python, matplotlib, beginners, datascience
Published: 2022-02-10 11:01:38

Data Visualization is a powerful way of presenting data in a readily acceptable and understandable way.
Several tools are used for data visualization including Tableau, PowerBI, and D3.js. Additionally, Python offers several plotting and graphing libraries such as Plotly, Matplotlib and Seaborn.

In this article, we will focus on Matplotlib.

#Matplotlib

Matplotlib is a plotting library used in creating interactive,static and animated visualizations.
The library is popular because:

- it is fast,powerful and efficient
- it is open source
- it works well with various OS.

###Usage
To use Matplotlib:

`import matplotlib`

The pyplot module is used for plotting therefore we import it as:

`import matplotlib.pyplot as plt`

_**Figure:**_ The area that contains all the plots, titles and elements.
_**Axes:**_ They delimit the chart area

##Methods in Matplotlib
In this section we will look at some common methods used for plotting.

![plot() and show()](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8vdqljffgzfqlei3m51.png)
Lets us examine the above code closely. 
First we import Numpy to work with numpy arrays, and import pyplot for plotting.
The **plot()** function takes two parameters; the first specifies values in the x-axis(horizontal) and the second specifies values in the y-axis(vertical). 
The plot() function draw points on the diagram and the default is a line from point to point.
The **show()** function displays all open figures.

Syntax: `matplotlib.pyplot.show(*, block=None)`

It starts an event loop that looks for all currently active figure objects, and opens one or more interactive windows that display the figures.

For a more descriptive figure we can include a title using the **title()** function.
To label the x axis we use **xlabel()** and **ylabel()** to label the y axis.


![title(),xlabel(), ylabel()](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iv5zai8fwcufhbh9bbbv.png)

So far we have a basic descriptive plot, but for more effective data visualization we can add some formatting to increase the appeal of our plots. 
Let us explore some pyplot formatting.

1. Consecutive Plots

You can plot consecutive plots as follows:

```python
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

data = pd.read_csv("https://raw.githubusercontent.com/dphi-official/Assignment_Solutions/master/Standard%20Metropolitan%20Areas%20Data%20-%20train_data%20-%20data.csv")
plt.plot(data.physicians, data.income,color = 'cyan')
plt.show()
plt.plot(data.work_force, data.income,color = 'red')
plt.show()
```
![consecutive plots](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gmo451txuzxlji65aw4d.png)
 
However, we can improve this presentation to be more informative by plotting the two plots on the same figure. You can make better comparisons of different data points.


![consecutive plots](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/40va4iepfut7gamwsxj0.png)
The **legend()** function is used  to add a legend that describes elements in the graph.
The plot() function takes additional parameters that enable use to include more description of how the graphs should appear.
We use color to specify the color, label to provide a label to be used by the legend() function.
Other parameters include marker to specify how markers should be displayed and the linestyle indicating the linestyle to be used in the plot.

![linstyle and markers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iq60wpwgf3oecojvluub.png)
You can achieve the same using:

![linestyle and markers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1g9q0poydzhu835psm9.png)
The b represents the color, the 'o' is the marker and the -- is the linestyle.

Sometimes you might need to resize the figure. The figsize argument is used for resizing.
Syntax: `plt.figure(figsize= (widthpixels, heightpixels))`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/va78z0pjizpo580zykzu.png)

2.Multiple Plots
We can also plot multiple plots in one figure using the subplot() function.
Syntax: `plt.subplot(nrows,ncolumns,index)`

The arguments specify the rows, columns, and index of the plot respectively.


![Multiple plots](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dpdytczom23kkmxej0ns.png)

You're all set with the basics of matplotlib. We will explore matplotlib further in [this](https://dev.to/wanguiwaweru/matplotlib-basic-plots-5b6c) post.