Lab 1
Overview of Matplotlib
1
Introduction
• Matplotlib is probably the most popular plotting library for
Python.
• Each plot is encapsulated in a Figure object. Which is the
top-level container of the visualization. It can have multiple
axes.
• The two main components of a plot are as follows:
– Figure: The outermost container and is used as a canvas to draw on.
It allows you to draw multiple plots within it.
– Axes :It is the actual plot, or subplot, depending on whether you want
to plot single or multiple visualizations. Its sub-objects include the x
and y axis, spines, and legends.
2
Introduction
3
Introduction
4
Plotting Basics
• We will use pyplot submodule of matplotlib
• Steps for using pyplot
– import [Link] as plt
– [Link]() # for creating figure
– [Link](figsize=(10, 5))# to change width and height
default values (6.4&4.8 inch)
– [Link](dpi=300)# to change default value (100)(dots/inch)
– [Link]()#close current figure
– [Link](‘all’) #close all figures
• If you want to close specified figure, you must give figure a number
– [Link](num=10)
– [Link](10)
• You can use this command to find figure number:
[Link]().number
5
Plotting Basics
• Format strings
– It is specified as “[color] [marker] [line]”
• We can specify color in either of these formats
– RGB or RGBA float tuples (for example, (0.2, 0.4, 0.3) or (0.2, 0.4, 0.3, 0.5))
– RGB or RGBA hex strings (for example, '#0F0F0F' or '#0F0F0F0F’)
– First letter of color represented through a character
6
Plotting Basics
For representing markers
7
Plotting Basics
For representing different types of lines
8
Plotting Basics
• General format of plot function
– import [Link] as plt
– [Link]([x],y,[fmt])# x optional
– [Link]([0, 1, 2, 3], [2, 4, 6, 8])# produces solid
[Link]
–
[Link]([0, 1, 2, 3], [2, 4, 6, 8],’o’)# same plot but with
circled marker
9
Creating Simple Visualization
• We want to create this plot
Notes:
Extra Features:
1)Add inline command after import :
1) Add features to line
%matplotlib inline
2) Draw another line on same
figure
2) After creating plot call [Link]() to
3) Save figure
show the figure
10
Text functions
• Functions for setting labels for x axis and y axis
– [Link]() , [Link]() # set labels for current axes (plot)
– set_xlabel() , set_ylabel() # set labels for specified axes (plot)
• Example:
• Fig,ax=[Link]()
• [Link]([1,2,3,4])
• [Link](‘X Label’)
• Functions for setting title
• fig = [Link]()
• [Link]('Suptitle', fontsize=10, fontweight='bold’) # for main figure
• [Link](“axes graph”) # for certain axes
11
Text functions
• Functions for adding text
– figtext(x,y,text) # add text for figure
– text (x,y,text) # add text for certain axes
• Functions for adding annotations (annotate some features of plot)
• We have 2 locations to consider ( annotated location (xy), annotation text
(text xylocation)
• It is useful to have arrow pointing to annotated location using arrowprops.
12
Text functions
[Link](4, 6, 'Text in Data Coords', bbox={'facecolor': 'yellow', 'alpha':0.5, 'pad':10})
[Link]('Example of Annotate', xy=(4,2), xytext=(8,4),
arrowprops=dict(facecolor='green', shrink=0.05))
13
Text functions
• Functions for adding legend
– [Link] () # for current axes
– [Link]() # for spec axes
– [Link]([1, 2, 3], label='Label 1')
– [Link]([2, 4, 3], label='Label 2')
– [Link]()
14
Ex: Visualization of stock trends
15