Data Visualization
Data Visualization
Data Visualization
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
plt.show()
Try this code:
import pandas as pd
import matplotlib.pyplot as plt
df=pd.DataFrame({'GeoArea':[83743,78438,22327,22429,21081,16579,10486],'ForestCover':[67353,27692,1
7280,17321,19240,13464,8073]},
index=['Arunachal Pradesh','Assam','Manipur','Meghalaya','Mizoram','Nagaland','Tripura'])
df.plot(kind='pie',y='ForestCover',title='Forest cover of North Eastern states',legend=False)
plt.show()
Plotting with PyPlot I –
Bar Graphs and Scatter Plots
Data Visualization basically refers to the graphical or visual
representation of information and data using visual elements
like charts, graphs, maps etc.
import matplotlib.pyplot as pl
You can easily plot the data available in the form of NumPy
arrays or dataframes etc.
Plotting with PyPlot I
Working with PyPlot methods
Try this code:
import numpy as np
import matplotlib.pyplot as pl
x=np.linspace(1,5,6)
y=np.log(x)
pl.plot(x,y)
pl.show()
Plotting with PyPlot I
Working with PyPlot methods
x=np.linspace(1,5,6)
Plotting with PyPlot I
Working with PyPlot methods
x=np.linspace(start=0, stop=100, num=5)
pl.bar(x,y)
pl.scatter(x,y)
Plotting with PyPlot I
Basics of simple plotting
Data visualization means graphical representation of
compiled data. Thus graphs and charts are very effective
tools for data visualization. You can create different types of
graphs and charts using PyPlot.
Plotting with PyPlot I
Basics of simple plotting
The line charts and scatter charts are similar. The only
difference is the presence/absence of the line connecting the
points.
Plotting with PyPlot I
Line chart using plot() function
a=[1,2,3,4]
b=[2,4,3,8]
import matplotlib.pyplot as pl
a=[1,2,3,4]
b=[2,4,3,8]
pl.plot(a,b) Plot the points a,b as x,y co-ordinates
pl.ylabel("Number of points scored")
pl.xlabel("Test Numbers") Display labels
pl.show() Display the chart on the screen
Plotting with PyPlot I
Applying various settings in plot() function:
import matplotlib.pyplot as pl
a=[1,2,3,4]
b=[2,4,3,8]
pl.bar(a,b,width=0.5,color='r')
pl.ylabel("Number of points scored")
pl.xlabel("Test Numbers")
pl.show()
Adding a title:
pl.title(“Tests Score Analysis”)
Only the data that falls into the limits of X and Y axes will be plotted, rest of the
data will not be plotted.
Customizing the plot:
Anatomy of a chart
Setting Ticks for axes:
By default, PyPlot will automatically decide which data points will
have ticks on the axes, but you can set it using the following
statement:
Syntax:
xticks(<sequence containing tick data points>,
[<optional sequence containing tick labels>])
import matplotlib.pyplot as pl
a=[1,2,3,4]
b=[2,4,3,8]
pl.bar(a,b,width=[0.5,0.6,0.7,0.8])
pl.title("Tests Score Analysis")
pl.ylabel("Number of points scored")
pl.xlabel("Test Numbers")
pl.xticks(a)
pl.show()
Customizing the plot:
Anatomy of a chart
Setting Ticks for axes:
import matplotlib.pyplot as pl
a=[1,2,3,4]
b=[2,4,3,8]
pl.bar(a,b,width=[0.5,0.6,0.7,0.8])
pl.title("Tests Score Analysis")
pl.ylabel("Number of points scored")
pl.xlabel("Test Numbers")
pl.xticks(a,['T1','T2','T3','T4'])
pl.show()
Question: CBSE Sample Paper 2019-20
Question: Solution
Steps to follow:
pl.savefig(“C:\\Data\\mychart.pdf”)
or
pl.savefig(“C:\\Data\\mychart.png”)
or
pl.savefig(“C:\\Data\\mychart.eps”)
Customizing the plot:
Saving a figure:
(i) Create bar charts to see the distribution of rainfall from Jan to May for all the
zones.
(ii) Create a line chart to observe any trends from Jan to May
Practical Question: Sample Output
Practical Question: Solution
x=np.arange(1,20,4)
pl.xlim(0,20)
pl.ylim(50,300)
pl.bar(x,jan,width=0.5,color='b',label="January")
pl.bar(x+0.5,feb,width=0.5,color='r',label="February")
pl.bar(x+1,mar,width=0.5,color='g',label="March")
pl.bar(x+1.5,apr,width=0.5,color='black',label="April")
import numpy as np pl.bar(x+2,may,width=0.5,color='pink',label="May")
import matplotlib.pyplot as pl pl.xticks(x,zones)
jan=[140,160,140,180,110] pl.title("Rainfall from Jan to May")
feb=[130,200,180,150,160] pl.xlabel("Zones")
mar=[130,130,150,200,130] pl.legend(loc='upper right')
apr=[190,200,170,120,110] pl.ylabel("Rainfall in mm")
may=[160,200,190,180,120] pl.show()
zones=["North","South","East","West","Central"]
Practical Question: Sample Output – Line Chart
Practical Question: Solution – Line Chart
import numpy as np
import matplotlib.pyplot as pl pl.xticks(x,zones)
jan=[140,160,140,180,110] pl.title("Rainfall from Jan to May")
feb=[130,200,180,150,160] pl.xlabel("Zones")
mar=[130,130,150,200,130] pl.legend(loc='upper right')
apr=[190,200,170,120,110] pl.ylabel("Rainfall in mm")
may=[160,200,190,180,120] pl.show()
zones=["North","South","East","West","Central"]
x=np.arange(1,20,4)
pl.xlim(0,20)
pl.ylim(50,300)
pl.plot(x,jan,color='b',linestyle="dashed",marker="d",markersize=10,label="January")
pl.plot(x,feb,color='r',linestyle="dashed",marker="d",markersize=10,label="February")
pl.plot(x,mar,color='g',linestyle="dashed",marker="d",markersize=10,label="March")
pl.plot(x,apr,color='black',linestyle="dashed",marker="d",markersize=10,label="April")
pl.plot(x,may,color='pink',linestyle="dashed",marker="d",markersize=10,label="May")
Practical Question 2:
CBSE Result Analysis – 2018-19
Pie Chart
import matplotlib.pyplot as plt
items = ['Cookies', 'Jellybean', 'Milkshake', 'Cheesecake']
data = [38.4, 40.6, 20.7, 10.3]
cols = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
exp=[0,0.1,0,0]
plt.pie(data, labels=items,colors=cols, shadow=True, startangle=90, autopct='%.2f%%',
explode=exp)
plt.legend(loc="lower left")
plt.axis('equal') #sets the aspect ratio so that the data units are the same in every direction.
plt.tight_layout() #to fit plots within the figure cleanly.
plt.show()
Practical Question 3:
Pie chart from data collected from data.gov.in
Practical Question 3:
Pie chart from data collected from data.gov.in
Practical Question 3:
Pie chart from data collected from data.gov.in
import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv("C:\\Data\\Agricultural Land.csv")
print(df)
data=df['Agricultural Land'].head(6)
states=df['States/UTs'].head(6)
cols=['m','c','g','b','r','gold']
exp=[0,0,0,0,0.1,0]
plt.pie(data, labels=states,colors=cols, shadow=True, startangle=90, autopct='%.2f%%', explode=exp)
plt.title(“State wise Agricultural Land”)
plt.legend(loc="lower left")
plt.axis('equal') #sets the aspect ratio so that the data units are the same in every direction.
plt.tight_layout() #to fit plots within the figure cleanly.
plt.show()