Saturday, 30 January 2021

1 - Data Visualization using matplotlib in Python

 


Data Visualization using matplotlib in Python

 

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,height))

 

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 axes 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. She 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.

 

 



No comments:

Post a Comment