Tuesday 16 February 2021

3.1 Data Visualization using matplotlib in Python

 

Data Visualization using matplotlib in Python

LINE CHART

Data Visualization:

                Data Visualization refers to the graphical or visual representation of information and data using visual elements like charts, graphs, maps etc.

 

PyPlot:

                PyPlot is a collection of methods within matplotlib library of Python, Which allow user to create 2D Plots easily. Matplotlib is a Python library.

To install matplotlib use the following command.

Pip install matplotlib

    Type of basic charts of PyPlot

1.       Line Chart: A line chart is created using plot() method.

2.       Bar Chart: A vertical bar chart is created using bar() method and horizontal bar chart using barh() method.

3.       Histogram Plot: A histogram is created using hist() method

4.       Pie Chart: A pie chart is created using pie() method.

5.       Scatter Plot: A scatter plot is created using scatter() method.


 

import matplotlib.pyplot as plt

x=[1,2,3,4,5]

y=[2,3,4,5,6]

plt.plot(x,y)        #draw a line chart

plt.xlabel(“x axis values”)

plt.ylabel(“y axis values”)

plt.legend()        #show the legend

plt.show()           #show the chart/plot as per given specification

 

 

import matplotlib.pyplot as plt

x=[1,2,3,4,5]

y=[1,2,3,4,5]

plt.plot(x,y)

plt.show()

 

 

 

import matplotlib.pyplot as plt

x=["Ram","Raj","Sam","john","Amit"]

y=[30,20,25,40,35]

plt.plot(x,y)

plt.title("Marks Sheet")

plt.xlabel("Student's Name")

plt.ylabel("Marks")

plt.show()

 

 

 

import matplotlib.pyplot as plt

x=[2,3,4,5,6,7]

y=[1,2,3,4,5,6]

plt.plot(x,y)

plt.show()

 

 

import matplotlib.pyplot as plt

x=["VII","VIII","IX","X"]

y=[40,50,35,45]

plt.bar(x,y)

plt.show()

 

Specifying Plot Size and Grid

 

plt.figure(figsize=(width,length))

 

plt.grid(True)

 

Changing Line Color and Style

 

Plt.plot(x,y,color_code)

 

 

Color Code

 

‘r’ red

‘g’ green

‘b’ blue

‘m’ magenta

‘y’ yellow

‘k’ black

‘c’ cyan

‘w’ white

 

 

Change Line Width

 

linewidth=width

plt.plot(x,y,linewidth=2)

 

Line Style

 

linestyle or ls =[‘solid’ | ‘dashed’ | ‘dashdot’ | ‘dotted ]

 

ls=’:’ | ‘-‘  | ‘_ _’ | ‘-‘

 

plt.plot(x,y,linewidth=4,linestyle=’dashed’)

 

 

Assignment - 1

 

1.       WAP to plot a line chart to depict the changing weekly onion prices for four weeks. Give appropriate axis labels.

 

2.       Marks is a list that stores marks of a student in 10 unit tests. Write a program to plot the student’s performance in these 10 unit tests.

 

3.       Ram is doing some research. He has a stored line of pascal’s triangle numbers as ar2 as shown below:

ar2 = [1,7,21,35,21,7,1]

·         He want to plot the sine, cosine, and tangent values for the same array.

·         He wants cyan color for sine line, red color for cosine line and black color for tangent line

·         Tangent line should be dashed. 


Changing Marker Type, Size and Color

 

The data points being plotted on a graph/chart are called markers.

 

marker=<marker_type>, markersize=<in points>, makeredgecolor=<valid color>

 

plt.plot(x,y,’k’,marker=’d’,markersize=5,markeredgecolor=’red)

 Types of marker:

 

.

,

o

+

X

D

d

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             s

P

*

h

H

1

2

3

4

V

^

< 

> 

|

_

 

 

 

 

plt.plot(x,y,’ro’)

 

Display only marker not line


Assignment - 2

 

1.       First 10 terms of Fibonacci series are stored in a list namely fib:

 

fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

 

Write a program to plot Fibonacci terms and their square-roots with two separate lines on the same plot.

(a)    Series should be plotted as a cyan line with ‘o’ markers having size as 5 and edge-color as red.

(b)   The square-root series should be plotted as a black line with ‘+’ markers having size as 7 and edge-color as red.

 

2.       Given a series nfib that contains reversed Fibonacci numbers with Fibonacci numbers as show below:

[0, -1, -1, -2, -3, -5, -8, -13, -21, -34, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

 

Write a program to plot nfib with following specifications:

 

(a)    The line color should be magenta

(b)   The marker edge color should be black with size 5

(c)    Grid should be displayed

No comments:

Post a Comment