0% found this document useful (0 votes)
70 views22 pages

Data Visualization with Python Libraries

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

Data Visualization with Python Libraries

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

Data Visualization in Python

Dr. [Link]
Associate Professor,
Dept of Computer Science & Systems Engineering
Andhra University
1
Data Visualization

• Data visualization is a field in data analysis that deals with visual representation of

data. It graphically plots data and is an effective way to communicate inferences

from data.

• When data is presented in images, maps, and graphs, the human brain processes it

quickly and easily.

• Both large and small data sets benefit from data visualization.

• Data visualization in Python is perhaps one of the most often utilized features for

data science with Python in today's environment.

• Python offers several libraries for data visualization, each with their capabilities.

• Python comes with several plotting libraries, including Matplotlib, Seaborn, Plotly

and Bokeh tools for data visualization.


Matplotlib
• A Python visualization library for 2D array plots is called
Matplotlib.
• The NumPy library is used by the Python library Matplotlib.
• It is compatible with Jupyter notebooks, web application server
software, and the Python and IPython shells.
• Various plots, including scatter, line, bar, histogram, and others,
are available in Matplotlib to help us delve deeper into trends,
behavioral patterns, and relationships. It was first introduced by
John Hunter in 2002.
Line Plots
Step-by-step Approach to draw the line plots
• Import module.
• Create data.
• Normally plot the data by setting linestyle or ls argument of plot() method.
• Add plot details
• Display plot.
Step1: Import module: Line plots can be created in Python with Matplotlib's
Pyplot library.
Line Plots
Step 2: Create a Data x_data = [0, 2, 4, 6, 8, 10, 12, 14]
y_data = [4, 2, 8, 6, 10, 5, 12, 6]
Step 3: Plot the Data [Link](x_data, y_data) function is used to create a
line plot with the ‘x_data' on the x-axis and ‘y_data' on
the y-axis.
Step 4: Add plot details After the [Link]() line, add details such as a title,
axis labels, legend, grid, and tick labels.
Step 5: Display the plot
Finally, the [Link]() function is called to display the
plot
Line Properties
Add plot details
Figures and Sub Plots
Subplots
• Sometimes it is useful for problem solvers to include a couple plots in the
same figure window.
• This can be accomplished using Matplotlib subplots. Matplotlib's
[Link]() function can include two positional arguments for the number
of rows of subplots in the figure and the number of columns of subplots in
the figure.
Syntax:

fig, <ax objects> = [Link](rows, cols)

fig: Figure - the container for plotting the all plots. You
can have multiple plots in one figure.
ax: Axes - a container for 1 plot. You can have multiple
Axes in a single Figure - corresponding to multiple plots.
rows and cols are integers that control the subplot layout. The <ax objects> needs to have
dimensions that correspond to rows and cols
Figures and Sub Plots
Suppose we declare: fig, ax = [Link](), this means that creating a single Figure
with a single Axes within. You can also use fig, axes = [Link](ncols=2), which will
create a single Figure with 2 Axes side-by-side (2 columns).

Note: See that in the above plot, there is an overlap between the axis names and the
titles of a different plots. For fixing this, you need to use tight_layout() function.
Figures and Sub Plots
Scatter Plots

• Scatter plots are utilized to see how different variables are related to each
other. The dots on the plot shows how the variables are related. A scatter
plot is made with the matplotlib library's scatter() method.
• People often use scatter plots to show the relationship between two or more
variables and how a change in one affects the other.

Syntax:
[Link](x_axis_data, y_axis_data, s, c, marker, linewidths, edgecolors)

x_axis_data: An array containing data for the [Link]


s: Marker size,
c: Color of the sequence of colors for markers.
marker: Marker style.
linewidths: Width of the marker border.
edgecolor: Marker border color.
Scatter Plots

Note: The two plots are plotted with


two different colors, by default blue
and orange
Scatter Plots
Bar Charts
• A bar plot or bar chart is a graph that represents the category of
data with rectangular bars with lengths and heights that is
proportional to the values which they represent.
• The bar plots can be plotted horizontally or vertically.
• A bar chart describes the comparisons between the discrete
categories.
• One of the axis of the plot represents the specific categories being
compared, while the other axis represents the measured values
corresponding to those categories.
Steps to Create a Bar Chart in Python using Matplotlib
1. Install the Matplotlib package:
2. Gather the data for the bar chart
3. Create the bar chart in Python using Matplotlib

4. Display bar Chart


Bar Charts
Syntax:
Bar Charts
Syntax:
Histogram Plots
• A histogram is a visual depiction of a frequency distribution table with
continuous divisions that have been grouped.
• A series of rectangles with foundations equal to the distances between class
bounds and areas proportionate to the frequency in the associated classes
make up the area diagram.
• They are particularly useful for exploring continuous data, such as
numerical measurements or sensor readings.
• It is a type of bar plot where the X-axis represents the bin ranges while the
Y-axis gives information about frequency.
Histogram Plots
Steps to create Histograms:
1. Install the Matplotlib package
2. Collect the data for the histogram
3. Determine the number of bins
4. Plot the histogram in Python using matplotlib
Legends
• A legend is an area describing the elements of the graph. In
the matplotlib library, there’s a function called legend() which
is used to Place a legend on the axes.

• The attribute Loc in legend() is used to specify the location of


the legend. Default value of loc is loc=”best” (upper left). The
strings ‘upper left’, ‘upper right’, ‘lower left’, ‘lower right’
place the legend at the corresponding corner of the
axes/figure.
Legends
Different ways to create a Legends
1. Automatic detection of elements to be shown in the legend:
• The elements to be added to the legend are automatically determined, when you
do not pass in any extra arguments.

2. Explicitly listing the artists and labels in the legend


For full control of which artists have a legend entry, it is possible to pass an iterable of
legend artists followed by an iterable of legend labels respectively:
Legends
Attributes of Legend function:

• shadow: [None or bool] Whether to draw a shadow behind the [Link]’s


Default value is None.
• markerscale: [None or int or float] The relative size of legend markers
compared with the originally drawn [Link] Default is None.
• fontsize: The font size of the [Link] the value is numeric the size will be
the absolute font size in points.
• facecolor: [None or “inherit” or color] The legend’s background color.
• edgecolor: [None or “inherit” or color] The legend’s background patch edge
color.
Annotations:

[Link]

[Link]
Annotations/

You might also like