DEV Community

mary kariuki
mary kariuki

Posted on

visualization of data using matplotlib and seaborn

Visualization of data.

Data visualization is the graphical representation of data.
Matplotlib is a python library used in plotting of graphs with other modules such such pandas and numpy while seaborn is also
a python library used for plotting graph with help ofother libararies like matplotlib,numpy and pandas.
The difference between seaborn and matplotlib is that,seaborn
complies the entire data into a single plot while matplotlib is
used in plotting 2-D graphs of arrays.

  1. Matplotlib

The first thing is to install matplotlib that uses a simple command

pip install matplotlib
Enter fullscreen mode Exit fullscreen mode

After matplotlib has being installed you have to import the matplotlib module as shown below

import matplotlib.pyplot as plt
Enter fullscreen mode Exit fullscreen mode

note: plt is an alias.
Matplotlib is used in plotting varoius graphs such as

  • bar graphs

  • histograms

  • pie charts

  • scatter plots

Scatter plot

to draw a scatter plot we use the SCATTER() method which draws one dot for each value.To plot a scatter function one should have two values that is the x-axis values and y-axis values.

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([3,4,5,7,1,0,5,8,6,4])
ypoints = np.array([70,20,70,30,50,90,55,49,34,28])

plt.scatter(xpoints, ypoints)
plt.show()
Enter fullscreen mode Exit fullscreen mode

Bar graphs

when drawing a bar graph we use the BAR() method to create bar graphs and provide the x-axis and y-axis values.

import matplotlib.pyplot as plt
import numpy as np

xvalues = np.array(["mary", "anne", "simon", "james"])
yvalues = np.array([90,10,50,70])

plt.bar(xvalues,yvalues)
plt.show()
Enter fullscreen mode Exit fullscreen mode

Histogram

A histogram is a graph that shows frequency distribution.
We use the HIST() method to create histograms, which uses arrays of numbers where the hist function reads the array and provide a histogram.

import matplotlib.pyplot as plt
import numpy as np

y = np.random.normal(20, 40, 500)

plt.hist(y)
plt.show()
Enter fullscreen mode Exit fullscreen mode

Piechart

We use the pie() method to create pie charts.

import matplotlib.pyplot as plt
import numpy as np

z = np.array([10,30,5,60,59,70,2])

plt.pie(z)
plt.show() 
Enter fullscreen mode Exit fullscreen mode

The pie chart is subdivided in 7parts since we have passed 7elements in the array.

  1. Seaborn

To use seaborn module you will first install as shown.

pip install seaborn
Enter fullscreen mode Exit fullscreen mode

after installing you now import the matplotlib and seaborn since they go hand in hand.

import matplotlib.pyplot as plt 
Enter fullscreen mode Exit fullscreen mode
import seaborn as sns
Enter fullscreen mode Exit fullscreen mode

Seaborn is used in statistical graphics in python now lets load our data.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df=sns.load_dataset("data")
df
Enter fullscreen mode Exit fullscreen mode

Out of our data we can have a single plot that describes
the entire data.

import matplotlib.pyplot as plt
import seaborn as sns
sns.pairplot(df)
Enter fullscreen mode Exit fullscreen mode

Note:pairplot > allows us to plot pairwise relationships between variables within a dataset.

Distplot in seaborn
Distplot stands for distribution plot it takes as input an array and plots a curve corresponding to the distribution of points in the array.

import matplotlib.pyplot as plt
import seaborn as sns

sns.distplot([2,4,6,8,10])

plt.show() 
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
hkmburu profile image
HK MBURU

genius

Collapse
 
andrea189699 profile image
Andrea189699

Hi Mary, this is awesome. Matplotlib and seaborn are very useful modules in data analysis.
Machine learning 🔥🔥

Collapse
 
marykariuki90 profile image
mary kariuki

Thank you so much