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

AI Lab4

The document outlines a practical lab exercise for students at Sukkur IBA University focusing on Python data visualization using the Matplotlib library. It covers objectives, installation, and various plotting techniques, including line plots, scatter plots, bar graphs, and pie charts. Additionally, it includes lab tasks that require students to create visualizations using the IRIS dataset and perform data cleaning and correlation analysis.

Uploaded by

ayesha mangrio
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 views25 pages

AI Lab4

The document outlines a practical lab exercise for students at Sukkur IBA University focusing on Python data visualization using the Matplotlib library. It covers objectives, installation, and various plotting techniques, including line plots, scatter plots, bar graphs, and pie charts. Additionally, it includes lab tasks that require students to create visualizations using the IRIS dataset and perform data cleaning and correlation analysis.

Uploaded by

ayesha mangrio
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
You are on page 1/ 25

Sukkur IBA University

Department of Computer Science


6 th
Semester 3rd Year

Artificial Intelligence - Lab

Practical No. 4
To become familiar with Data Python visualization
Student’s Roll no: _______________ Points Scored: __________________________

Date of Conduct: ________________ Teacher’s Signature: ___________________

LAB DATA ANALYSIS ABILITY TO


SUBJECT CALCULATION OBSERVATION/
PERFORMANCE KNOWLEDGE
AND CONDUCT PRESENTATION
AND CODING RESULTS
SCORE
INDICATOR INTERPRETATION EXPERIMENT

 OBJECTIVES: Upon successful completion of this practical, the students will be able to:
 Understand the basics of python data visualization.
 Understand and learn matplotlib.
 Create programs for data visualization using matplotlib.

Python Data Visualization

● Data visualization is the art of presenting data graphically or in a pictorial format.


● In today’s world, a lot of data is being generated on a daily basis. And sometimes to analyze this
data for certain trends, patterns may become difficult if the data is in its raw format. To
overcome this data visualization comes into play. Data visualization provides a good, organized
pictorial representation of the data which makes it easier to understand, observe, analyze.
● Python provides various libraries that come with different features for visualizing data. All
these libraries come with different features and can support various types of graphs.

● In this tutorial, we will be discussing four such libraries.


 Matplotlib
 Seaborn
 Bokeh
 Plotly
In this lab we will discuss matplotlib and will plot some most commonly used graphs.

Data Visualization using Matplotlib.


Matplotlib is a 2-D plotting library that helps in visualizing figures. Matplotlib in Python is used as it
is a robust, free and easy library for data visualization.

 Anatomy of Matplotlib Figure

 Installing Matplotlib
You can do installation of matplotlib in three ways.
1. Using Anaconda navigator, you can do it using the GUI itself. Otherwise, you can install it using
either of the following commands (for Anaconda prompt and command prompt respectively).
2. conda install matplotlib.
3. pip install matplotlib.

 Checking Matplotlib Version


The version string is stored under __version__ attribute.

 Importing Matplotlib
For plotting graph, need to import matplotlib in the python file. Here is two way to import
matplotlib given below.

 Pyplot
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under
the plt alias:

Now the Pyplot package can be referred to as plt.

• Example
Draw a line in a diagram from position (0,0) to position (6,250):

 Matplotlib Plotting

 Plotting x and y points


The plot() function is used to draw points (markers) in a diagram.

By default, the plot() function draws a line from point to point.

The function takes parameters for specifying points in the diagram.

Parameter 1 is an array containing the points on the x-axis.

Parameter 2 is an array containing the points on the y-axis.

If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to the
plot function.

 Plotting Without Line


To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'.
• Example
Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):

 Multiple Points
You can plot as many points as you like, just make sure you have the same number of points in both
axis.
• Example
Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8, 10):
 Default X-Points
If we do not specify the points on the x-axis, they will get the default values 0, 1, 2, 3 (etc.,
depending on the length of the y-points.
So, if we take the same example as above, and leave out the x-points, the diagram will look like this:

The x-points in the example above are [0, 1, 2, 3, 4, 5].

 Matplotlib Markers

You can use the keyword argument marker to emphasize each point with a specified marker:
 Example
Mark each point with a circle:

 Example
 Marker Reference

 Marker Reference
You can use also use the shortcut string notation parameter to specify the marker.
This parameter is also called fmt, and is written with this syntax:
Format Strings fmt

• Example
Mark each point with a circle:

 Line Reference

 Color Reference
• Example

• Note
You can also use hexadecimal color values.
You can use 140 supporting HTML color names

 Matplotlib Line
 Linestyle
You can use the keyword argument linestyle, or shorter ls, to change the style of the plotted line:
• Example

• Shorter Syntax
The line style can be written in a shorter syntax:

• Line Styles
You can choose any of these styles:

• Line Color
You can use the keyword argument color or the shorter c to set the color of the line:
• Example

• Multiple Lines
You can plot as many lines as you like by simply adding more plt.plot() functions:

 Matplotlib Labels
Create Labels for a Plot
With Pyplot, you can use the xlabel() and ylabel() functions to set a label for the x- and y-axis.
• Set Font Properties for Title and Labels
You can use the fontdict parameter in xlabel(), ylabel(), and title() to set font properties for the title
and labels.
• Position the Title

 Matplotlib Adding Grid Lines

With Pyplot, you can use the grid() function to add grid lines to the plot.
 Matplotlib Subplot

With the subplot() function you can draw multiple plots in one figure:

• With the subplots() function you can draw multiple plots in one figure.

• The subplots() function takes three arguments that describes the layout of the figure.

• The layout is organized in rows and columns, which are represented by the first and second
argument.

• The third argument represents the index of the current plot.

• You can add a title to each plot with the title() function:

• You can add a title to the entire figure with the suptitle() function:
• Example
• Example
How To Decide Which Type of Chart to Use?

• If you want to know more information about how a data set performed during a specific time.

– Column

– Bar

– Pie

– Line

– Scatter Plot
• Use this type of chart to show how individual parts make up the whole of something.

– Pie

– Stacked Bar

– Stacked Column

– Area or Waterfall

• Distribution charts help you to understand outliers, the normal tendency, and the range of
information in your values.

– Scatter Plot

– Line

– Column

– Bar

 Types of Charts
 Matplotlib Scatter plot

With Pyplot, you can use the scatter() function to draw a scatter plot.

The scatter() function plots one dot for each observation. It needs two arrays of the same length,
one for the values of the x-axis, and one for values on the y-axis:

• Example
• Example

• Example
 ColorMap

The Matplotlib module has a number of available colormaps.

A colormap is like a list of colors, where each color has a value that ranges from 0 to 100.

Here is an example of a colormap:


This colormap is called 'viridis' and as you can see it ranges from 0, which is a purple color, up to
100, which is a yellow color.

 Matplotlib Bar Plot


With Pyplot, you can use the bar() function to draw bar graphs:
• Example

 Matplotlib Histogram
A histogram is a graph showing frequency distributions.
It is a graph showing the number of observations within each given interval.

• Example
• Example

 Matplotlib Pie Charts


With Pyplot, you can use the pie() function to draw pie charts:

• Add labels to the pie chart with the labels parameter.


• The label parameter must be an array with one label for each wedge:
• As mentioned the default start angle is at the x-axis, but you can change the start angle by
specifying a startangle parameter.
• The startangle parameter is defined with an angle in degrees, default angle is 0.
• Maybe you want one of the wedges to stand out? The explode parameter allows you to do
that.
• The explode parameter, if specified, and not None, must be an array with one value for each
wedge.
• Each value represents how far from the center each wedge is displayed:
• Add a shadow to the pie chart by setting the shadow parameter to True
• You can set the color of each wedge with the colors parameter.
• The colors parameter, if specified, must be an array with one value for each wedge
• To add a distribution of numbers in a wedges, use the autopct parameter.
• To add a list of explanation for each wedge, use the legend() function.
• To add a header to the legend, add the title parameter to the legend function.

• Example
Lab Tasks

1. Perform all given lab example tasks, add code and screenshot here for each type of visualization
charts.
2. Create a graph having four subplots using IRIS dataset. The first subplot should show scatter
graph showing the id on x-axis and the ratio of sepal width and sepal width on y-axis of all three
species. The second subplot should show scatter graph showing the id on x-axis and the ratio of
sepal width and sepal width on y-axis of all three species. The third subplot should show scatter
graph showing the id on x-axis and the ratio of sepal width and sepal width on y-axis of all three
species. The fourth subplot should show the distribution of instances of all three species in pie
chart. In addition, also create a heatmap to show the correlation between the sepal length, sepal
width, petal length and petal width. Save both the diagrams and send with code to you
instructor email id and upload on google classroom. Here is iris dataset link: Iris Dataset |
Kaggle
3. Perform data visualization using different charts, add code and screenshot of output here. Also
perform correlation, and data cleaning.You can use any dataset from given link: GitHub -
mwaskom/seaborn-data: Data repository for seaborn examples

The End

You might also like