0% found this document useful (0 votes)
24 views30 pages

Plotting Graph10072019

Chapter 3 discusses data visualization using Python libraries matplotlib and seaborn, highlighting their features and capabilities for creating various types of graphs. It provides examples of line graphs, histograms, bar charts, and pie charts, along with code snippets for implementation. The chapter also explains the differences between histograms and bar charts, emphasizing their respective uses in data representation.

Uploaded by

ghhg78150
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views30 pages

Plotting Graph10072019

Chapter 3 discusses data visualization using Python libraries matplotlib and seaborn, highlighting their features and capabilities for creating various types of graphs. It provides examples of line graphs, histograms, bar charts, and pie charts, along with code snippets for implementation. The chapter also explains the differences between histograms and bar charts, emphasizing their respective uses in data representation.

Uploaded by

ghhg78150
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

CHAPTER-3

DATA VISUALIZATION USING PYPLOT


Introduction
There are multiple tools for performing
visualization in data science. In Python we can
use two exclusive libraries for visualization,
commonly known as matplotlib and seaborn.
Matplotlib
Python based plotting library offers
matplotlib with a complete 2D support
along with limited 3D graphic support. It is
useful in producing publication quality
figures in interactive environment across
platforms. It can also be used for
animations as well.
Seaborn
Seaborn is a library for creating informative and
attractive statistical graphics in python. This
library is based on matplotlib. Seaborn offers
various features such as built in themes, color
palettes, functions and tools to visualize kartik
seengh, bivariate, linear regression, matrices of
data, statistical time series etc which lets us to
build complex visualizations.
Setup installation steps
Open cmd and go to directory by using following command:
Checking install version of matplotlib
C:\Python27\Scripts>pip show matplotlib
GRAPH
Example-1
Here's some basic code to generating one of the most
simple graphs that we can, it will take us only 3 lines.
#Importing pyplot
from matplotlib import pyplot as plt
#Plotting to our canvas plt.plot([1,2,3],[4,5,1])
#Showing what we plotted plt.show()
Example-2
from matplotlib import pyplot as plt

x = [5,8,10]

y=[12,16,6]

plt.plot(x,y)
plt.title('Epic Info')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
Line Graph
A line chart or line graph is a type of chart which
displays information as a series of data points called
‘markers’ connected by straight line segments.
Line graphs are usually used to find relationship
between two data sets on different axis; for instance X,
Y.
Example-Line Graph
Population dataset of India and Pakistan
from matplotlib import pyplot as plt year = [1960, 1970, 1980, 1990, 2000,
2010] pop_pakistan = [44.91, 58.09, 78.07, 107.7, 138.5, 170.6] pop_india
= [449.48, 553.57, 696.783, 870.133, 1000.4, 1309.1]

plt.plot(year, pop_pakistan, color='g')

plt.plot(year, pop_india, color='orange')


plt.xlabel('Countries') plt.ylabel('Population in million')
plt.title('Pakistan India Population till 2010')
plt.show()
From the above source we got the data of both X and Y axis. year will
be on x-axis where population of both countries on Y axis. Since I need
two different lines so plot was called twice. The color attribute used to
assign the color of the line.

xlabel and ylabel are being used to give some friendly name to the
axises and finally .title() for giving the name of the entire graph. When
runs it will appear like given below:
HISTOGRAM
A histogram is a powerful technique in data visualization. It is an
accurate graphical representation of the distribution of numerical
data. It was first introduced by Karl Pearson. It is an estimate of the
distribution of a continuous variable (quantitative variable). It is
similar to bar graph. To construct a histogram, the first step is to
“bin” the range of values — means divide the entire range of values
into a series of intervals — and then count how many values fall into
each interval. The bins are usually specified as consecutive,
nonoverlapping intervals of a variable. The bins (intervals) must be
adjacent, and are often (but are not required to be) of equal size.
Components of Histogram
Title –To display heading of the histogram.

Color – To show the color of the bar.

Axis: y-axis and x-axis.

Data: The data can be represented as an array.

Height and width of bars. This is determined based on the analysis. The
width of the bar is called bin or intervals.

Border color –To display border color of the bar.

Histogram in Python –
There are various ways to create histogram in python
pandas.One of them is using matplotlib python
Example
import numpy as np
import matplotlib.pyplot
as plt data =
[1,11,21,31,41]
plt.hist([5,15,25,35,45, 55], bins=[0,10,20,30,40,50, 60],
weights=[20,10,45,33,6,8], edgecolor="red") plt.show()

#first argument of hist() method is


position (x,y Coordinate) of weight, where
weight is to be displayed. No of
coordinates must match with No of weight
otherwise error will generate
#Second argument is interval
#Third argument is weight for bars
Histogram inPython –
For better understanding we can develop the same program with
minor change .
import numpy as np
import matplotlib.pyplot as
plt data = [1,11,21,31,41]
plt.hist([5,15,25,35,15, 55], bins=[0,10,20,30,40,50, 60],
weights=[20,10,45,33,6,8], edgecolor="red") plt.show()
# at interval(bin)40 to 50 no bar because we
have not mentioned position from 40 to 50 in
first argument(list) of hist method.
Where as in interval 10 to 20 width is being
Displayed as 16 (10+6 both weights are added)
because 15 is twice In first argument.
Bar Chart
Another very popular chart on matplotlib is the bar chart. For the
bar chart we will use the bar() function, where we define the
position of the bars on the X axis and their height on the Y axis.
Additionally, we can also configure another characteristics for the
chart, like width of the bars, color, among others. The X axis will
be a range with the same quantity of items as the Y axis. Let’s see
a simple example, where we will store the configurations we
want in variables and then we will pass them to the bar()
function:
Example-Bar Chart
import matplotlib.pyplot as plt

# Variables for the bar chart

y_axis = [20,50,30]
x_axis = range(len(y_axis))
plt.bar(x_axis, y_axis, width=.5, color='orange')
plt.show()
Difference between a Histogram and a Bar chart / graph
A bar chart majorly represents categorical data (data that has some labels
associated with it), they are usually represented using rectangular bars with
lengths proportional to the values that they represent.

While histograms on the other hand, is used to describe


distributions. Given a set of data, what are their distributions
Pie Chart
import matplotlib.pyplot as plt

# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java' sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue'] explode = (0.1, 0, 0, 0) #
explode 1st slice

# Plot
plt.pie(sizes, explode=explode, labels=labels, colors=colors, topct='%1.1f%%',
shadow=True, startangle=140)

plt.axis('equal')

plt.show()
THANKS

You might also like