SlideShare a Scribd company logo
Matplotlib: Python Plotting
Hendrik Speleers
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Overview
– Anatomy of a figure
●
Figures and axes
– 2D plotting
●
Standard line plotting
●
Other plotting + text annotation
– 3D plotting
●
3D axes + 3D line/surface plotting
– Other plotting
●
Contours + image visualization
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Matplotlib
– Mathematical plotting library
– Python extension for graphics
●
Suited for visualization of data and creation of high-quality figures
●
Extensive package for 2D plotting, and add-on toolkits for 3D plotting
●
Pyplot: MATLAB-like procedural interface to the object-oriented API
– Import convention
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Matplotlib
– Mathematical plotting library
– Interactive matplotlib sessions
●
IPython console
●
Jupyter notebook
%matplotlib
%matplotlib inline
%matplotlib notebook
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
A simple plot
– Syntax is array-based
– If not interactive, also write:
In [1]: x = np.linspace(0, 2.0*np.pi, 100)
In [2]: cx, sx = np.cos(x), np.sin(x)
In [3]: plt.plot(x, cx)
...: plt.plot(x, sx)
...: plt.show()
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
A simple plot
– Default settings (see also plt.rcParams)
In [3]: plt.figure(figsize=(6.0, 4.0), dpi=72.0)
...: plt.subplot(1, 1, 1)
...: plt.plot(x, cx, color='#1f77b4',
...: linewidth=1.5, linestyle='-')
...: plt.plot(x, sx, color='#ff7f0e',
...: linewidth=1.5, linestyle='-')
...: plt.xlim(-0.1*np.pi, 2.1*np.pi)
...: plt.xticks(np.linspace(0, 6, 7))
...: plt.ylim(-1.1, 1.1)
...: plt.yticks(np.linspace(-1, 1, 9))
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Anatomy
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Anatomy
– Hierarchical structure
– Figure
●
The overall window on which everything is drawn
●
Components: one or more axes, suptitle, ...
plt.figure(num=None, figure index, 1-based
figsize=None, (width, height) in inches
dpi=None, resolution
facecolor=None, background color
...)
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Anatomy
– Axes
●
The area on which the data is plotted
● Belongs to a figure, placed arbitrarily (axes) or in grid (subplot)
●
Components: x/y-axis, ticks, spines, labels, title, legend, ...
●
All methods of active axes are directly callable via Pyplot interface
plt.axes((left, bottom, width, height), **kwargs)
plt.subplot(nrows, ncols, index, **kwargs)
**kwargs: facecolor=None, polar=False, ...
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Anatomy
– Axes components
● Get or set limits: plt.xlim, plt.ylim, plt.axis
– left, right = plt.xlim()
– plt.xlim(left, right)
– plt.axis((left, right, bottom, top)), plt.axis('equal')
● Get or set ticks: plt.xticks, plt.yticks
– locs, labels = plt.xticks()
– plt.xticks(np.arange(3), ('a', 'b', 'c'))
● Set labels: plt.xlabel(txt), plt.ylabel(txt)
● Set title: plt.title(txt)
● Others: plt.box(), plt.grid(), ...
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Anatomy
– Example
In [1]: plt.figure(facecolor='silver')
...: plt.subplot(1, 2, 1)
...: plt.title('subplot')
...: plt.axes((0.4, 0.3, 0.4, 0.4))
...: plt.xlim(1, 5)
...: plt.ylim(-5, -1)
...: plt.title('axes')
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
2D plotting
– Standard line plotting: basic syntax
●
Connect data points (x, y) with optional format string
● Color (c): b, g, r, c, m, y, k, w
● Linestyle (l): -, --, -., :
● Marker (m): o, *, ., +, x, s, d, ^, <, >, p, h, ...
plt.plot(y)
plt.plot(x, y)
plt.plot(x, y, 'clm')
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
2D plotting
– Standard line plotting: advanced syntax
– Multiple plots per axes possible
– Legend:
plt.plot(x, y, **kwargs)
**kwargs: color, linestyle, linewidth, marker,
markeredgecolor, markeredgewidth,
markerfacecolor, markersize, label, ...
plt.legend(('a', 'b', 'c'), loc='upper right')
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
2D plotting
– For full plot details, check out plt.plot?
– Example
In [1]: t = np.arange(0.0, 2.0, 0.01)
...: s = 1.0 + np.sin(2.0*np.pi*t)
In [2]: plt.axes(facecolor='silver')
...: plt.plot(t, s, 'r')
...: plt.xlabel('time (s)')
...: plt.ylabel('voltage (mV)')
...: plt.title('About as simple as it gets')
...: plt.grid()
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
2D plotting
– Plotting methods are actually connected to axes
●
Pyplot provides an interface to the active axes
In [1]: t = np.arange(0.0, 2.0, 0.01)
...: s = 1.0 + np.sin(2.0*np.pi*t)
In [2]: ax = plt.axes()
...: ax.plot(t, s, 'r')
...: ax.set(facecolor='silver',
...: xlabel='time (s)',
...: ylabel='voltage (mV)',
...: title='About as simple as it gets')
...: ax.grid()
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
2D plotting
– Example: data statistics
●
Data in the file “populations.txt” describes the populations of
hares, lynxes and carrots in northern Canada during 20 years
●
Load the data and plot it
●
Compute the mean populations over time
●
Which species has the highest population each year?
# year hare lynx carrot
1900 30e3 4e3 48300
1901 47.2e3 6.1e3 48200
1902 70.2e3 9.8e3 41500
...
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
2D plotting
– Example: data statistics
●
Load the data and plot it
In [1]: data = np.loadtxt('populations.txt')
In [2]: year, hares, lynxes, carrots = data.T
In [3]: plt.axes((0.2, 0.1, 0.6, 0.8))
...: plt.plot(year, hares)
...: plt.plot(year, lynxes)
...: plt.plot(year, carrots)
...: plt.xticks(np.arange(1900, 1921, 5))
...: plt.yticks(np.arange(1, 9) * 10000)
...: plt.legend(('Hare', 'Lynx', 'Carrot'))
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
2D plotting
– Example: data statistics
●
Compute the mean populations over time
●
Which species has the highest population each year?
In [4]: populations = data[:, 1:]
In [5]: populations.mean(axis=0)
Out[5]: array([34080.9524, 20166.6667, 42400.])
In [6]: populations.std(axis=0)
Out[6]: array([20897.9065, 16254.5915, 3322.5062])
In [7]: populations.argmax(axis=1)
Out[7]: array([2, 2, 0, 0, 1, 1, 2, 2, 2, 2, ...])
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
2D plotting
– Other plotting
● Log plots: plt.loglog(x, y), plt.semilogx(x, y), plt.semilogy(x, y)
● Polar plots: plt.polar(theta, r)
● Scatter plots: plt.scatter(x, y)
● Bar graphs: plt.bar(x, height), plt.barh(y, width)
● Pie charts: plt.pie(x)
● Histogram: plt.hist(x, bins=None)
● Filled curves: plt.fill(x, y), plt.fill_between(x, y1, y2=0)
– For full method details, check out plt.method?
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
2D plotting
– Example
In [1]: names = ['a', 'b', 'c', 'd']
...: values = [1, 10, 40, 50]
In [2]: plt.figure(figsize=(3, 9))
...: plt.subplot(3, 1, 1)
...: plt.bar(names, values)
...: plt.subplot(3, 1, 2)
...: plt.scatter(names, values)
...: plt.subplot(3, 1, 3)
...: plt.fill_between(names, values)
...: plt.suptitle(
...: 'categorical plotting', y=0.92)
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
2D plotting
– Text
● Axes text: plt.title(txt), plt.xlabel(txt), plt.ylabel(txt)
● Plain text: plt.text(x, y, txt)
● Annotation: plt.annotate(txt, xy=(x, y), xytext=(xt, yt),
arrowprops={'arrowstyle':'->'})
●
Extensive math rendering engine
– Support for TeX markup inside dollar signs ($)
– Use raw strings (precede the quotes with an 'r')
plt.title('alpha > beta') # normal text
plt.title(r'$alpha > beta$') # math text
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
2D plotting
– Example
In [1]: x = np.linspace(0, 2.0*np.pi, 25)
In [2]: plt.scatter(x, np.sin(x))
...: plt.ylim(-2, 2)
...: plt.text(3, 1, 'sine function',
...: fontsize=18, style='italic')
...: plt.annotate('localnmax',
...: xy=(np.pi/2.0, 1), xytext=(1.3, 0),
...: arrowprops={'arrowstyle':'->'})
...: plt.annotate('localnmin',
...: xy=(np.pi*3.0/2.0, -1), xytext=(4.5, -0.4),
...: arrowprops={'arrowstyle':'->'})
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
3D plotting
– Module mplot3d
●
This toolkit adds simple 3D plotting to matplotlib with same “look-and-feel”
●
It supplies an axes object that can create a 2D projection of a 3D scene
– Creation of 3D axes object
● Use ax = mplot3d.Axes3D(fig)
●
Use any standard axes creation method
with keyword projection='3d'
– ax = plt.subplot(111, projection='3d')
from mpl_toolkits import mplot3d
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
3D plotting
– 3D axes properties
● Z-axis: ax.set(..., zlabel='z', zticks=(-1,0,1))
● Orientation: ax.view_init(elev=30, azim=45)
In [1]: ax = plt.axes(projection='3d')
...: ax.view_init(elev=30, azim=45)
...: ax.set(xlabel='x', ylabel='y', zlabel='z')
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
3D plotting
– Natural plot extensions
● Line plots: ax.plot(x, y, z), ax.plot3D(x, y, z)
● Scatter plots: ax.scatter(x, y, z), ax.scatter3D(x, y, z)
In [1]: theta = np.linspace(-4*np.pi, 4*np.pi, 100)
...: z = np.linspace(-2, 2, 100)
...: r = z**2 + 1
...: x = r * np.sin(theta)
...: y = r * np.cos(theta)
In [2]: ax = plt.axes(projection='3d')
...: ax.plot(x, y, z, 'r')
...: ax.set(title='parametric curve')
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
3D plotting
– Surface plotting
● Wireframe plot: ax.plot_wireframe(X, Y, Z)
● Surface plot: ax.plot_surface(X, Y, Z)
– Surface options
●
Create coordinate matrices from coordinate vectors
– X, Y = np.meshgrid(x, y, sparse=False, copy=True)
●
Color maps: mapping between numeric values and colors
– Use keyword cmap
– Manipulated via module matplotlib.cm
– Examples: jet, hot, coolwarm, bone, ...
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
3D plotting
– Example
In [1]: x = np.arange(-5, 5, 0.25)
...: y = np.arange(-5, 5, 0.25)
...: X, Y = np.meshgrid(x, y)
...: R = np.sqrt(X**2 + Y**2)
...: Z = np.sin(R)
In [2]: plt.figure(figsize=(10, 4))
...: plt.suptitle('surface plots')
...: ax1 = plt.subplot(1, 2, 1, projection='3d')
...: ax1.plot_wireframe(X, Y, Z, color='black')
...: ax2 = plt.subplot(1, 2, 2, projection='3d')
...: ax2.plot_surface(X, Y, Z, cmap='coolwarm')
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Contour plotting
– Contour lines: basic syntax
– Other contour functions:
● Filled contours: plt.contourf(X, Y, Z, levels)
● Contour identification: plt.clabel(cs), plt.colorbar(cs)
● 3D contour lines (mplot3d): ax.contour(X, Y, Z, levels)
plt.contour(Z)
plt.contour(X, Y, Z)
plt.contour(X, Y, Z, levels)
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Contour plotting
– Example
In [1]: t = np.arange(-2, 2, 0.01)
...: X, Y = np.meshgrid(t, t)
...: Z = np.sin(X * np.pi / 2)
...: + np.cos(Y * np.pi / 4)
In [2]: plt.figure(figsize=(10, 4))
...: plt.subplot(1, 2, 1)
...: cs = plt.contour(X, Y, Z)
...: plt.clabel(cs)
...: plt.subplot(1, 2, 2)
...: cs = plt.contourf(X, Y, Z, cmap='coolwarm')
...: plt.colorbar(cs)
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Image plotting
– Image
●
A matrix of color intensities (via color map)
●
A matrix of RGB or RGBA colors (3D array of dept = 3 or 4)
– Image plots: basic syntax
– Other matrix visualization:
● Matrix values: plt.matshow(A)
● Matrix sparsity: plt.spy(A)
plt.imshow(img)
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Image plotting
– Example
In [1]: A = np.diag(np.arange(10, 21))
In [2]: plt.figure(figsize=(10, 4))
...: plt.subplot(2, 1, 1)
...: plt.imshow(A, cmap='summer')
...: plt.subplot(2, 1, 2)
...: plt.spy(A, marker='*')
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Image plotting
– Example: Mandelbrot set
●
Fractal set of complex numbers
● Definition: any c for which zi+1
= zi
2
+ c does
not diverge, starting from z0
= 0
● Property: limi→∞
sup | zi+1
| ≤ 2 for any valid c
In [1]: def mandelbrot(nx, ny, max_it=20):
...: # TODO
...: return M
In [2]: M = mandelbrot(501, 501, 50)
...: plt.imshow(M.T, cmap='flag')
...: plt.axis('off')
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Image plotting
– Example: Mandelbrot set
In [1]: def mandelbrot(nx, ny, max_it=20):
...: x = np.linspace(-2.0, 1.0, nx)
...: y = np.linspace(-1.5, 1.5, ny)
...: C = x[:,np.newaxis]
...: + 1.0j*y[np.newaxis,:]
...: Z = C.copy()
...: M = np.ones((nx, ny), dtype=bool)
...: for i in range(max_it):
...: Z[M] = Z[M]**2 + C[M]
...: M[np.abs(Z) > 2] = False
...: return M
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Colors
– Predefined colors
●
abbreviation: b, g, r, c, m, y, k, w
●
full name: blue, green, red, cyan, magenta, yellow, black, white, ...
– RGB/RGBA code
●
tuple of three or four float values in [0, 1]
●
a hexadecimal RGB or RGBA string
– Black and white
●
string representation of a float value in [0, 1]
– All string specifications of color are case-insensitive
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Colormaps
Lab Calc
2024-2025
Matplotlib: Python Plotting
●
Input and output
– Save figures
●
Most backends support png, pdf, eps, svg
– Image I/O
In [1]: plt.plot([1, 2, 4, 2])
...: plt.savefig('plot.png', format='png')
In [1]: img = plt.imread('elephant.png')
In [2]: plt.imshow(img)
In [3]: plt.imsave('new_elephant.png', img)
Ad

More Related Content

Similar to slides_python_ for basic operation of it (20)

PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...
Andrey Karpov
 
DDU Workshop Day-2 presentation (1).pptx
DDU Workshop Day-2 presentation (1).pptxDDU Workshop Day-2 presentation (1).pptx
DDU Workshop Day-2 presentation (1).pptx
ritu23mts5729
 
Data Visualization 2020_21
Data Visualization 2020_21Data Visualization 2020_21
Data Visualization 2020_21
Sangita Panchal
 
Py lecture5 python plots
Py lecture5 python plotsPy lecture5 python plots
Py lecture5 python plots
Yoshiki Satotani
 
Towards typesafe deep learning in scala
Towards typesafe deep learning in scalaTowards typesafe deep learning in scala
Towards typesafe deep learning in scala
Tongfei Chen
 
MATLABgraphPlotting.pptx
MATLABgraphPlotting.pptxMATLABgraphPlotting.pptx
MATLABgraphPlotting.pptx
PrabhakarSingh646829
 
Matplotlib yayyyyyyyyyyyyyin Python.pptx
Matplotlib yayyyyyyyyyyyyyin Python.pptxMatplotlib yayyyyyyyyyyyyyin Python.pptx
Matplotlib yayyyyyyyyyyyyyin Python.pptx
AamnaRaza1
 
Python chart plotting using Matplotlib.pptx
Python chart plotting using Matplotlib.pptxPython chart plotting using Matplotlib.pptx
Python chart plotting using Matplotlib.pptx
sonali sonavane
 
Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012
Wen-Wei Liao
 
Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)
Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)
Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)
Austin Benson
 
Python grass
Python grassPython grass
Python grass
Margherita Di Leo
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
PyData
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
g3_nittala
 
SCIPY-SYMPY.pdf
SCIPY-SYMPY.pdfSCIPY-SYMPY.pdf
SCIPY-SYMPY.pdf
FreddyGuzman19
 
Visualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptxVisualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptx
SharmilaMore5
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
Yung-Yu Chen
 
Primitives
PrimitivesPrimitives
Primitives
Nageswara Rao Gottipati
 
Unit III for data science engineering.pptx
Unit III for data science engineering.pptxUnit III for data science engineering.pptx
Unit III for data science engineering.pptx
rhsingh033
 
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
Austin Benson
 
Swift for tensorflow
Swift for tensorflowSwift for tensorflow
Swift for tensorflow
규영 허
 
PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...
Andrey Karpov
 
DDU Workshop Day-2 presentation (1).pptx
DDU Workshop Day-2 presentation (1).pptxDDU Workshop Day-2 presentation (1).pptx
DDU Workshop Day-2 presentation (1).pptx
ritu23mts5729
 
Data Visualization 2020_21
Data Visualization 2020_21Data Visualization 2020_21
Data Visualization 2020_21
Sangita Panchal
 
Towards typesafe deep learning in scala
Towards typesafe deep learning in scalaTowards typesafe deep learning in scala
Towards typesafe deep learning in scala
Tongfei Chen
 
Matplotlib yayyyyyyyyyyyyyin Python.pptx
Matplotlib yayyyyyyyyyyyyyin Python.pptxMatplotlib yayyyyyyyyyyyyyin Python.pptx
Matplotlib yayyyyyyyyyyyyyin Python.pptx
AamnaRaza1
 
Python chart plotting using Matplotlib.pptx
Python chart plotting using Matplotlib.pptxPython chart plotting using Matplotlib.pptx
Python chart plotting using Matplotlib.pptx
sonali sonavane
 
Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012Use the Matplotlib, Luke @ PyCon Taiwan 2012
Use the Matplotlib, Luke @ PyCon Taiwan 2012
Wen-Wei Liao
 
Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)
Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)
Tall-and-skinny Matrix Computations in MapReduce (ICME MR 2013)
Austin Benson
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
PyData
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
g3_nittala
 
Visualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptxVisualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptx
SharmilaMore5
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
Yung-Yu Chen
 
Unit III for data science engineering.pptx
Unit III for data science engineering.pptxUnit III for data science engineering.pptx
Unit III for data science engineering.pptx
rhsingh033
 
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
Tall-and-skinny Matrix Computations in MapReduce (ICME colloquium)
Austin Benson
 
Swift for tensorflow
Swift for tensorflowSwift for tensorflow
Swift for tensorflow
규영 허
 

Recently uploaded (20)

22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB
22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB
22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB
Guru Nanak Technical Institutions
 
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
Pierre Celestin Eyock
 
AI Chatbots & Software Development Teams
AI Chatbots & Software Development TeamsAI Chatbots & Software Development Teams
AI Chatbots & Software Development Teams
Joe Krall
 
Understand water laser communication using Arduino laser and solar panel
Understand water laser communication using Arduino laser and solar panelUnderstand water laser communication using Arduino laser and solar panel
Understand water laser communication using Arduino laser and solar panel
NaveenBotsa
 
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdfIBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
VigneshPalaniappanM
 
Deepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber ThreatsDeepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber Threats
RaviKumar256934
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptxUnleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
SanjeetMishra29
 
Introduction to Additive Manufacturing(3D printing)
Introduction to Additive Manufacturing(3D printing)Introduction to Additive Manufacturing(3D printing)
Introduction to Additive Manufacturing(3D printing)
vijimech408
 
IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...
IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...
IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...
ssuserd9338b
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
Jimmy Lai
 
Environment .................................
Environment .................................Environment .................................
Environment .................................
shadyozq9
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Journal of Soft Computing in Civil Engineering
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
VISHAL KUMAR SINGH Latest Resume with updated details
VISHAL KUMAR SINGH Latest Resume with updated detailsVISHAL KUMAR SINGH Latest Resume with updated details
VISHAL KUMAR SINGH Latest Resume with updated details
Vishal Kumar Singh
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
Pierre Celestin Eyock
 
AI Chatbots & Software Development Teams
AI Chatbots & Software Development TeamsAI Chatbots & Software Development Teams
AI Chatbots & Software Development Teams
Joe Krall
 
Understand water laser communication using Arduino laser and solar panel
Understand water laser communication using Arduino laser and solar panelUnderstand water laser communication using Arduino laser and solar panel
Understand water laser communication using Arduino laser and solar panel
NaveenBotsa
 
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdfIBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
VigneshPalaniappanM
 
Deepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber ThreatsDeepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber Threats
RaviKumar256934
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptxUnleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
SanjeetMishra29
 
Introduction to Additive Manufacturing(3D printing)
Introduction to Additive Manufacturing(3D printing)Introduction to Additive Manufacturing(3D printing)
Introduction to Additive Manufacturing(3D printing)
vijimech408
 
IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...
IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...
IPC-7711D-7721D_ EN 2023 TOC Rework, Modification and Repair of Electronic As...
ssuserd9338b
 
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
Jimmy Lai
 
Environment .................................
Environment .................................Environment .................................
Environment .................................
shadyozq9
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
VISHAL KUMAR SINGH Latest Resume with updated details
VISHAL KUMAR SINGH Latest Resume with updated detailsVISHAL KUMAR SINGH Latest Resume with updated details
VISHAL KUMAR SINGH Latest Resume with updated details
Vishal Kumar Singh
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Ad

slides_python_ for basic operation of it

  • 2. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Overview – Anatomy of a figure ● Figures and axes – 2D plotting ● Standard line plotting ● Other plotting + text annotation – 3D plotting ● 3D axes + 3D line/surface plotting – Other plotting ● Contours + image visualization
  • 3. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Matplotlib – Mathematical plotting library – Python extension for graphics ● Suited for visualization of data and creation of high-quality figures ● Extensive package for 2D plotting, and add-on toolkits for 3D plotting ● Pyplot: MATLAB-like procedural interface to the object-oriented API – Import convention from matplotlib import pyplot as plt import matplotlib.pyplot as plt
  • 4. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Matplotlib – Mathematical plotting library – Interactive matplotlib sessions ● IPython console ● Jupyter notebook %matplotlib %matplotlib inline %matplotlib notebook
  • 5. Lab Calc 2024-2025 Matplotlib: Python Plotting ● A simple plot – Syntax is array-based – If not interactive, also write: In [1]: x = np.linspace(0, 2.0*np.pi, 100) In [2]: cx, sx = np.cos(x), np.sin(x) In [3]: plt.plot(x, cx) ...: plt.plot(x, sx) ...: plt.show()
  • 6. Lab Calc 2024-2025 Matplotlib: Python Plotting ● A simple plot – Default settings (see also plt.rcParams) In [3]: plt.figure(figsize=(6.0, 4.0), dpi=72.0) ...: plt.subplot(1, 1, 1) ...: plt.plot(x, cx, color='#1f77b4', ...: linewidth=1.5, linestyle='-') ...: plt.plot(x, sx, color='#ff7f0e', ...: linewidth=1.5, linestyle='-') ...: plt.xlim(-0.1*np.pi, 2.1*np.pi) ...: plt.xticks(np.linspace(0, 6, 7)) ...: plt.ylim(-1.1, 1.1) ...: plt.yticks(np.linspace(-1, 1, 9))
  • 8. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Anatomy – Hierarchical structure – Figure ● The overall window on which everything is drawn ● Components: one or more axes, suptitle, ... plt.figure(num=None, figure index, 1-based figsize=None, (width, height) in inches dpi=None, resolution facecolor=None, background color ...)
  • 9. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Anatomy – Axes ● The area on which the data is plotted ● Belongs to a figure, placed arbitrarily (axes) or in grid (subplot) ● Components: x/y-axis, ticks, spines, labels, title, legend, ... ● All methods of active axes are directly callable via Pyplot interface plt.axes((left, bottom, width, height), **kwargs) plt.subplot(nrows, ncols, index, **kwargs) **kwargs: facecolor=None, polar=False, ...
  • 10. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Anatomy – Axes components ● Get or set limits: plt.xlim, plt.ylim, plt.axis – left, right = plt.xlim() – plt.xlim(left, right) – plt.axis((left, right, bottom, top)), plt.axis('equal') ● Get or set ticks: plt.xticks, plt.yticks – locs, labels = plt.xticks() – plt.xticks(np.arange(3), ('a', 'b', 'c')) ● Set labels: plt.xlabel(txt), plt.ylabel(txt) ● Set title: plt.title(txt) ● Others: plt.box(), plt.grid(), ...
  • 11. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Anatomy – Example In [1]: plt.figure(facecolor='silver') ...: plt.subplot(1, 2, 1) ...: plt.title('subplot') ...: plt.axes((0.4, 0.3, 0.4, 0.4)) ...: plt.xlim(1, 5) ...: plt.ylim(-5, -1) ...: plt.title('axes')
  • 12. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 2D plotting – Standard line plotting: basic syntax ● Connect data points (x, y) with optional format string ● Color (c): b, g, r, c, m, y, k, w ● Linestyle (l): -, --, -., : ● Marker (m): o, *, ., +, x, s, d, ^, <, >, p, h, ... plt.plot(y) plt.plot(x, y) plt.plot(x, y, 'clm')
  • 13. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 2D plotting – Standard line plotting: advanced syntax – Multiple plots per axes possible – Legend: plt.plot(x, y, **kwargs) **kwargs: color, linestyle, linewidth, marker, markeredgecolor, markeredgewidth, markerfacecolor, markersize, label, ... plt.legend(('a', 'b', 'c'), loc='upper right')
  • 14. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 2D plotting – For full plot details, check out plt.plot? – Example In [1]: t = np.arange(0.0, 2.0, 0.01) ...: s = 1.0 + np.sin(2.0*np.pi*t) In [2]: plt.axes(facecolor='silver') ...: plt.plot(t, s, 'r') ...: plt.xlabel('time (s)') ...: plt.ylabel('voltage (mV)') ...: plt.title('About as simple as it gets') ...: plt.grid()
  • 15. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 2D plotting – Plotting methods are actually connected to axes ● Pyplot provides an interface to the active axes In [1]: t = np.arange(0.0, 2.0, 0.01) ...: s = 1.0 + np.sin(2.0*np.pi*t) In [2]: ax = plt.axes() ...: ax.plot(t, s, 'r') ...: ax.set(facecolor='silver', ...: xlabel='time (s)', ...: ylabel='voltage (mV)', ...: title='About as simple as it gets') ...: ax.grid()
  • 16. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 2D plotting – Example: data statistics ● Data in the file “populations.txt” describes the populations of hares, lynxes and carrots in northern Canada during 20 years ● Load the data and plot it ● Compute the mean populations over time ● Which species has the highest population each year? # year hare lynx carrot 1900 30e3 4e3 48300 1901 47.2e3 6.1e3 48200 1902 70.2e3 9.8e3 41500 ...
  • 17. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 2D plotting – Example: data statistics ● Load the data and plot it In [1]: data = np.loadtxt('populations.txt') In [2]: year, hares, lynxes, carrots = data.T In [3]: plt.axes((0.2, 0.1, 0.6, 0.8)) ...: plt.plot(year, hares) ...: plt.plot(year, lynxes) ...: plt.plot(year, carrots) ...: plt.xticks(np.arange(1900, 1921, 5)) ...: plt.yticks(np.arange(1, 9) * 10000) ...: plt.legend(('Hare', 'Lynx', 'Carrot'))
  • 18. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 2D plotting – Example: data statistics ● Compute the mean populations over time ● Which species has the highest population each year? In [4]: populations = data[:, 1:] In [5]: populations.mean(axis=0) Out[5]: array([34080.9524, 20166.6667, 42400.]) In [6]: populations.std(axis=0) Out[6]: array([20897.9065, 16254.5915, 3322.5062]) In [7]: populations.argmax(axis=1) Out[7]: array([2, 2, 0, 0, 1, 1, 2, 2, 2, 2, ...])
  • 19. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 2D plotting – Other plotting ● Log plots: plt.loglog(x, y), plt.semilogx(x, y), plt.semilogy(x, y) ● Polar plots: plt.polar(theta, r) ● Scatter plots: plt.scatter(x, y) ● Bar graphs: plt.bar(x, height), plt.barh(y, width) ● Pie charts: plt.pie(x) ● Histogram: plt.hist(x, bins=None) ● Filled curves: plt.fill(x, y), plt.fill_between(x, y1, y2=0) – For full method details, check out plt.method?
  • 20. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 2D plotting – Example In [1]: names = ['a', 'b', 'c', 'd'] ...: values = [1, 10, 40, 50] In [2]: plt.figure(figsize=(3, 9)) ...: plt.subplot(3, 1, 1) ...: plt.bar(names, values) ...: plt.subplot(3, 1, 2) ...: plt.scatter(names, values) ...: plt.subplot(3, 1, 3) ...: plt.fill_between(names, values) ...: plt.suptitle( ...: 'categorical plotting', y=0.92)
  • 21. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 2D plotting – Text ● Axes text: plt.title(txt), plt.xlabel(txt), plt.ylabel(txt) ● Plain text: plt.text(x, y, txt) ● Annotation: plt.annotate(txt, xy=(x, y), xytext=(xt, yt), arrowprops={'arrowstyle':'->'}) ● Extensive math rendering engine – Support for TeX markup inside dollar signs ($) – Use raw strings (precede the quotes with an 'r') plt.title('alpha > beta') # normal text plt.title(r'$alpha > beta$') # math text
  • 22. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 2D plotting – Example In [1]: x = np.linspace(0, 2.0*np.pi, 25) In [2]: plt.scatter(x, np.sin(x)) ...: plt.ylim(-2, 2) ...: plt.text(3, 1, 'sine function', ...: fontsize=18, style='italic') ...: plt.annotate('localnmax', ...: xy=(np.pi/2.0, 1), xytext=(1.3, 0), ...: arrowprops={'arrowstyle':'->'}) ...: plt.annotate('localnmin', ...: xy=(np.pi*3.0/2.0, -1), xytext=(4.5, -0.4), ...: arrowprops={'arrowstyle':'->'})
  • 23. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 3D plotting – Module mplot3d ● This toolkit adds simple 3D plotting to matplotlib with same “look-and-feel” ● It supplies an axes object that can create a 2D projection of a 3D scene – Creation of 3D axes object ● Use ax = mplot3d.Axes3D(fig) ● Use any standard axes creation method with keyword projection='3d' – ax = plt.subplot(111, projection='3d') from mpl_toolkits import mplot3d
  • 24. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 3D plotting – 3D axes properties ● Z-axis: ax.set(..., zlabel='z', zticks=(-1,0,1)) ● Orientation: ax.view_init(elev=30, azim=45) In [1]: ax = plt.axes(projection='3d') ...: ax.view_init(elev=30, azim=45) ...: ax.set(xlabel='x', ylabel='y', zlabel='z')
  • 25. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 3D plotting – Natural plot extensions ● Line plots: ax.plot(x, y, z), ax.plot3D(x, y, z) ● Scatter plots: ax.scatter(x, y, z), ax.scatter3D(x, y, z) In [1]: theta = np.linspace(-4*np.pi, 4*np.pi, 100) ...: z = np.linspace(-2, 2, 100) ...: r = z**2 + 1 ...: x = r * np.sin(theta) ...: y = r * np.cos(theta) In [2]: ax = plt.axes(projection='3d') ...: ax.plot(x, y, z, 'r') ...: ax.set(title='parametric curve')
  • 26. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 3D plotting – Surface plotting ● Wireframe plot: ax.plot_wireframe(X, Y, Z) ● Surface plot: ax.plot_surface(X, Y, Z) – Surface options ● Create coordinate matrices from coordinate vectors – X, Y = np.meshgrid(x, y, sparse=False, copy=True) ● Color maps: mapping between numeric values and colors – Use keyword cmap – Manipulated via module matplotlib.cm – Examples: jet, hot, coolwarm, bone, ...
  • 27. Lab Calc 2024-2025 Matplotlib: Python Plotting ● 3D plotting – Example In [1]: x = np.arange(-5, 5, 0.25) ...: y = np.arange(-5, 5, 0.25) ...: X, Y = np.meshgrid(x, y) ...: R = np.sqrt(X**2 + Y**2) ...: Z = np.sin(R) In [2]: plt.figure(figsize=(10, 4)) ...: plt.suptitle('surface plots') ...: ax1 = plt.subplot(1, 2, 1, projection='3d') ...: ax1.plot_wireframe(X, Y, Z, color='black') ...: ax2 = plt.subplot(1, 2, 2, projection='3d') ...: ax2.plot_surface(X, Y, Z, cmap='coolwarm')
  • 28. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Contour plotting – Contour lines: basic syntax – Other contour functions: ● Filled contours: plt.contourf(X, Y, Z, levels) ● Contour identification: plt.clabel(cs), plt.colorbar(cs) ● 3D contour lines (mplot3d): ax.contour(X, Y, Z, levels) plt.contour(Z) plt.contour(X, Y, Z) plt.contour(X, Y, Z, levels)
  • 29. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Contour plotting – Example In [1]: t = np.arange(-2, 2, 0.01) ...: X, Y = np.meshgrid(t, t) ...: Z = np.sin(X * np.pi / 2) ...: + np.cos(Y * np.pi / 4) In [2]: plt.figure(figsize=(10, 4)) ...: plt.subplot(1, 2, 1) ...: cs = plt.contour(X, Y, Z) ...: plt.clabel(cs) ...: plt.subplot(1, 2, 2) ...: cs = plt.contourf(X, Y, Z, cmap='coolwarm') ...: plt.colorbar(cs)
  • 30. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Image plotting – Image ● A matrix of color intensities (via color map) ● A matrix of RGB or RGBA colors (3D array of dept = 3 or 4) – Image plots: basic syntax – Other matrix visualization: ● Matrix values: plt.matshow(A) ● Matrix sparsity: plt.spy(A) plt.imshow(img)
  • 31. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Image plotting – Example In [1]: A = np.diag(np.arange(10, 21)) In [2]: plt.figure(figsize=(10, 4)) ...: plt.subplot(2, 1, 1) ...: plt.imshow(A, cmap='summer') ...: plt.subplot(2, 1, 2) ...: plt.spy(A, marker='*')
  • 32. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Image plotting – Example: Mandelbrot set ● Fractal set of complex numbers ● Definition: any c for which zi+1 = zi 2 + c does not diverge, starting from z0 = 0 ● Property: limi→∞ sup | zi+1 | ≤ 2 for any valid c In [1]: def mandelbrot(nx, ny, max_it=20): ...: # TODO ...: return M In [2]: M = mandelbrot(501, 501, 50) ...: plt.imshow(M.T, cmap='flag') ...: plt.axis('off')
  • 33. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Image plotting – Example: Mandelbrot set In [1]: def mandelbrot(nx, ny, max_it=20): ...: x = np.linspace(-2.0, 1.0, nx) ...: y = np.linspace(-1.5, 1.5, ny) ...: C = x[:,np.newaxis] ...: + 1.0j*y[np.newaxis,:] ...: Z = C.copy() ...: M = np.ones((nx, ny), dtype=bool) ...: for i in range(max_it): ...: Z[M] = Z[M]**2 + C[M] ...: M[np.abs(Z) > 2] = False ...: return M
  • 34. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Colors – Predefined colors ● abbreviation: b, g, r, c, m, y, k, w ● full name: blue, green, red, cyan, magenta, yellow, black, white, ... – RGB/RGBA code ● tuple of three or four float values in [0, 1] ● a hexadecimal RGB or RGBA string – Black and white ● string representation of a float value in [0, 1] – All string specifications of color are case-insensitive
  • 36. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Colormaps
  • 37. Lab Calc 2024-2025 Matplotlib: Python Plotting ● Input and output – Save figures ● Most backends support png, pdf, eps, svg – Image I/O In [1]: plt.plot([1, 2, 4, 2]) ...: plt.savefig('plot.png', format='png') In [1]: img = plt.imread('elephant.png') In [2]: plt.imshow(img) In [3]: plt.imsave('new_elephant.png', img)