0% found this document useful (0 votes)
28 views33 pages

UNIT II Visualizing Using Matplotlib-Updated

This document provides an overview of using Matplotlib for data visualization in Python, including installation instructions, basic and advanced plotting techniques, and customization options. It covers topics such as simple line plots, scatter plots, and enhancing visualizations with styles and annotations. The document also includes examples demonstrating how to create and customize plots effectively.

Uploaded by

kiranishere549
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)
28 views33 pages

UNIT II Visualizing Using Matplotlib-Updated

This document provides an overview of using Matplotlib for data visualization in Python, including installation instructions, basic and advanced plotting techniques, and customization options. It covers topics such as simple line plots, scatter plots, and enhancing visualizations with styles and annotations. The document also includes examples demonstrating how to create and customize plots effectively.

Uploaded by

kiranishere549
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/ 33

18/06/2024, UNIT II Visualizing Using

15:37 Matplotlib

UNIT II VISUALIZING USING


MATPLOTLIB

Introduction to Matplotlib, Advanced Plotting Techniques,


Customization and Enhancement, Geographic Data
Visualization, Visualization with Seaborn

2.1 IMPORTING MATPLOTLIB:


Matplotlib is a cross-platform data visualization
and graphical plotting library for Python and its
numerical extension NumPy. Matplotlib is a low-
level graph plotting library in Python that serves
as visualization utility. Matplotlib is open source,
and we can use it freely. Matplotlib is a Python
library that helps plot graphs. It is used in data
visualization and graphical plotting.
2.1.1 Installing Matplotlib
To use matplotlib, we need to install it.
Step 1: Make sure Python and pip are
preinstalled on the system, and type the
following commands in the command prompt
to check if Python and pip are installed on the
system.
about:bla 1/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

● To check Python
python --version
If Python is successfully installed, the version
of Python installed on the system will be
displayed.
● To check pip
pip -V
The version of pip will be displayed if it is
successfully installed on the system.
Step 2: Install Matplotlib
Matplotlib can be installed using pip. The
following command is run at the command
prompt to install Matplotlib.
pip install matplotlib
This command will start downloading and
installing packages related to the Matplotlib
library. Once done, the message of successful
installation will be displayed.
Step 3: Check if it is installed successfully
To verify that matplotlib is successfully installed on
your system, execute the following command in the

about:bla 2/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

command prompt. If matplotlib is successfully


installed, the version of matplotlib installed will be
displayed.
● import matplotlib
matplotlib. ersion
Example:
import matplotlib as mpl
import matplotlib.pyplot
as plt x = np.linspace(0,
10, 100) plt.plot(x,
np.sin(x))
plt.plot(x,
np.cos(x))
plt.show()

about:bla 3/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

2.1.2 Saving Figures to File


One nice feature of Matplotlib is the ability to
save figures in a wide variety of formats.
Figures can be saved using the savefig()
command.
Example:
fig.savefig('my_figure.png')
from IPython.display
import Image
Image('my_figure.png')

about:bla 4/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

2.2 ADVANCED PLOTTING TECHNIQUES

SIMPLE LINE PLOTS


Simplest of all plots is the visualization of a
single function y = f( x). For all Matplotlib
plots, the first step is to create a figure and an
axes.
fig = plt.figure()
ax = plt.axes()
● The figure (an instance of the class
plt.Figure) is a single container that contains
all the objects representing axes, graphics,
text, and labels.
● The axes (an instance of the class plt.Axes):
a bounding box with ticks and labels, which
will eventually contain the plot elements.
Next step is to use the ax.plot function to plot some
data.
x = p.linspace(0, 0,
1000) ax.plot(x,
p.sin(x));
Example:

about:bla 5/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
ax.plot(x, np.sin(x));

In Object oriented interface the above program can be


plotted using plt.plot(x, np.sin(x)); To create a single
figure with multiple lines, the plot function is called
multiple times:
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x));

about:bla 6/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

2.2.1 Adjusting the Plot: Line Colors and Styles


Line styles help differentiate graphs by drawing
the lines in various ways. The plt.plot() function
takes additional arguments that can be used to
specify control of the line colors and styles. To
adjust the color, the color keyword is used, which
accepts a string argument representing virtually
any imaginable color. The color can be specified

about:bla 7/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

in the following ways

plt.plot(x, np.sin(x - 0), color='blue')


# specify color by name plt.plot(x,
np.sin(x - 1), color='g')

# short color code (rgbcmyk)


plt.plot(x, np.sin(x - 2), color='0.75')
# Grayscale between 0 and 1

If no color is specified, Matplotlib will automatically


cycle through a set of default colors for multiple
lines. Similarly, the line style can be adjust using the
linestyle keyword

about:bla 8/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

plt.plot(x, x + 0, linestyle='solid')

Line style Keyword Co


de
Solid ‘solid’ ‘-’
Dashed ‘dashed’ ‘--’
Dashdot 'dashed' ‘-.’
Dotted 'dotted’ ':'

linestyle and color codes can be combined into a


single non keyword argument to the plt.plot()
function plt.plot(x, x + 0, '-g') # solid green
2.2.2 Adjusting the Plot: Axes Limits
Matplotlib sets the default range of the axis by
finding extreme values (i.e. minimum and
maximum) on the axes. However, to get a better
view of data the Pyplot module can be used to set
axis ranges of the graphs according to the
requirements in Matplotlib. The most basic way to
adjust axis limits is to use the plt.xlim() and
plt.ylim() methods:

about:bla 9/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

plt.plot(x, np.sin(x))
plt.xlim(-1, 11)
plt.ylim(-1.5, 1.5);

To display either of the axes in reverse, order


of the arguments in the above example has to be
reversed.
2.2.3 Labeling Plots
● The heading or subheading written at the
vertical axis (say Y-axis) and the horizontal
axis(say X-axis) is called a label.
● It improves the quality of understanding of
plotted stats.

about:bla 10/2
nk 4
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

● Titles and axis labels are the simplest such


labels.
Example:
plt.plot(x,
np.sin(x))
plt.title("A
Sine Curve")
plt.xlabel("x")
plt.ylabel("sin (x)");

The position, size, and style of these labels can be


adjusted using optional arguments to the function.
2.2.4 SIMPLE SCATTER PLOT
about:bla 11/2
nk 4
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

● Another commonly used plot type is the simple


scatter plot, a close cousin of the line plot.
● A scatter plot is a visual representation of how
two variables relate to each other. we can use
scatter plots to explore the relationship
between two
variables, for example, by looking for any
correlation between them.

● Instead of points being joined by line


segments, here the points are represented
individually with a dot, circle, or other shape.
A scatter plot can
be plotted using Matplotlibib in two different
ways. They are

1. Using plt.plot()

2. Using plt.scatter()

2.2.4 plt.plot()

● A scatter plot can be plotted using plot() by


including the marker "o" as a third argument,
as otherwise plt.plot() would plot a line graph.
about:bla 12/2
nk 4
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

● The third argument in the function call is a


character that represents the type of symbol
used for the plotting.
Example:

x = np.linspace(0, 10, 30)


y = np.sin(x)
plt.plot(x, y, 'o', color='black');

● The marker or character style has its own set of


short string codes.

Example:

rng = np.random.RandomState(0)

about:bla 13/2
nk 4
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

for marker in ['o', '.', ',', 'x', '+', 'v', '^', '<', '>',
's', 'd']:

plt.plot(rng.rand(5), rng.rand(5),
marker,
label="marker='{0}'".format(marker)
) plt.legend(numpoints=1)

about:bla 14/2
nk 4
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

plt.xlim(0, 1.8);

● Additional keyword arguments to plt.plot


specify a wide range of properties of the
lines and markers such as markersize,
linewidth, markerfacecolor,
markeredgecolor, markeredgewidth etc…
about:bla 15/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

● This type of flexibility in the plt.plot


function allows for a wide variety of
possible visualization options.
2.2.5 plt.scatter()

● A second, more powerful method of creating


scatter plots is the plt.scatter function, which
can be used very similarly to the plt.plot
function.
● The scatter function is used to display data
values as a collection of x, y coordinates
represented by standalone dots.
plt.scatter(x,y, marker='o');

Comparing plt.scatter() and plt.plot():

● The primary difference of plt.scatter from


plt.plot is that it can be used to
create scatter plots where the properties of
about:bla 16/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

each individual point (size, face color, edge


color, etc.) can be individually controlled or
mapped to data.

● If we need a basic scatter plot, use plt.plot(),


especially if we want to prioritize
performance.
● If we want to customize our scatter plot by
using more advanced plotting features, use
plt.scatter().
● plt.plot should be preferred over plt.scatter for
large datasets
3. Customization and Enhancement
Customization and enhancement in data exploration and
visualization involve fine-tuning visual elements,
applying advanced techniques, and tailoring the
presentation of data to improve clarity and insight
Example: Visualizing Monthly Sales Data
Let’s say we have a dataset of monthly sales figures for a
retail store, and we want to create a line plot that clearly
shows the sales trend over a year. We’ll start with a basic
about:bla 17/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

line plot and then enhance it through customization.

Basic Line Plot:

import matplotlib.pyplot as plt

# Sample data: Months and Sales figures


months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec']
sales = [2500, 2700, 3000, 3200, 3400, 3800, 4000, 4200,
3800, 3600, 3400, 3100]

# Basic line plot


plt.plot(months, sales)
plt.xlabel('Months')
plt.ylabel('Sales ($)')
plt.title('Monthly Sales Over a Year')
plt.show()

about:bla 18/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

OUTPUT:

This basic plot shows the trend, but it’s quite plain and
doesn’t highlight any specific information.

about:bla 19/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

Customized and Enhanced Line Plot


Now, let’s customize and enhance the plot to make it
more informative and visually appealing:

Example :
import matplotlib.pyplot as plt
import seaborn as sns

# Sample data: Months and Sales figures


months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec']
sales = [2500, 2700, 3000, 3200, 3400, 3800, 4000, 4200,
3800, 3600, 3400, 3100]

# Setting a style for the plot


sns.set(style="whitegrid")

# Figure size
plt.figure(figsize=(10, 6))

# Enhanced line plot with markers, colors, and


annotations
plt.plot(months, sales, marker='o', linestyle='-',
color='teal', linewidth=2)

# Adding labels and title with custom fonts


plt.xlabel('Months', fontsize=14, fontweight='bold')
plt.ylabel('Sales ($)', fontsize=14, fontweight='bold')
plt.title('Monthly Sales Over a Year', fontsize=16,
fontweight='bold')

about:bla 20/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

# Highlighting specific points with annotations


plt.annotate('Peak Sales', xy=('Aug', 4200), xytext=('Jun',
4500),
arrowprops=dict(facecolor='black', shrink=0.05),
fontsize=12, fontweight='bold')

plt.annotate('Low Sales', xy=('Jan', 2500), xytext=('Mar',


2800),
arrowprops=dict(facecolor='red', shrink=0.05),
fontsize=12, fontweight='bold')

# Customizing the ticks on the x-axis and y-axis


plt.xticks(fontsize=12)
plt.yticks(fontsize=12)

# Adding gridlines for better readability


plt.grid(True)

# Show the enhanced plot


plt.show()

about:bla 21/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

OUTPUT:

about:bla 22/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

Explanation of Customizations:
1. Style and Grid: We used Seaborn’s whitegrid
style to make the plot look cleaner and more
professional. The gridlines improve readability.
2. Figure Size: Adjusting the figure size helps in
making the plot clearer, especially for
presentations or reports.
3. Line and Markers: The line color is changed to
teal, and markers are added at each data point to
highlight the exact sales figures for each month.
The line is also thickened for better visibility.
4. Annotations: Annotations are added to highlight
the peak and low sales months, providing context
and making the plot more informative. Arrows
point to the significant points, drawing the viewer's
attention.
5. Axis Labels and Title: The labels and title are
made bold and larger to stand out, ensuring that the
viewer easily understands the data being presented.
6. Ticks Customization: Font size of the axis ticks is
increased to make them more readable.
about:bla 23/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

3.1. Benefits of Customization and Enhancement


 Clarity: The enhanced plot clearly highlights key
information, such as peak and low sales, making it
easier to interpret the data.
 Visual Appeal: Customizations like colors,
markers, and annotations make the plot more
engaging and aesthetically pleasing.
 Insight: By enhancing the visualization, it
becomes easier to draw meaningful insights from
the data, such as identifying trends or significant
fluctuations in sales.
Geographic Data Visualization

Geographic Data Visualization refers to the graphical


representation of data that is associated with specific
locations on the Earth's surface. It is important because it
helps in understanding patterns, relationships, and trends
that are geographically significant, such as population
distribution, climate change, and regional sales
performance. By visualizing data on maps, it becomes
easier to identify spatial trends and make informed
decisions based on geographic factors.
about:bla 24/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

Geographic Data Visualization in Python can be


implemented using libraries like Matplotlib, Plotly, and
specialized tools like geopandas and folium. These tools
allow you to create maps and plot data on them. Below is
a simple example using geopandas and Matplotlib to plot
geographic data:

import geopandas as gpd


import matplotlib.pyplot as plt

# Load a sample world map shapefile


world =
gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')
)

# Plot the world map


world.plot()
plt.title('World Map')
plt.show()

# Example: Highlighting a specific country (e.g., Brazil)


brazil = world[world.name == 'Brazil']
about:bla 25/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib
base = world.plot(color='white', edgecolor='black')
brazil.plot(ax=base, color='green')
plt.title('Geographic Data Visualization: Highlighting
Brazil')
plt.show()

Explanation:
 geopandas: A library that extends the capabilities
of pandas to allow spatial operations on geometric
types.
 world.plot(): This command plots a simple map of
the world using the shapefile.
 Highlighting Brazil: The code isolates Brazil
from the world map and highlights it in green.
Benefits of using Geographic Data Visualization:
The benefits of using Geographic Data Visualization
include:
1. Spatial Understanding: It helps in understanding
the spatial relationships and patterns within data,
such as the distribution of resources, population
density, or the impact of environmental changes.
2. Enhanced Decision-Making: By visualizing data
geographically, decision-makers can better
understand regional trends and make informed
about:bla 26/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib
decisions based on location-specific insights.
3. Identification of Trends: It allows for the easy
identification of geographic trends, such as areas
with high sales performance or regions prone to
certain diseases.
4. Improved Communication: Maps and other
geographic visualizations are often more intuitive
and easier to understand for a broad audience,
making it easier to communicate complex data.
5. Integration of Diverse Data Sources: Geographic
Data Visualization can integrate and display data
from multiple sources, such as demographic data,
environmental data, and economic indicators,
providing a comprehensive view of the factors
influencing a particular region.
6. Visual Appeal: Maps and geographic
visualizations are often more engaging and
visually appealing, which can help capture
attention and convey information effectively.

about:bla 27/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

Application of Geographic Data Visualization in data


analysis:
Some common applications of Geographic Data
Visualization in data analysis include:
1. Urban Planning: Visualizing population density,
infrastructure, and land use to inform city planning
and development decisions.
2. Public Health: Mapping the spread of diseases,
tracking vaccination rates, and identifying regions
at risk for outbreaks.
3. Environmental Monitoring: Analyzing the impact
of climate change, tracking deforestation, and
monitoring pollution levels across different
regions.
4. Market Analysis: Identifying high-performing
sales regions, visualizing customer demographics,
and optimizing supply chain logistics.
5. Disaster Management: Mapping disaster-prone
areas, visualizing the impact of natural disasters,
and planning evacuation routes.
6. Resource Allocation: Distributing resources such
as healthcare, education, and emergency services
based on geographic need and accessibility.
about:bla 28/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

Visualization with Seaborn


Seaborn provides a high-level interface for creating attractive
and informative statistical graphics.
Sample Source code:
#Box Plot for Sales Distribution by Region
import seaborn as sns
# Plot
sns.boxplot(x='Region', y='Sales', data=df)
plt.title('Sales Distribution by Region')
plt.xlabel('Region')
plt.ylabel('Sales')
plt.show()
Explanation: This creates a box plot that shows the distribution
of sales across different regions, highlighting the spread and
outliers.
 Grouping and Aggregation: Use pandas to group data
and compute aggregate statistics like sum, mean, etc.

 Visualization with Matplotlib and Seaborn: Use


matplotlib and seaborn to create visualizations that
make the aggregated data easier to interpret.

These techniques are essential for understanding patterns and


trends in your data, enabling better decision-making based on
summarized information.

about:bla 29/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

about:bla 30/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

about:bla 31/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

about:bla 32/24
nk
18/06/2024, UNIT II Visualizing Using
15:37 Matplotlib

about:bla 33/24
nk

You might also like