visualization of data using matplotlib and seaborn
mary kariuki
Posted on October 22, 2022
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.
- Matplotlib
The first thing is to install matplotlib that uses a simple command
pip install matplotlib
After matplotlib has being installed you have to import the matplotlib module as shown below
import matplotlib.pyplot as plt
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()
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()
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()
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()
The pie chart is subdivided in 7parts since we have passed 7elements in the array.
- Seaborn
To use seaborn module you will first install as shown.
pip install seaborn
after installing you now import the matplotlib and seaborn since they go hand in hand.
import matplotlib.pyplot as plt
import seaborn as sns
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
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)
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()
Posted on October 22, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024