0% found this document useful (0 votes)
9 views5 pages

Data Visualization with Matplotlib

The document provides an overview of data visualization using Matplotlib, including how to create various types of plots such as line charts, bar charts, and histograms. It explains the use of different parameters to customize plots, such as color, line width, and markers. Additionally, it includes coding examples for plotting data from CSV files and exporting DataFrames to CSV.

Uploaded by

adeshsasikumar85
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Data Visualization with Matplotlib

The document provides an overview of data visualization using Matplotlib, including how to create various types of plots such as line charts, bar charts, and histograms. It explains the use of different parameters to customize plots, such as color, line width, and markers. Additionally, it includes coding examples for plotting data from CSV files and exporting DataFrames to CSV.

Uploaded by

adeshsasikumar85
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

wredwr PLOTTING DATA USING MATPLOTLIB

DATA VISUALISATION
● Data visualisation means graphical or pictorial representation of the data
using graph, chart,etc.
● Visualisation also helps to effectively communicate information to intended
users.
● The purpose of plotting data is to visualise variation or show relationships
between variables.
PLOTTING USING MATPLOTLIB
● Matplotlib library is used for creating static, animated, and interactive 2D-
plots or figures in Python.
● The pyplot module of matplotlib contains a collection of functions that can be
used to work on a plot.
● The plot() function of the pyplot module is used to create a figure.
● A figure is the overall window where the outputs of pyplot functions are
plotted. It contains a plotting area, legend, axis labels, ticks, title, etc.
MARKER
● A marker is any symbol that represents a data value in a line chart or a scatter
plot.
COLOUR
● It is possible to format the plot by changing the colour of the plotted data
LINEWIDTH AND LINE STYLE
● The linewidth and linestyle property can be used to change the width and the
style of the line chart.
● The default line width is 1 pixel showing a thin line.
● The linestyle parameters are "solid", "dotted", "dashed" or "dashdot".
LINE CHART

● A line plot is used to visualise growth or decline in data over a time interval.

To plot a simple line chart


import [Link] as plt
[Link]([10, 20, 30, 40, 50])
[Link]('x-axis')
[Link]('y-axis')
[Link]('Line chart')
[Link]()

To plot frequency of marks using line chart


import [Link] as plt
list1=[50,50,50,65,65,75,75,80,80,90,90,90,90]
[Link](list1, color='red', linewidth=5)
[Link](“value”)
[Link](“Frequency”)
[Link](“Marks using Line Chart”)
[Link]()
BAR CHART
● A bar chart represents categorical(category) data with rectangular bars.
● In order to show comparisons, bar charts can be used
● The bars can be plotted vertically or horizontally.

To plot a bar chart for the given class and strength


import [Link] as plt
x=['VII','VIII','IX','X']
y=[60,40,55,30]
[Link]('Secondary Class Strength')
[Link]('Class')
[Link]('No. of students')
[Link](x,y)
[Link]()

To plot a horizontal bar chart


import numpy as np
import [Link] as plt
objects=('python','c++','java','perl','scala','lisp')
y_pos=[Link](len(objects))
p=[10,8,6,4,2,1]
[Link](y_pos,p,align='center',color='r')
[Link](y_pos,objects)
[Link]('usage')
[Link]('programming language')
[Link]()

HISTOGRAM
● Histograms are column-charts, where each column represents a range of
values, and the height of a column corresponds to how many values are in
that range.
● To make a histogram, the data is sorted into "bins" and the number of data
points in each bin is counted.
● The height of each column in the histogram is then proportional to the
number of data points its bin contains.

To display a histogram with well defined edges


import [Link] as plt
import numpy as np
y= [Link](1000)
[Link](y,25,edgecolor="red")
[Link]()

To generate a histogram having the same values


import [Link] as plt
data=[5,15,25,35,15,55]
[Link](data,bins=[0,10,20,30,40,50,60],weights=[20,10,45,33,6,8],facecolor='y
', edgecolor="red")
[Link]('histogram for students data')
[Link]('value')
[Link]('frequency')
[Link]('[Link]')
[Link]()

Difference between Bar chart and Histogram

Bar chart Histogram

Bar chart represents categorical data Histogram presents numerical data


with bins

A bar chart is drawn with gap The histogram is drawn with no gap
between the bars between the bars

Diagrammatic comparison of discrete Frequency distribution of continuous


variables variables

[Link]() is used to plot the bar chart [Link]() is used to plot the histogram

KIND FOR DIFFERENT PLOTS USING DATAFRAME


● The plot() method of pandas accepts different plot types by the kind
keyword arguments
● [Link](kind), where kind accepts a string indicating the type of plot()

Importing CSV to DataFrame and generate bar chart

1001 Ajay 25000

1003 Nikil 32000


1007 Gani 29500
1009 Kumar 43000

Empid Name Salary

● Read the details from CSV file and store the contents into data frame
● Retrieve all the details of Ajay and Kumar
● Generate a bar chart for the Name and Salary columns by applying the
pattern ‘/’ with the edgecolor ‘purple’ and fill with grey color
● Give proper labels for X and Y axis
● The Title of the chart should be ‘Employee details’
● Save the chart as ‘[Link]’

CODING
import pandas as pd
import [Link] as plt
d=pd.read_csv(r"D:/[Link]")
df=[Link](d)
print([Link][[0,3],:])
[Link](kind="bar",x="Name",y="Salary", hatch="/", color="grey",
edgecolor="purple")
[Link]("Employee Name")
[Link]("Salary")
[Link]("Employee details")
[Link]("[Link]")
[Link]()

Exporting DataFrame to CSV and generate line chart

Code Vtype Perkm


11 Volvo 160
12 Deluxe 150
13 SUV 40
14 Micro 20
● Create a data frame “vdf” with the given details
● Retrieve the Vtype from Deluxe to Micro
● Draw a Line chart for the Vtype and Perkm columns with red color ,
marker point as “*” with color yellow, size of the marker is 10
● Give proper labels for X and Y axis
● The Title of the chart should be ‘Charges per km’
● Save the chart as ‘[Link]’
● Export the details from the dataframe and store the contents into CSV
CODING
import [Link] as plt
import pandas as pd
d=[{'Code':11,'Vtype':'Volvo', 'Perkm':160},
{'Code':12,'Vtype':'Deluxe','Perkm':150},
{'Code':13,'Vtype':'SUV','Perkm':40},
{'Code':14,'Vtype':'Micro','Perkm':20}]
vdf=[Link](d)
print([Link][1:3,'Vtype'])
[Link](kind='line',x='Vtype',y='Perkm',color='red',marker='*',markerfacecolor
='yellow',markersize=10)
[Link]('Vtype')
[Link]('Per km')
[Link]('Charges per km')
[Link]("D:/[Link]")
[Link]()
vdf.to_csv("D:/[Link]")
print('Data Exported')

You might also like