How To Create Different Types Of Plots And Charts In Data Visualization Using Python

Python has incredible support for libraries required for data visualization and data analysis. Pandas, Numpy, and Matplotlib ensure the creation of nearly all types of required visualization charts. This article aims to provide various ways of creating charts and plots in python which may be utilized to understand certain types of analysis or to explain some data patterns. In this article, we will learn to create the following,

  • Line Plot
  • Box Plot
  • Scatter Plot
  • Histogram
  • Heat Maps

Line Plot

A line plot is one of the basic types of plot in Matplotlib. Line plots enable us to display the values on one axis and the categorical values on the other. To create a line plot we will first import the pyplot as plt from matplotlib, and then we will define the values of X and Y. After with the defined values, we will create the plot with plt.plot, and then to display we will use plt.show.

from matplotlib import pyplot as plt

# x-axis values
x = [4, 8, 6, 1, 3]

# Y-axis values
y = [5, 8, 3, 5, 8]

# Function to plot
plt.plot(x,y)

# function 
plt.show()

Output

Line Plot Output

Box Plot

Boxplot enables to understanding and measurement of how well the data which is distributed in a dataset is, as it divides the data into three quartiles. The graph is also useful in comparing the distribution of data across datasets by drawing boxplots for each of them.

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(20,5), columns=['A','B','C','D','E'])
df.plot.box(grid="True")

Output

Scatter Plot

A Scatter plot can be defined as a diagram where each value in the data set is represented by a dot. The Matplotlib enables the creation of a Scatter plot too. To create a Scatter plot the very first important step is to import the pyplot, then define the values of X and Y, and then create the scatter as plt.scatter and then display as plt.show.

from matplotlib import pyplot as plt

# x-axis values
x = [74, 88, 52, 72, 78]

# Y-axis values
y = [23, 22, 54, 69, 80]

# Function to plot scatter
plt.scatter(x, y)

# function to show the plot
plt.show()

Output

Histogram

A graph showing the frequency of distribution, or the number of observations with each given interval can be defined as a histogram. Matplotlib enables the creation of the histogram as shown below. It is an estimate of the probability distribution of a continuous variable.

from matplotlib import pyplot as plt

# Y-axis values
y = [1,15,19,40,65]

# Function to plot histogram
plt.hist(y)

# Function to show the plot
plt.show()

Output

HeatMaps

A heat map is a representation of the data, where the data values are depicted by color, the variation of the color may be by hue or intensity. There are usually two types of heatmaps:

  • Spatial Heat Map
  • Grid Heat Map
from pandas import DataFrame
import matplotlib.pyplot as plt

data=[{8,9,3,4},{4,6,9,1},{7,4,1,3},{9,5,1,3},{7,1,3,9}]
Index= ['I1', 'I2','I3','I4','I5']
Cols = ['C1', 'C2', 'C3','C4']
df = DataFrame(data, index=Index, columns=Cols)

plt.pcolor(df)
plt.show()

Output


Similar Articles