0% found this document useful (0 votes)
3 views27 pages

OOP Visualization in Matplotlib

Visualization biology

Uploaded by

theodore.anton
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)
3 views27 pages

OOP Visualization in Matplotlib

Visualization biology

Uploaded by

theodore.anton
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

ENCMP 100 - COMPUTER PROGRAMMING FOR ENGINEERS

Topic 11: Visualization with Matplotlib

1. Drawing and Manipulating Figures in Matplotlib


2. Image Processing with Matplotlib
3. Modular Programming in Image Processing
1. Drawing and Manipulating Figures in Matplotlib

2
Comparison: pyplot vs. OOP Mode
pyplot vs. OOP

Feature pyplot Mode ([Link]()) OOP Mode (fig, ax)

Best for Quick, simple plots Complex, structured plots


Control Implicit Explicit
(plt as a global state manager) (Figure, Axes control everything)

Multiple subplots Harder to manage Easier with fig, ax = [Link]()

Readability Shorter but less structured More readable and maintainable

3
Object Oriented Interface of Matplotlib

Core Components:
o Figure: The overall container that holds one or more Axes.
o Axes: The actual plotting area (can have multiple per Figure).
o Axis: Controls the X and Y axes properties (ticks, labels, etc.).
o Artist: Everything in the figure (lines, text, shapes, etc.).

4
Object Oriented Interface of Matplotlib

[Link]

5
EzGraphics and Matplotlib
EzGraphics Matplotlib Description
GraphicsWindow(wi fig, ax = [Link](figsize=(width/100, Creates a drawing window
dth, height) height/100)) (figure)
[Link]() ax Retrieves the canvas (axes) for
drawing
[Link]("col Handled in drawing functions (e.g., Sets the drawing color
or") [Link](..., color='color'))
[Link](x, ax.add_patch([Link]((x, y), Draws a filled rectangle
y, width, height) width, height, color='color'))
[Link](x1, [Link]([x1, x2], [y1, y2], color='color') Draws a straight line
y1, x2, y2)
[Link](x, ax.add_patch([Link]((x + width/2, Draws an oval (ellipse)
y, width, height) y + height/2), width, height, color='color'))
[Link](x, [Link](x, y, "text", color='color') Draws text at a given position
y, "text")
[Link]() [Link]() Clears the canvas
[Link]() [Link]() Displays the window and waits
for user interaction 6
EzGraphics and Matplotlib

matplotlib_ezgrap
hics

7
gca (Get Current Axes)
Retrieves the current Axes object in a Matplotlib figure. If no axes
exist, it creates one. It allows customization of the plot, such as
setting titles, labels, ticks, and other properties.

An Axes object contains the actual plot elements:


• The coordinate system (x, y, and possibly z axes).
• Data points, lines, bars, etc.
• Labels, ticks, grids, and titles.

gca()

8
Avoid [Link]()? Avoid [Link]()

Method Requires [Link]()? Code Style


[Link]() + [Link]() Yes Hybrid (Pyplot + OOP)
fig, ax = [Link]() No Fully OOP

9
invert_yaxis
Reverses the y-axis direction, such that values increase downward.

gca()

10
add_patch
Adds a graphical element (such as a shape like a Rectangle, Circle,
etc.) to an Axes object as a Patch.

add_patch

Each tuple (R, G, B) defines the intensity of the


red, green, and blue channels, respectively.
Values range from 0 (none) to 1 (full intensity).
11
set_visible
Controls whether an artist (e.g., line, text, or patch) is visible.

set_visible

12
set_aspect
Sets the axes' aspect ratio to 'equal' for uniform x and y scaling or
'auto' for automatic adjustment based on the data.

add_patch

13
set_facecolor
Sets the background color of an Axes.

add_patch

14
Example
Draws the Italian flag using Matplotlib's Rectangle patch

ItalianFlag_Ez ItalianFlag_mat

15
Selection, repetition and patches
Draw a target with a bull's eye using the number of rings specified by the user

Bull's eye

16
text
Adds text at a specified location in the plot. Circles

add_patch

17
User defined graphics functions
Simulate the rolling of 5 dice and draws the face of each die in a graphics window

rolldice

18
2. Image Processing with Matplotlib

19
imread, imshow, imsave, savefig

image file

image

20
Textbook Examples

viewimage filterimage flipimage

21
3. Modular Programming in Image Processing

Advantages:
• Writing functions for reusable image processing tasks
• Structuring code into functions for better organization

22
23
Textbook Example
Create an interactive image processor with the user-defined module imgproctools, enabling
users to apply transformations via a menu.

imgproctools processimg

24
Textbook Example
Draw figures based on data in a text file

drawscene lamppost

25
Summary
Drawing and Manipulating Figures in Matplotlib:

 Working with Figures and Axes:


o Understanding [Link] and gca() (Get Current Axes)
o Adjusting proportions with set_aspect()
o Changing background color using set_facecolor()
o Controlling visibility with set_visible()
o Flipping the coordinate system with invert_yaxis()

 Drawing and Modifying Shapes:


o Adding shapes with add_patch() (Rectangle, Circle, Ellipse, etc.)
o Using loops for pattern creation
o Selecting and modifying specific patches
o Adding labels with text(), customizing color, size, and alignment

26
Summary
Image Processing with Matplotlib:

o Loading images with imread()


o Displaying images using imshow()
o Saving images with imsave() and savefig()

Modular Programming in Image Processing:

o Writing reusable image processing functions


o Structuring code for better organization

27

You might also like