Matplotlib

Matplotlib is an easy-to-use, low-level data visualization library that is built on NumPy arrays. It consists of various plots like scatter plot, line plot, histogram, etc. Matplotlib provides a lot of flexibility. 

To install this type the below command in the terminal.

pip install matplotlib

Refer to the below articles to get more information setting up an environment with Matplotlib.

After installing Matplotlib, let’s see the most commonly used plots using this library.

Scatter Plot

Scatter plots are used to observe relationships between variables and uses dots to represent the relationship between them. The scatter() method in the matplotlib library is used to draw a scatter plot.

Example:

Python3




import pandas as pd
import matplotlib.pyplot as plt
 
 
# reading the database
data = pd.read_csv("tips.csv")
 
# Scatter plot with day against tip
plt.scatter(data['day'], data['tip'])
 
# Adding Title to the Plot
plt.title("Scatter Plot")
 
# Setting the X and Y labels
plt.xlabel('Day')
plt.ylabel('Tip')
 
plt.show()


Output:

This graph can be more meaningful if we can add colors and also change the size of the points. We can do this by using the c and s parameter respectively of the scatter function. We can also show the color bar using the colorbar() method.

Example:

Python3




import pandas as pd
import matplotlib.pyplot as plt
 
 
# reading the database
data = pd.read_csv("tips.csv")
 
# Scatter plot with day against tip
plt.scatter(data['day'], data['tip'], c=data['size'],
            s=data['total_bill'])
 
# Adding Title to the Plot
plt.title("Scatter Plot")
 
# Setting the X and Y labels
plt.xlabel('Day')
plt.ylabel('Tip')
 
plt.colorbar()
 
plt.show()


Output:

Line Chart

Line Chart is used to represent a relationship between two data X and Y on a different axis. It is plotted using the plot() function. Let’s see the below example.

Example:

Python3




import pandas as pd
import matplotlib.pyplot as plt
 
 
# reading the database
data = pd.read_csv("tips.csv")
 
# Scatter plot with day against tip
plt.plot(data['tip'])
plt.plot(data['size'])
 
# Adding Title to the Plot
plt.title("Scatter Plot")
 
# Setting the X and Y labels
plt.xlabel('Day')
plt.ylabel('Tip')
 
plt.show()


Output:

Bar Chart

A bar plot or bar chart is a graph that represents the category of data with rectangular bars with lengths and heights that is proportional to the values which they represent. It can be created using the bar() method.

Example:

Python3




import pandas as pd
import matplotlib.pyplot as plt
 
 
# reading the database
data = pd.read_csv("tips.csv")
 
# Bar chart with day against tip
plt.bar(data['day'], data['tip'])
 
plt.title("Bar Chart")
 
# Setting the X and Y labels
plt.xlabel('Day')
plt.ylabel('Tip')
 
# Adding the legends
plt.show()


Output:

Histogram

A histogram is basically used to represent data in the form of some groups. It is a type of bar plot where the X-axis represents the bin ranges while the Y-axis gives information about frequency. The hist() function is used to compute and create a histogram. In histogram, if we pass categorical data then it will automatically compute the frequency of that data i.e. how often each value occurred.

Example:

Python3




import pandas as pd
import matplotlib.pyplot as plt
 
 
# reading the database
data = pd.read_csv("tips.csv")
 
# histogram of total_bills
plt.hist(data['total_bill'])
 
plt.title("Histogram")
 
# Adding the legends
plt.show()


Output:

Note: For complete Matplotlib Tutorial, refer Matplotlib Tutorial

Data Visualization with Python

In today’s world, a lot of data is being generated on a daily basis. And sometimes to analyze this data for certain trends, patterns may become difficult if the data is in its raw format. To overcome this data visualization comes into play. Data visualization provides a good, organized pictorial representation of the data which makes it easier to understand, observe, analyze. In this tutorial, we will discuss how to visualize data using Python.

Python provides various libraries that come with different features for visualizing data. All these libraries come with different features and can support various types of graphs. In this tutorial, we will be discussing four such libraries.

  • Matplotlib
  • Seaborn
  • Bokeh
  • Plotly

We will discuss these libraries one by one and will plot some most commonly used graphs. 

Note: If you want to learn in-depth information about these libraries you can follow their complete tutorial.

Before diving into these libraries, at first, we will need a database to plot the data. We will be using the tips database for this complete tutorial. Let’s discuss see a brief about this database.

Similar Reads

Database Used

Tips Database...

Matplotlib

...

Seaborn

Matplotlib is an easy-to-use, low-level data visualization library that is built on NumPy arrays. It consists of various plots like scatter plot, line plot, histogram, etc. Matplotlib provides a lot of flexibility....

Bokeh

...

Plotly

...

Conclusion

...