AI Lab4
AI Lab4
Practical No. 4
To become familiar with Data Python visualization
Student’s Roll no: _______________ Points Scored: __________________________
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.
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.
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:
• Example
Draw a line in a diagram from position (0,0) to position (6,250):
Matplotlib Plotting
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.
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:
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
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.
• 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
A colormap is like a list of colors, where each color has a value that ranges from 0 to 100.
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
• 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