SlideShare a Scribd company logo
1 of 44
Download to read offline
MATPLOTLIB
VIC
2016/7/28
OUTLINE
• Self introduction
• What is matplotlib?
• How to use matplotlib?
• Some examples
• Demo
SELF INTRODUCTION
• Vic
• NRL
• Python web
•
• Twitter: https://twitter.com/vrootic
• GitHub: https://github.com/vrootic
• Email: vrootic@gmail.com
3
WHAT CAN IT DO?
WHAT IS MATPLOTLIB?
• Matplotlib Python lib
Matlab Matplotlib cmd interface
IPython script
5
WHAT IS MATPLOTLIB?
•
• Python data Mathematica
Adaptive Plotting
• Multiplot
• LaTex render
6
WHAT IS MATPLOTLIB?
•
• (cmd & OO) API
•
7
HOW TO USE MATPLOTLIB?
• Environment
• python==3.5
• numpy==1.11.0
• matplotlib==1.5.0
• jupyter notebook
• https://gist.github.com/vrootic/
9c857c0987788774bf3d26682af3152e
8
HOW TO USE MATPLOTLIB?
• jupyter notebook
• python shell
9
JUPYTER NOTEBOOK
• Add magic command
• %pylab inline(not recommeded)
• %matplotlib inline
10
WARM UP(IN JUPYTER)
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
# plot(X-axis, Y-axis, style=‘b-’)
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
11
WARM UP(IN JUPYTER)
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
c1 = [1, 2, 3, 4] #
c2 = [1, 4, 9, 16] #
plt.xlim(-1, 4) # x
plt.ylim(0, 18) # y
plt.plot(c1, 'b-')
plt.plot(c2, 'ro')
12
PRACTICE 1
Hint:
pi = np.pi
x = np.linspace(-pi, pi, 256)
sinx = np.sin(x)
cosx = np.cos(x)
13
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
X = np.linspace(-np.pi, np.pi, 256)
cosx = np.cos(X)
sinx = np.sin(X)
plt.xlim(-4.0, 4.0)
plt.ylim(-1.0, 1.0)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])
plt.yticks([-1, 0, 1])
plt.plot(X, cosx, 'b-', linewidth=1.0)
plt.plot(X, sinx, 'r-', linewidth=1.0)
BASIC EXAMPLES(IN JUPYTER)
X = np.linspace(-np.pi, np.pi, 256)
cosx = np.cos(X)
sinx = np.sin(X)
plt.xlim(-4.0, 4.0)
plt.ylim(-1.0, 1.0)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])
plt.yticks([-1, 0, 1])
plt.plot(X, cosx, 'b-', linewidth=1.0)
plt.plot(X, sinx, 'r-', linewidth=1.0)
15
(CHANGE AXIS DISPLAY)
BASIC EXAMPLES(IN JUPYTER)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r'$-pi$', r'$-pi/2$', r'$0$',
r'$+pi/2$', r'$+pi$'])
plt.yticks([-1, 0, +1],
[r'$-1$', r'$0$', r'$+1$'])
16
(ADD LEGEND)
BASIC EXAMPLES(IN JUPYTER)
plt.plot(X, cosx, 'b-', linewidth=1.0, label="cosine")
plt.plot(X, sinx, 'r-', linewidth=1.0, label="sine")
plt.legend(loc=‘upper left’)
17
(CHANGE SPINE)
BASIC EXAMPLES(IN JUPYTER)
ax = plt.gca() # spine
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines[‘left’].set_position(('data',np.pi/2))
18
(SUBPLOT)
BASIC EXAMPLES(IN JUPYTER)
plt.axes([.1, .1, 1, 1])
plt.axes([.2, .2, .3, .3],
axisbg='green')
plt.subplot(2, 1, 1, axisbg=‘y’)
plt.subplot(2, 1, 2)
19
plt.subplot(2, 1, 1)
plt.subplot(2, 1, 2)
PRACTICE 2
20
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
for idx, color in enumerate("rgbyck"):
plt.subplot(3, 2, 1+idx, axisbg=color)
BASIC EXAMPLES
http://matplotlib.org/users/gridspec.html
22
PLOT STYLE CHEATSHEET
Ex: Draw blue star line:
plot(x, y, ‘b*’)
23
(SCATTER)
SOME EXAMPLES
n = 1024
X = np.random.normal(0, 1, n)
Y = np.random.normal(0, 1, n)
T = np.arctan2(Y,X)
plt.axes([0.025,0.025,0.95,0.95])
plt.scatter(X,Y, s=75, c=T, alpha=.5)
plt.xlim(-1.5,1.5)
plt.xticks([])
plt.ylim(-1.5,1.5)
plt.yticks([])
24
# matplotlib.colors.Colormap
(HISTOGRAM)
SOME EXAMPLES
25
n = 7
X = np.arange(n)
Y1 = [0.5, 0.6, 0.65, 0.7, 0.8, 0.55, 0.9]
plt.bar(X, Y1, facecolor='#9999ff')
for x,y in zip(X,Y1):
if y == sorted(Y1)[1]:
plt.text(x+0.4, y+0.05, '%.2f' % y,
ha='center', va= 'bottom')
plt.ylim(0, 1.25)
(HISTOGRAM)
SOME EXAMPLES
n = 12
X = np.arange(n)
Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n)
plt.bar(X, +Y1, facecolor='#9999ff',
edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999',
edgecolor='white')
for x,y in zip(X,Y1):
plt.text(x+0.4, y+0.05, '%.2f' % y,
ha='center', va= 'bottom')
plt.ylim(-1.25,+1.25)
26
(CONTOUR)
SOME EXAMPLES
def f(x,y):
return (1-x/2+x**5+y**3)*np.exp(-x**2 - y**2)
n = 256
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)
X,Y = np.meshgrid(x,y)
plt.contourf(X, Y, f(X,Y), 8, alpha=.75,cmap=‘jet')
C = plt.contour(X, Y, f(X,Y), 8, colors='black',
linewidth=.5)
27
(COLORMAP)
SOME EXAMPLES
def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-
y**2)
n = 10
x = np.linspace(-3,3,3.5*n)
y = np.linspace(-3,3,3.0*n)
X,Y = np.meshgrid(x,y)
Z = f(X,Y)
plt.axes([0.025,0.025,0.95,0.95])
plt.imshow(Z,interpolation='nearest',
cmap='bone', origin='lower')
plt.colorbar(shrink=.92)
plt.xticks([]), plt.yticks([])
28
GALLERY
SOME EXAMPLES
http://matplotlib.org/gallery.html
http://www.labri.fr/perso/nrougier/coding/gallery/
29
LOCATION AWARE SENSING SYSTEM
LASS
•
• https://www.facebook.com/groups/1607718702812067/
• Hackpad
• https://lass.hackpad.com/LASS-README-DtZ5T6DXLbu
•
• http://lass-net.org
LOCATION AWARE SENSING SYSTEM
LASS
•


•
LASS Bottom up
PRACTICE 3
Data source: http://nrl.iis.sinica.edu.tw/LASS/last-all-airbox.json
32
33
Document
34
jupyter
35
shell
36
EXAMPLES(SHELL)
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(0, 1, 256)
plt.plot(X)
plt.show() #
37
38
39
BASIC EXAMPLES(SHELL)
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(0, 1, 256)
fig1 = plt.figure("fig_1")
ax1 = fig1.add_subplot(1, 1, 1)
ax1.plot(X)
fig1.savefig(“fig_1.jpg")
plt.show()
40
SHELL VS. JUPYTER
COMPARE
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
X = np.linspace(0, 1, 256)
plt.plot(X)
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(0, 1, 256)
fig1 = plt.figure("fig_1")
ax1 = fig1.add_subplot(1, 1, 1)
ax1.plot(X)
fig1.savefig(“fig_1.jpg")
plt.show()
SUMMARY
• Matplotlib class Figure -> Axes -> (Line2D,
Text, etc) Figure Axes( ) matplotlib
Axes
• -> -> Matplotlib gallery
• jupyter matplotlib inline
environment
•
(ex. )
42
DEMO
43
REFERENCE
• http://liam0205.me/2014/09/11/matplotlib-tutorial-zh-cn/
• http://www.cnblogs.com/wei-li/archive/2012/05/23/2506940.html
• http://darksair.org/wiki/matplotlib.html
• http://2484055.blog.51cto.com/2474055/1334257
• http://matplotlib.org/users/pyplot_tutorial.html
• http://matplotlib.org/users/style_sheets.html
• http://rickchungtw-blog.logdown.com/tags/Matplotlib
• http://yukuan.blogspot.tw/2006/12/analyze-sunspots.html
• http://stackoverflow.com/questions/12987624/confusion-between-numpy-scipy-matplotlib-and-pylab
• http://hhtucode.blogspot.tw/2013/04/ml-gradient-descent-algorithm.html
• http://matplotlib.org/examples/subplots_axes_and_figures/subplot_demo.html
• http://matplotlib.org/users/shell.html
44

More Related Content

What's hot

What's hot (20)

Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib. Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib.
 
Python for Scientific Computing -- Ricardo Cruz
Python for Scientific Computing -- Ricardo CruzPython for Scientific Computing -- Ricardo Cruz
Python for Scientific Computing -- Ricardo Cruz
 
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYAPYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
 
The TensorFlow dance craze
The TensorFlow dance crazeThe TensorFlow dance craze
The TensorFlow dance craze
 
Matplotlib demo code
Matplotlib demo codeMatplotlib demo code
Matplotlib demo code
 
Functional Programming, simplified
Functional Programming, simplifiedFunctional Programming, simplified
Functional Programming, simplified
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage Collection
 
Intermediate python ch1_slides
Intermediate python ch1_slidesIntermediate python ch1_slides
Intermediate python ch1_slides
 
Basic of python for data analysis
Basic of python for data analysisBasic of python for data analysis
Basic of python for data analysis
 
Introduction to numpy
Introduction to numpyIntroduction to numpy
Introduction to numpy
 
A born-again programmer's odyssey
A born-again programmer's odysseyA born-again programmer's odyssey
A born-again programmer's odyssey
 
What Haskell taught us when we were not looking! - Eric Torreborre - Codemoti...
What Haskell taught us when we were not looking! - Eric Torreborre - Codemoti...What Haskell taught us when we were not looking! - Eric Torreborre - Codemoti...
What Haskell taught us when we were not looking! - Eric Torreborre - Codemoti...
 
Introduction to Python and Matplotlib
Introduction to Python and MatplotlibIntroduction to Python and Matplotlib
Introduction to Python and Matplotlib
 
Gráficas en python
Gráficas en python Gráficas en python
Gráficas en python
 
intro to scikits.learn
intro to scikits.learnintro to scikits.learn
intro to scikits.learn
 
Dma
DmaDma
Dma
 
Matlab plotting
Matlab plottingMatlab plotting
Matlab plotting
 
Network Analysis with networkX : Real-World Example-2
Network Analysis with networkX : Real-World Example-2Network Analysis with networkX : Real-World Example-2
Network Analysis with networkX : Real-World Example-2
 
Seeing Like Software
Seeing Like SoftwareSeeing Like Software
Seeing Like Software
 
Deep Learning in theano
Deep Learning in theanoDeep Learning in theano
Deep Learning in theano
 

Viewers also liked

GR.jl - Plotting for Julia based on GR
GR.jl - Plotting for Julia based on GRGR.jl - Plotting for Julia based on GR
GR.jl - Plotting for Julia based on GR
Josef Heinen
 
Bloomberg Brief, Hedge Funds
Bloomberg Brief, Hedge FundsBloomberg Brief, Hedge Funds
Bloomberg Brief, Hedge Funds
David Dent
 
Workindenmark herning
Workindenmark herningWorkindenmark herning
Workindenmark herning
Morten Vium
 
Michael Timm (Stockler)
Michael Timm (Stockler)Michael Timm (Stockler)
Michael Timm (Stockler)
Luiz Valeriano
 
A primera-guerra-mundial-1914-1918
A primera-guerra-mundial-1914-1918A primera-guerra-mundial-1914-1918
A primera-guerra-mundial-1914-1918
samudiego
 
Best Bday Gift 2014
Best Bday Gift 2014Best Bday Gift 2014
Best Bday Gift 2014
Ariel Chen
 

Viewers also liked (20)

Data Visualization in matplotlib
Data Visualization in matplotlibData Visualization in matplotlib
Data Visualization in matplotlib
 
Life after Matplotlib: Harder, Better, Faster, Stronger by Kayla Lacovino
Life after Matplotlib: Harder, Better, Faster, Stronger by Kayla LacovinoLife after Matplotlib: Harder, Better, Faster, Stronger by Kayla Lacovino
Life after Matplotlib: Harder, Better, Faster, Stronger by Kayla Lacovino
 
GR.jl - Plotting for Julia based on GR
GR.jl - Plotting for Julia based on GRGR.jl - Plotting for Julia based on GR
GR.jl - Plotting for Julia based on GR
 
Glenn Edwards: 'Defining the feral camel problem'. Reducing feral camel impac...
Glenn Edwards: 'Defining the feral camel problem'. Reducing feral camel impac...Glenn Edwards: 'Defining the feral camel problem'. Reducing feral camel impac...
Glenn Edwards: 'Defining the feral camel problem'. Reducing feral camel impac...
 
Presentación11
Presentación11Presentación11
Presentación11
 
Social Bite Build A Village - Edinburgh
Social Bite Build A Village - EdinburghSocial Bite Build A Village - Edinburgh
Social Bite Build A Village - Edinburgh
 
ASOBOSQUE
ASOBOSQUEASOBOSQUE
ASOBOSQUE
 
Docker Docker - Docker Security - Docker
Docker Docker - Docker Security - DockerDocker Docker - Docker Security - Docker
Docker Docker - Docker Security - Docker
 
Introduction to plotting in Python
Introduction to plotting in Python Introduction to plotting in Python
Introduction to plotting in Python
 
Up and Down the Python Data & Web Visualization Stack by Rob Story PyData SV ...
Up and Down the Python Data & Web Visualization Stack by Rob Story PyData SV ...Up and Down the Python Data & Web Visualization Stack by Rob Story PyData SV ...
Up and Down the Python Data & Web Visualization Stack by Rob Story PyData SV ...
 
Building a Compelling Business Case for Continuous Delivery
Building a Compelling Business Case for Continuous DeliveryBuilding a Compelling Business Case for Continuous Delivery
Building a Compelling Business Case for Continuous Delivery
 
Data Visualization(s) Using Python
Data Visualization(s) Using PythonData Visualization(s) Using Python
Data Visualization(s) Using Python
 
Daniel O' Connell
Daniel O' ConnellDaniel O' Connell
Daniel O' Connell
 
Bloomberg Brief, Hedge Funds
Bloomberg Brief, Hedge FundsBloomberg Brief, Hedge Funds
Bloomberg Brief, Hedge Funds
 
Workindenmark herning
Workindenmark herningWorkindenmark herning
Workindenmark herning
 
Michael Timm (Stockler)
Michael Timm (Stockler)Michael Timm (Stockler)
Michael Timm (Stockler)
 
A primera-guerra-mundial-1914-1918
A primera-guerra-mundial-1914-1918A primera-guerra-mundial-1914-1918
A primera-guerra-mundial-1914-1918
 
LinkedIn foredrag hos FTF-A i Odense
LinkedIn foredrag hos FTF-A i OdenseLinkedIn foredrag hos FTF-A i Odense
LinkedIn foredrag hos FTF-A i Odense
 
Best Bday Gift 2014
Best Bday Gift 2014Best Bday Gift 2014
Best Bday Gift 2014
 
Japan Disaster
Japan Disaster Japan Disaster
Japan Disaster
 

Similar to Matplotlib 簡介與使用

matplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guidematplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guide
Arulalan T
 
Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...
Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...
Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...
GapData Institute
 

Similar to Matplotlib 簡介與使用 (20)

SciSmalltalk: Doing Science with Agility
SciSmalltalk: Doing Science with AgilitySciSmalltalk: Doing Science with Agility
SciSmalltalk: Doing Science with Agility
 
Deep Learning meetup
Deep Learning meetupDeep Learning meetup
Deep Learning meetup
 
UNit-III. part 2.pdf
UNit-III. part 2.pdfUNit-III. part 2.pdf
UNit-III. part 2.pdf
 
1 matlab basics
1 matlab basics1 matlab basics
1 matlab basics
 
Numba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPyNumba: Array-oriented Python Compiler for NumPy
Numba: Array-oriented Python Compiler for NumPy
 
Presentation: Plotting Systems in R
Presentation: Plotting Systems in RPresentation: Plotting Systems in R
Presentation: Plotting Systems in R
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
 
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...
 
Dynamic Binary Analysis and Obfuscated Codes
Dynamic Binary Analysis and Obfuscated Codes Dynamic Binary Analysis and Obfuscated Codes
Dynamic Binary Analysis and Obfuscated Codes
 
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
 
Python for Machine Learning(MatPlotLib).pptx
Python for Machine Learning(MatPlotLib).pptxPython for Machine Learning(MatPlotLib).pptx
Python for Machine Learning(MatPlotLib).pptx
 
matplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guidematplotlib-installatin-interactive-contour-example-guide
matplotlib-installatin-interactive-contour-example-guide
 
Vectorization in ATLAS
Vectorization in ATLASVectorization in ATLAS
Vectorization in ATLAS
 
AOP on Android
AOP on AndroidAOP on Android
AOP on Android
 
Tips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native codeTips and tricks for building high performance android apps using native code
Tips and tricks for building high performance android apps using native code
 
Visualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptxVisualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptx
 
Matplotlib Review 2021
Matplotlib Review 2021Matplotlib Review 2021
Matplotlib Review 2021
 
Matplotlib_Complete review_2021_abridged_version
Matplotlib_Complete review_2021_abridged_versionMatplotlib_Complete review_2021_abridged_version
Matplotlib_Complete review_2021_abridged_version
 
Nodejs性能分析优化和分布式设计探讨
Nodejs性能分析优化和分布式设计探讨Nodejs性能分析优化和分布式设计探讨
Nodejs性能分析优化和分布式设计探讨
 
Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...
Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...
Matúš Cimerman: Building AI data pipelines using PySpark, PyData Bratislava M...
 

Recently uploaded

Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 

Matplotlib 簡介與使用

  • 2. OUTLINE • Self introduction • What is matplotlib? • How to use matplotlib? • Some examples • Demo
  • 3. SELF INTRODUCTION • Vic • NRL • Python web • • Twitter: https://twitter.com/vrootic • GitHub: https://github.com/vrootic • Email: vrootic@gmail.com 3
  • 5. WHAT IS MATPLOTLIB? • Matplotlib Python lib Matlab Matplotlib cmd interface IPython script 5
  • 6. WHAT IS MATPLOTLIB? • • Python data Mathematica Adaptive Plotting • Multiplot • LaTex render 6
  • 7. WHAT IS MATPLOTLIB? • • (cmd & OO) API • 7
  • 8. HOW TO USE MATPLOTLIB? • Environment • python==3.5 • numpy==1.11.0 • matplotlib==1.5.0 • jupyter notebook • https://gist.github.com/vrootic/ 9c857c0987788774bf3d26682af3152e 8
  • 9. HOW TO USE MATPLOTLIB? • jupyter notebook • python shell 9
  • 10. JUPYTER NOTEBOOK • Add magic command • %pylab inline(not recommeded) • %matplotlib inline 10
  • 11. WARM UP(IN JUPYTER) import matplotlib.pyplot as plt import numpy as np %matplotlib inline # plot(X-axis, Y-axis, style=‘b-’) plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) 11
  • 12. WARM UP(IN JUPYTER) import matplotlib.pyplot as plt import numpy as np %matplotlib inline c1 = [1, 2, 3, 4] # c2 = [1, 4, 9, 16] # plt.xlim(-1, 4) # x plt.ylim(0, 18) # y plt.plot(c1, 'b-') plt.plot(c2, 'ro') 12
  • 13. PRACTICE 1 Hint: pi = np.pi x = np.linspace(-pi, pi, 256) sinx = np.sin(x) cosx = np.cos(x) 13
  • 14. import numpy as np import matplotlib.pyplot as plt %matplotlib inline X = np.linspace(-np.pi, np.pi, 256) cosx = np.cos(X) sinx = np.sin(X) plt.xlim(-4.0, 4.0) plt.ylim(-1.0, 1.0) plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi]) plt.yticks([-1, 0, 1]) plt.plot(X, cosx, 'b-', linewidth=1.0) plt.plot(X, sinx, 'r-', linewidth=1.0)
  • 15. BASIC EXAMPLES(IN JUPYTER) X = np.linspace(-np.pi, np.pi, 256) cosx = np.cos(X) sinx = np.sin(X) plt.xlim(-4.0, 4.0) plt.ylim(-1.0, 1.0) plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi]) plt.yticks([-1, 0, 1]) plt.plot(X, cosx, 'b-', linewidth=1.0) plt.plot(X, sinx, 'r-', linewidth=1.0) 15
  • 16. (CHANGE AXIS DISPLAY) BASIC EXAMPLES(IN JUPYTER) plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-pi$', r'$-pi/2$', r'$0$', r'$+pi/2$', r'$+pi$']) plt.yticks([-1, 0, +1], [r'$-1$', r'$0$', r'$+1$']) 16
  • 17. (ADD LEGEND) BASIC EXAMPLES(IN JUPYTER) plt.plot(X, cosx, 'b-', linewidth=1.0, label="cosine") plt.plot(X, sinx, 'r-', linewidth=1.0, label="sine") plt.legend(loc=‘upper left’) 17
  • 18. (CHANGE SPINE) BASIC EXAMPLES(IN JUPYTER) ax = plt.gca() # spine ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.spines['bottom'].set_position(('data',0)) ax.yaxis.set_ticks_position('left') ax.spines[‘left’].set_position(('data',np.pi/2)) 18
  • 19. (SUBPLOT) BASIC EXAMPLES(IN JUPYTER) plt.axes([.1, .1, 1, 1]) plt.axes([.2, .2, .3, .3], axisbg='green') plt.subplot(2, 1, 1, axisbg=‘y’) plt.subplot(2, 1, 2) 19 plt.subplot(2, 1, 1) plt.subplot(2, 1, 2)
  • 21. import numpy as np import matplotlib.pyplot as plt %matplotlib inline for idx, color in enumerate("rgbyck"): plt.subplot(3, 2, 1+idx, axisbg=color)
  • 23. PLOT STYLE CHEATSHEET Ex: Draw blue star line: plot(x, y, ‘b*’) 23
  • 24. (SCATTER) SOME EXAMPLES n = 1024 X = np.random.normal(0, 1, n) Y = np.random.normal(0, 1, n) T = np.arctan2(Y,X) plt.axes([0.025,0.025,0.95,0.95]) plt.scatter(X,Y, s=75, c=T, alpha=.5) plt.xlim(-1.5,1.5) plt.xticks([]) plt.ylim(-1.5,1.5) plt.yticks([]) 24 # matplotlib.colors.Colormap
  • 25. (HISTOGRAM) SOME EXAMPLES 25 n = 7 X = np.arange(n) Y1 = [0.5, 0.6, 0.65, 0.7, 0.8, 0.55, 0.9] plt.bar(X, Y1, facecolor='#9999ff') for x,y in zip(X,Y1): if y == sorted(Y1)[1]: plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom') plt.ylim(0, 1.25)
  • 26. (HISTOGRAM) SOME EXAMPLES n = 12 X = np.arange(n) Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n) Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n) plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white') plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white') for x,y in zip(X,Y1): plt.text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom') plt.ylim(-1.25,+1.25) 26
  • 27. (CONTOUR) SOME EXAMPLES def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2 - y**2) n = 256 x = np.linspace(-3,3,n) y = np.linspace(-3,3,n) X,Y = np.meshgrid(x,y) plt.contourf(X, Y, f(X,Y), 8, alpha=.75,cmap=‘jet') C = plt.contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5) 27
  • 28. (COLORMAP) SOME EXAMPLES def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2- y**2) n = 10 x = np.linspace(-3,3,3.5*n) y = np.linspace(-3,3,3.0*n) X,Y = np.meshgrid(x,y) Z = f(X,Y) plt.axes([0.025,0.025,0.95,0.95]) plt.imshow(Z,interpolation='nearest', cmap='bone', origin='lower') plt.colorbar(shrink=.92) plt.xticks([]), plt.yticks([]) 28
  • 30. LOCATION AWARE SENSING SYSTEM LASS • • https://www.facebook.com/groups/1607718702812067/ • Hackpad • https://lass.hackpad.com/LASS-README-DtZ5T6DXLbu • • http://lass-net.org
  • 31. LOCATION AWARE SENSING SYSTEM LASS • • LASS Bottom up
  • 32. PRACTICE 3 Data source: http://nrl.iis.sinica.edu.tw/LASS/last-all-airbox.json 32
  • 33. 33
  • 37. EXAMPLES(SHELL) import numpy as np import matplotlib.pyplot as plt X = np.linspace(0, 1, 256) plt.plot(X) plt.show() # 37
  • 38. 38
  • 39. 39
  • 40. BASIC EXAMPLES(SHELL) import numpy as np import matplotlib.pyplot as plt X = np.linspace(0, 1, 256) fig1 = plt.figure("fig_1") ax1 = fig1.add_subplot(1, 1, 1) ax1.plot(X) fig1.savefig(“fig_1.jpg") plt.show() 40
  • 41. SHELL VS. JUPYTER COMPARE import numpy as np import matplotlib.pyplot as plt %matplotlib inline X = np.linspace(0, 1, 256) plt.plot(X) import numpy as np import matplotlib.pyplot as plt X = np.linspace(0, 1, 256) fig1 = plt.figure("fig_1") ax1 = fig1.add_subplot(1, 1, 1) ax1.plot(X) fig1.savefig(“fig_1.jpg") plt.show()
  • 42. SUMMARY • Matplotlib class Figure -> Axes -> (Line2D, Text, etc) Figure Axes( ) matplotlib Axes • -> -> Matplotlib gallery • jupyter matplotlib inline environment • (ex. ) 42
  • 44. REFERENCE • http://liam0205.me/2014/09/11/matplotlib-tutorial-zh-cn/ • http://www.cnblogs.com/wei-li/archive/2012/05/23/2506940.html • http://darksair.org/wiki/matplotlib.html • http://2484055.blog.51cto.com/2474055/1334257 • http://matplotlib.org/users/pyplot_tutorial.html • http://matplotlib.org/users/style_sheets.html • http://rickchungtw-blog.logdown.com/tags/Matplotlib • http://yukuan.blogspot.tw/2006/12/analyze-sunspots.html • http://stackoverflow.com/questions/12987624/confusion-between-numpy-scipy-matplotlib-and-pylab • http://hhtucode.blogspot.tw/2013/04/ml-gradient-descent-algorithm.html • http://matplotlib.org/examples/subplots_axes_and_figures/subplot_demo.html • http://matplotlib.org/users/shell.html 44