Unit 4 Plotting Final
Unit 4 Plotting Final
Data Visulization:
Index
CHAPTER 10: Visualizing Information:
Starting with a Graph, Defining the plot, Drawing multiple lines
and plots, Saving your work to disk, Setting the Axis, Ticks,
Grids, Getting the axes, Formatting the axes, Adding grids,
Defining the Line Appearance, Working with line style, Using
colors, Adding markers, Using Labels, Annotations, and Legends,
Adding labels, Annotating the chart, Creating a legend.
CHAPTER 11: Visualizing the Data:
Choosing the Right Graph, Showing parts of a whole with
pie charts, Creating comparisons with bar charts, Showing
distributions using histograms, Depicting groups using
boxplots, Seeing data patterns using scatterplots, Creating
Advanced Scatterplots, Depicting groups, Showing
correlations, Plotting Time Series, Representing time on axes,
Plotting trends over time, Plotting Geographical Data, Using
an environment in Notebook, Getting the Basemap toolkit,
Dealing with deprecated library issues, Using Basemap to
plot geographic data, Visualizing Graphs, Developing
undirected graphs, Developing directed graphs.
Introduction-Data Visulization:
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.
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.
Matplotlib
Seaborn
Bokeh
Plotly
Matplotlib -INTRODUCTION
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.
Matplotlib is a low level graph plotting library in python that serves
as a visualization utility.
Matplotlib was created by John D. Hunter.
Matplotlib is open source and we can use it freely.
Matplotlib is mostly written in python, a few segments are written
in C, Objective-C and Javascript for Platform compatibility.
• Pylab example:-
import pylab
Or
Or
from matplotlib.pyplot
import matplotlib.pyplot as plt import *
import numpy as np From numpy import *
Or
from pylab import *
Types of Plot in Python using PyPlot and Pylab
module.
• There are several types of plots we can generate using matplolib
like:-
1)Line Plot
2)Scatter Plot
3)Bar and Histogram Plot
4)Image Plot
5)Contour Plot
6)Polar Plot
7)3D Plot
8)Pie-chart Plot etc.
Various Functions used for Plotting
• plot()
Given a single list or array, plot() assumes it’s a vector of y-values.
Automatically generates an x vector of the same length with consecutive integers beginning
with 0.
Here [0,1,2,3]
To override default behavior, supply the x data: plot(x,y )
where x and y have equal lengths
• show()
Should be called at most once per script
Last line of the script
Then the GUI takes control, rendering the figure.
Cont.
• Savefig()
We can also save output to supported formats: emf, eps, pdf,
png, ps, raw, rgba, svg, svgz
If no extension specified, defaults to .png.
• draw()
Clear the current figure and initialize a blank figure without hanging or displaying anything.
• close()
To dismiss the figure and clear it.
• axis()
Example 2: Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints, 'o')
plt.show()
Example 4: Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and
finally to position (8, 10):
# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]
# line 1 points
x1 = [1,2,3]
y1 = [2,4,1]
# plotting the line 1 points
plt.plot(x1, y1, label = "line 1")
# line 2 points
x2 = [1,2,3]
y2 = [4,1,3]
# plotting the line 2 points
plt.plot(x2, y2, label = "line 2")
# x axis values
x = [1,2,3,4,5,6]
# corresponding y axis values
y = [2,4,1,5,2,6]
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y=
np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x, y)
plt.grid(axis = 'x')
plt.show()
Example 12 : Specify Which Grid Lines to Display
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x, y)
plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)
plt.show()
Different Matplotlib Linestyle /Marker in
Python
Character Definition Character Definition
b blue
g green
r red
c cyan
m magenta
y yellow
k black
w white
Annotating Chart
import matplotlib.pyplot as plt
values=[1,5,8,9,2,0,3,10,4,7]
plt.annotate(xy=[1,1],s="First Entry")
plt.plot(range(1,11),values)
plt.show()
CHAPTER 11: Visualizing the Data:
# heights of bars
height = [10, 24, 36, 40, 5]
# frequencies
ages = [2,5,70,40,30,45,50,45,43,40,44,
60,7,13,57,18,90,77,32,21,20,40]
# plotting a histogram
plt.hist(ages, bins, range, color = 'green',
histtype = 'bar', rwidth = 0.8)
# x-axis label
plt.xlabel('age')
# frequency label
plt.ylabel('No. of people')
# plot title
plt.title('My histogram')
# x-axis values
x = [1,2,3,4,5,6,7,8,9,10]
# y-axis values
y = [2,4,5,7,6,8,9,11,12,12]
# x-axis label
plt.xlabel('x - axis')
# frequency label
plt.ylabel('y - axis')
# plot title
plt.title('My scatter plot!')
# showing legend
plt.legend()
# defining labels
activities = ['eat', 'sleep', 'work', 'play']
# plotting legend
plt.legend()
# Creating dataset
np.random.seed(10)
# Creating plot
bp = ax.boxplot(data)
# show plot
plt.show()
Plotting Time Series
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
import numpy as np
start_date=dt.datetime(2022,7,1)
end_date=dt.datetime(2022,8,1)
daterange=pd.date_range(start_date,end_date) sales
2022-07-01 37
sales=(np.random.rand(len(daterange))*50).astype(int) 2022-07-02 47
2022-07-03 30
df=pd.DataFrame(sales,index=daterange,columns=['sales']) 2022-07-04 15
2022-07-05 5
print(df) 2022-07-06 3
df.loc['July 01 2022':"Aug 01 2022"].plot() 2022-07-07 15
2022-07-08 33
plt.ylim(0,50) 2022-07-09 34
2022-07-10 10
plt.xlabel("sales Date") 2022-07-11 4
2022-07-12 15
plt.ylabel("Sales value") …..
2022-07-30 18
plt.title("Plotting times") 2022-07-31 14
plt.show() 2022-08-01 22
Plotting Trends over Time
import matplotlib.pyplot as plt
import datetime as dt
import numpy as np
start_date=dt.datetime(2022,7,1)
end_date=dt.datetime(2022,8,1)
daterange=pd.date_range(start_date,end_date)
sales=(np.random.rand(len(daterange))*50).astype(int)
df=pd.DataFrame(sales,index=daterange,columns=['sales'])
#In python, Numpy polyfit() is a method that fits the data within a
#polynomial function.
lr_coef=np.polyfit(range(0,len(df)),df['sales'],1)
#The numpy. poly1d() function helps to define a polynomial function
lr_func=np.poly1d(lr_coef)
trend=lr_func(range(0,len(df)))
df['trend']=trend sales trend
print(df) 2022-07-01 48 27.414773
df.loc['July 01 2022':"Aug 01 2022"].plot() 2022-07-02 23 27.210594
plt.ylim(0,50) 2022-07-03 38 27.006415
2022-07-04 24 26.802236
plt.xlabel("sales Date")
2022-07-05 33 26.598057
plt.ylabel("Sales value")
2022-07-06 11 26.393878
plt.title("Plotting times") 2022-07-07 5 26.189699
plt.legend(['sales,trend']) 2022-07-08 48 25.985521
plt.show() ….
2022-07-29 5 21.697764
2022-07-30 9 21.493585
2022-07-31 38 21.289406
2022-08-01 35 21.085227
Visualizing Graph – Undirected graph
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
H=nx.Graph()
G.add_node(1)
G.add_nodes_from([2,3])
G.add_nodes_from(range(4,7))
H.add_node(7)
G.add_nodes_from(H)
G.add_edge(1,2)
G.add_edge(1,1)
G.add_edges_from([(2,3),(3,6),(4,6),(5,6)])
H.add_edges_from([(4,7),(5,7),(6,7)])
G.add_edges_from(H.edges())
nx.draw_networkx(G)
plt.show()
Visualizing Graph –directed graph
import networkx as nx
import matplotlib.pyplot as plt
G=nx.DigGraph()
G.add_node(1)
G.add_nodes_from([2,3])
G.add_nodes_from(range(4,6))
G.add_path([6,7,8])
G.add_edge(1,2)
G.add_edge(1,1)
G.add_edges_from([(1,4),(4,5),(2,3),(3,6),(5,6)])
nx.draw_networkx(G)
plt.show()
Program 5: plotting curve equation
# importing the required modules
import matplotlib.pyplot as plt
import numpy as np