0% found this document useful (0 votes)
5 views56 pages

Unit_I_1_1

The document provides an overview of Matplotlib, an open-source library for creating 2D and 3D graphics, emphasizing its utility in data visualization through various plot types such as line graphs, bar charts, scatter plots, and box plots. It includes instructions for generating different plots, formatting them, and saving the output, along with assignments and questions for practical application. Key functionalities like adjusting axis limits, adding legends, and customizing styles are also discussed.

Uploaded by

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

Unit_I_1_1

The document provides an overview of Matplotlib, an open-source library for creating 2D and 3D graphics, emphasizing its utility in data visualization through various plot types such as line graphs, bar charts, scatter plots, and box plots. It includes instructions for generating different plots, formatting them, and saving the output, along with assignments and questions for practical application. Key functionalities like adjusting axis limits, adding legends, and customizing styles are also discussed.

Uploaded by

Calmer Music
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Data Visualization

What is Matplotlib
• It is an open-source drawing library which supports rich
drawing types.
• It is used to draw 2D and 3D graphics.

Why
• You can understand your data easily by visualization it
with the help of matplotlib.
• You can generate plots, histograms, bar charts and
many other charts with just a few lines of code.
Types of plots
Import MATPLOTLIB
MATPLOTLIB graph functions
MATPLOTLIB graph functions
Graph Functions
Graph Functions
Graph Functions
Formatting Graphs
Line graph
• Line charts are typically used to visualize data points over a
continuous interval or time period. They are particularly useful for
showing trends and patterns in data.
Two things to note from this plot:

• pyplot.plot assumed our single data list to


be the y-values;
• in the absence of an x-values list, [0, 1, 2,
3] was used instead.
If you provide two lists their lengths must match:

To plot multiple curves simply call plt.plot with as many x–y list pairs as needed:
Multiple Line graph
more plots may be added by repeatedly calling plt.plot.
Also, adding an legend is rather simple:
Adjusting axis ranges can be done by calling plt.xlim and plt.ylim with the lower and higher
limits for the respective axes.
The style strings, one per x–y pair, specify color and shape: ‘rx’ stands for red crosses, and ‘b-.’
stands for blue dash-point line.
Save the plot
• plt.savefig('my_plot.png')
Assignment using line plot
• Draw the line plot for sepal length, sepal width, petal
length and petal width individually.
• Plot then plot in one plot. (assign the different color for
different plot, add legend)
• Provide the axis name, limit, and save the plot.
• Also change the font of the plot’s axis.
• Change the color of the grid, change the grid to major
and linewidth to 1.5.
• Plot a subplot for each individual features in iris dataset.
Bar Chart
Bar Charts
Types of Bar charts
Examples
Side-By-Side Bars Chart
Example
Difference of Bar and Histogram
chart
Difference of Bar and Histogram
chart
Creating a bar plot
The syntax of the bar() function to be used with the axes is as follows:-
plt.bar(x, height, width, bottom, align)

import matplotlib.pyplot as plt

x_positions = [0, 1, 2]
heights = [6, 12, 9]

plt.bar(x_positions, heights)
plt.show()
Creating a bar plot

import numpy as np
import matplotlib.pyplot as plt

# creating the dataset


data = {'C':20, 'C++':15, 'Java':30, 'Python':35}
courses = list(data.keys())
values = list(data.values())

fig = plt.figure(figsize = (10, 5))


# creating the bar plot
plt.bar(courses, values, color ='maroon’, width = 0.4)
plt.xlabel("Courses offered")
plt.ylabel("No. of students enrolled")
plt.title("Students enrolled in different courses")
plt.show()
Creating a Group bar plot
• x = np.arange(5)
• y1 = [34, 56, 12, 89, 67]
• y2 = [12, 56, 78, 45, 90]
• width = 0.40

• # plot data in grouped manner of bar type


• plt.bar(x-0.2, y1, width)
• plt.bar(x+0.2, y2, width)
Questions
• Plot the bar chart having mean value of each features
(sepal_length, sepal_width, petal_length, and petal_width).
Vertical bar chart.

• Plot the bar chart having mean value of each features


(sepal_length, sepal_width, petal_length, and petal_width).
Using horizontal bar chart

• Plot the bar chart having standard devision value of each


features (sepal_length, sepal_width, petal_length, and
petal_width). Using horizontal bar chart
Questions

• Now plot mean value of each feature with different category


(group chart)
Scatter plot

Scatter plot is a plot that display the relationship correlation between two
numerical variables.

Correlation- Two events are related


Line of fit- Line that closely approximates data
Correlation Coefficient – Measure how well data is modelled by a equation
Scatter plot
Positive Correlation
Positive Correlation
Negative Correlation
Negative Correlation
No Correlation
No Correlation
Correlation
Code
Syntax
• plt.scatter(X1, X2, colour, size )
Box plot
Question
• Draw the scatter plot between Sepal length and petal
length of all three groups.
• Draw the scatter plot between petal length and petal
width of all three groups.
• Draw the scatter plot between Sepal length and Sepal
width of all three groups.
• Draw the scatter plot between Sepal width and petal
width of all three groups.
• Try to plot all four in one figure (subplot)
• Save the generated image
Box plot
Box plot
Box plot
Box plot
Box plot
Box Plot
Box plot examples
Box Plot
Box plot
Plot
• plt.boxplot(data)

• Plot boxplot of all four


features

You might also like