Unit I, Ploting with Pyplot › 2019 › 07 … · data = [1,11,21,31,41] plt.hist(data, bins=20,...

6
V . Khatri, PGT CS, KV1 Jammu Page 1 | 6 Unit – I, Ploting with Pyplot VISUALISATION : is the representation of an object, situation, or set of information as a chart or other image (the formation of a mental image of something.) In Python we can use two exclusive libraries for visualization, commonly known as matplotlib and seaborn. Matplotlib is the whole python package/ library used to create 2D graphs and plots by using python scripts. pyplot is a module in matplotlib, which supports a very wide variety of graphs and plots namely - histogram, bar charts, power spectra, error charts etc. It is used along with NumPy to provide an environment for MatLab. Seaborn: Seaborn is a library for creating informative and attractive statistical graphics in python. This library is based on matplotlib. Seaborn offers various features How to install Matplotlib Python shell must be installed in your System Open command prompt as an Administrator Go back to root directory by typing cd\ Now type pip install matplotlib 1. LINE GRAPH : 1. A line Chart/Plot/Graph is a type of chart which displays information as a series of data points called 'markers' connected by straight line segments. import matplotlib.pyplot as p Yr=[2000,2002,2004,2006] rate=[21.0,20.7,21.2,21.6] p.plot(Yr,rate, lablel=’first line’) p.title('Epic Info') # This will show title of the Chart p.ylabel('Y axis') # This message will be shown on Y Axis p.xlabel('X axis') # This message will be shown on X Axis

Transcript of Unit I, Ploting with Pyplot › 2019 › 07 … · data = [1,11,21,31,41] plt.hist(data, bins=20,...

Page 1: Unit I, Ploting with Pyplot › 2019 › 07 … · data = [1,11,21,31,41] plt.hist(data, bins=20, histtype='step') # Histtype command shows the histogram in steps as shown in above

V . K h a t r i , P G T C S , K V 1 J a m m u P a g e 1 | 6

Unit – I, Ploting with Pyplot VISUALISATION : is the representation of an object, situation, or set of information as a

chart or other image (the formation of a mental image of something.) In Python we can use

two exclusive libraries for visualization, commonly known as matplotlib and seaborn.

Matplotlib is the whole python package/ library used to create 2D graphs and plots by using

python scripts. pyplot is a module in matplotlib, which supports a very wide variety of graphs

and plots namely - histogram, bar charts, power spectra, error charts etc. It is used along

with NumPy to provide an environment for MatLab.

Seaborn: Seaborn is a library for creating informative and attractive statistical graphics in

python. This library is based on matplotlib. Seaborn offers various features

How to install Matplotlib

Python shell must be installed in your System Open command prompt as an

Administrator Go back to root directory by typing cd\ Now type pip install matplotlib

1. LINE GRAPH :

1. A line Chart/Plot/Graph is a type of chart which displays information as a series of data

points called 'markers' connected by straight line segments.

import matplotlib.pyplot as p

Yr=[2000,2002,2004,2006]

rate=[21.0,20.7,21.2,21.6]

p.plot(Yr,rate, lablel=’first line’)

p.title('Epic Info') # This will show title of the Chart

p.ylabel('Y axis') # This message will be shown on Y Axis

p.xlabel('X axis') # This message will be shown on X Axis

Page 2: Unit I, Ploting with Pyplot › 2019 › 07 … · data = [1,11,21,31,41] plt.hist(data, bins=20, histtype='step') # Histtype command shows the histogram in steps as shown in above

V . K h a t r i , P G T C S , K V 1 J a m m u P a g e 2 | 6

p.show()

p.legend() # it will show legend on the Chart, showing all lines.

You can change Line Style by specifying pattern with plot function.

FOR EXAMPLE p.plot(Yr, ‘—‘) # To draw dashed line p.plot(Yr,’-.’) # To draw dash-dot line

p.plot(Yr,rate,color='r') # To draw line in red colour

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')

# This will also show two Line Graph of in colour green and orange.

p.plot(Yr,rate, marker=’d’, markersize=5,

markeredgecolor=’g’)

# here diamond marker will be shown on points value

also with size =5

Multiple Plots by Subplot() :

This function has three argument, number of rows,

number of columns and figure number.

p.subplot(2,1,1)

# This will show 2 Rows of Graphs, with 1 column showing figure number

p.plot(x,y)

p.subplot(2,1,2)

p.plot(x,y)

p.subplots_adjust(hspace=.4, vspce=.4)

# it will put space between graphs

y= np.sin(x) # y will take sin value of x.

Various colour abbreviation: b= blue, c= cyan,

m=magenta, w = white, k= black etc.

Line Style abbreviation: - = solid line, - - = dashed line, -. = dashed dot line etc.

BAR GRAPH:

A bar graph / bar chart/ bar diagram is a visual tool that uses bars(Vertical or Horizontal) to

represent/compare categorical data.

p.bar(Yr,rate,color='blue', width=.50) # it will create a bar graph with colour blue and width

of bar is .50

We can draw grids also with the chart by adding

p.grid(True) #with the above coding, it will show grids on bar graph

Page 3: Unit I, Ploting with Pyplot › 2019 › 07 … · data = [1,11,21,31,41] plt.hist(data, bins=20, histtype='step') # Histtype command shows the histogram in steps as shown in above

V . K h a t r i , P G T C S , K V 1 J a m m u P a g e 3 | 6

If we want to plot Horizontal bar graph for the Same (above) question then we use barh()

Function and we have to interchange x and y labels as shown below

p.bar(Yr,rate,color='blue', width=.50)

Pie chart:

import matplotlib.pyplot as plt

l = ['Python', 'C++', 'Ruby', 'Java']

s = [215, 130, 245, 210]

c= ['gold', 'yellowgreen', 'lightcoral',

'lightskyblue']

explode = (0.1, 0, 0, 0)

plt.pie(s, explode=explode, labels=l, colors=c, shadow=True, startangle=140)

plt.show()

Scatter Chart:

x = np.linspace(0, 10, 30) y = np.sin(x) plt.plot(x, y, marker='o', color='black');

Page 4: Unit I, Ploting with Pyplot › 2019 › 07 … · data = [1,11,21,31,41] plt.hist(data, bins=20, histtype='step') # Histtype command shows the histogram in steps as shown in above

V . K h a t r i , P G T C S , K V 1 J a m m u P a g e 4 | 6

# Here marker shows sign of symbolsof chart Various Marker values are specified as : S= square, o=circle, d= diamond etc.

Frequency Polygon : If we just connect the top center points of each bins then we obtain relative frequency polygon

Frequency polygon is shown by creating Histogram like this :

data = [1,11,21,31,41]

plt.hist(data, bins=20, histtype='step')

# Histtype command shows the histogram in steps as shown in above figure no 2

Box Plots : Boxplots are a measure of how well distributed the data in a data set is. It

divides the data set into three quartiles. A Box Plot is also the visual representation of the statistical five number summary of a given data set. It is also useful in comparing the distribution of data across data sets by drawing boxplots for each of them. A Five Number Summary includes: •Minimum

•First Quartile

•Median (Second Quartile)

•Third Quartile

•Maximum Program coding for boxplot is : import matplotlib.pyplot as plt value1 = [82,76,24,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52] value2=[62,5,91,25,36,32,96,95,3,90,95,32,27,55,100,15,71,11,37,21] value3=[23,89,12,78,72,89,25,69,68,86,19,49,15,16,16,75,65,31,25,52] value4=[59,73,70,16,81,61,88,98,10,87,29,72,16,23,72,88,78,99,75,30] plt.boxplot(value1,value2,value3,value4,patch_artist=True,labels=['course1','course2','course3','course4']) plt.show()

Page 5: Unit I, Ploting with Pyplot › 2019 › 07 … · data = [1,11,21,31,41] plt.hist(data, bins=20, histtype='step') # Histtype command shows the histogram in steps as shown in above

V . K h a t r i , P G T C S , K V 1 J a m m u P a g e 5 | 6

boxplot() function takes the data array to be plotted as input in first argument, second argument patch_artist=True , fills the boxplot and third argument takes

the label to be plotted. If we give commmand like this :

plt.boxplot(box_plot_data,notch='True',patch_artist=True,vert=

True, showbox=True, showmen=True

labels=['course1','course2','course3','course4'])

second argument notch=‘True’ creates the notch format of the box plot. Vert= True shows boxplot vertically, if it is false then everything will show horizontally.

If show box= False then central box on the boxplot will not be seen. If show mean=False then symbol for mean point will not be seen.

Page 6: Unit I, Ploting with Pyplot › 2019 › 07 … · data = [1,11,21,31,41] plt.hist(data, bins=20, histtype='step') # Histtype command shows the histogram in steps as shown in above

V . K h a t r i , P G T C S , K V 1 J a m m u P a g e 6 | 6

value1 = [82,76,24,40,67,62,75,78,71,32,98,89,78,67,72,82,87,66,56,52] In this Example first of all computer will arrange them in ascending order as Value 1=[24,32,40, 52,56,62,66, 67,67,71, 72, 75, 76, 78, 78, 82, 82, 87, 89, 98] Here Minimum and Maximum values are 24 and 98 Q1 is 1ST Quartile (25th Percentile) =.4(20+1) =8.4, round of its is 8, it means 67 is first quantile Q2 is 2nd Quartile (50th Percentile) also called Median = .5(21)=10.5, round of its is 11, it means item is 72, Q3 is 3rd Quartile (75th Percentile)= .75(21)= 15.75, round of it is 16 and the 16th item is 82 Middle half of the data is represented by box, the highest and lowest scores are joined to the box by straight line

Unsolved Questions of Sumita Arora

Ans 1. (a) pl.hist(x, bins=16) (b) pl.hist(x, orientation=’horizontal’) (c) pl.hist(x, histtype=step) (d) pl.hist(x, commulative=’True’) Ans2. (a) a=np.arrange(16), pl.hist(a) (b) pl.hist(x, commulative=’True’) (c) pl.hist(a, orientation=’horizontal’) Ans3. pl.hist(x, histtype=step) Ans 5. (a) plt.boxplot(a) (b) plt.boxplot(a,vert=True) (c) plt.boxplot(a,showmeans=True) (d) plt.boxplot(a, showbox=’False’)