SlideShare a Scribd company logo
# matplotlib demo from San Diego Python Data Analysis Workshop 20APR2013
# Drew Arnett
# a.arnett@ieee.org
# code from this file was copied and pasted in chunks to run
# import libraries that will be used
import matplotlib.mlab
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
# read in the data set]
x = matplotlib.mlab.csv2rec("s_p_historical_closes.csv")
# plot closing data
plt.plot(x.date, x.close, ".")
plt.show()
# plot opening and closing data on one plot
plt.plot(x.date, x.open, ".", label="open")
plt.plot(x.date, x.close, ".", label="close")
plt.legend()
plt.show()
# that wasn't very interesting, so...
# plot daily range
plt.plot(x.date, x.high-x.low, ".")
plt.show()
# that isn't very fair, so...
# plot range scaled against close and in %
dailyrange = 100.*(x.high-x.low)/x.close
plt.plot(x.date, dailyrange, ".")
plt.show()
# use subplots to show more than one set of data at a time
# can also say subplot(6,1,1)
# subplot(number of subplot rows, number of subplot columns, specific subplot to
use)
plt.subplot(611)
plt.plot(x.date, x.open, ".", label="open")
plt.legend()
plt.subplot(612)
plt.plot(x.date, x.high, ".", label="high")
plt.legend()
plt.subplot(613)
plt.plot(x.date, x.low, ".", label="low")
plt.legend()
plt.subplot(614)
plt.plot(x.date, x.close, ".", label="close")
plt.legend()
plt.subplot(615)
plt.plot(x.date, x.volume, ".", label="volume")
plt.legend()
plt.subplot(616)
plt.plot(x.date, 100.*(x.high-x.low)/x.close, ".", label="range")
plt.legend()
plt.show()
# the same thing, but more concise and maintainable code, perhaps a bit more
pythonic
for sub, item in enumerate("open,high,low,close,volume".split(",")):
plt.subplot(5,1,sub+1)
plt.plot(x.date, x[item], ".", label = item)
plt.legend(loc="best")
plt.show()
# all of that was not interactive, plot shown only on show()
# would like to see what happens with each plotting command
# so turn on interactive mode. this might be more useful for either
# interactive data analysis or refinement of a plot's formatting
plt.isinteractive()
plt.ion()
plt.subplot(211)
plt.plot(x.date, x.close, ".", label="close")
plt.subplot(212)
plt.plot(x.date, 100.*(x.high-x.low)/x.close, ".", label="range")
plt.close()
plt.ioff()
# plot daily range to a file instead of interactive
plt.plot(x.date, 100.*(x.high-x.low)/x.close, ".")
plt.title("S&P Daily range (% of close")
plt.xlabel("date")
plt.ylabel("%")
plt.savefig("snp range.png")
plt.show()
# plot numerous plots to a multipage PDF file
# obvious pros and cons to raster versus vector image file formats
pp = PdfPages("example.pdf")
for item in "open,high,low,close,volume".split(","):
plt.plot(x.date, x[item], ".", label = item)
plt.title(item)
plt.legend(loc="best")
pp.savefig()
plt.close()
pp.close()
# usually I'll use an image manipulation program to add annotation
# but matplotlib supports a lot of annotation and this could be very useful
# here the daily range is plotted with an annotation on the max point
dailyrange = 100.*(x.high-x.low)/x.close
peak = (x.date[dailyrange.argmax()], dailyrange[dailyrange.argmax()])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x.date, dailyrange, ".")
ax.annotate("WOW!", xy=peak, xytext = (peak[0], peak[1] + 3), arrowprops =
dict(facecolor = "black"))
plt.show()
# now two examples not using the S&P data set
# plotting two sets of data and with two scales for the vertical axis
data1 = [1,2,3,4,5,6,5,4,3,2,1]
data2 = [1,2,1,2,3,1,2,1,3,1,0]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.plot(data1, color = "red")
ax1.set_ylabel("red")
ax2.plot(data2, color = "blue")
ax2.set_ylabel("blue")
plt.show()
# often I don't want autoscaling
# it may be good to assert to find situations where data exceeds a fixed scale
# and of course, now, the two scales are now the same and are redundant
# plotting the same two sets of data with fixed identical scales
data1 = [1,2,3,4,5,6,5,4,3,2,1]
data2 = [1,2,1,2,3,1,2,1,3,1,0]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.plot(data1, color = "red")
ax1.set_ylabel("red")
ax1.set_ylim(0, 10)
ax2.plot(data2, color = "blue")
ax2.set_ylabel("blue")
ax2.set_ylim(0, 10)
plt.show()

More Related Content

What's hot

TF.data & Eager Execution
TF.data & Eager ExecutionTF.data & Eager Execution
TF.data & Eager Execution
Modulabs
 
tf.data: TensorFlow Input Pipeline
tf.data: TensorFlow Input Pipelinetf.data: TensorFlow Input Pipeline
tf.data: TensorFlow Input Pipeline
Alluxio, Inc.
 
Project gnuplot
Project gnuplotProject gnuplot
Project gnuplot
Sabyasachi Ray
 
Python gis
Python gisPython gis
Python gis
John Zhou
 
D3 data, user, interaction
D3  data, user, interactionD3  data, user, interaction
D3 data, user, interactionDummas
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
Sayantan Sur
 
16858 memory management2
16858 memory management216858 memory management2
16858 memory management2Aanand Singh
 
Chunked, dplyr for large text files
Chunked, dplyr for large text filesChunked, dplyr for large text files
Chunked, dplyr for large text files
Edwin de Jonge
 
Functional Programming, simplified
Functional Programming, simplifiedFunctional Programming, simplified
Functional Programming, simplified
Naveenkumar Muguda
 
3. basic data structures(2)
3. basic data structures(2)3. basic data structures(2)
3. basic data structures(2)
Hongjun Jang
 
Build 2017 - B8037 - Explore the next generation of innovative UI in the Visu...
Build 2017 - B8037 - Explore the next generation of innovative UI in the Visu...Build 2017 - B8037 - Explore the next generation of innovative UI in the Visu...
Build 2017 - B8037 - Explore the next generation of innovative UI in the Visu...
Windows Developer
 
We Must Go Deeper
We Must Go DeeperWe Must Go Deeper
We Must Go Deeper
The Software House
 
The Algorithms of CSS @ CSSConf EU 2018
The Algorithms of CSS @ CSSConf EU 2018The Algorithms of CSS @ CSSConf EU 2018
The Algorithms of CSS @ CSSConf EU 2018
Lara Schenck
 
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...
Lara Schenck
 
The Daily Method: JS Arrays::flatMap()
The Daily Method: JS Arrays::flatMap()The Daily Method: JS Arrays::flatMap()
The Daily Method: JS Arrays::flatMap()
Lemuel Uhuru
 

What's hot (16)

TF.data & Eager Execution
TF.data & Eager ExecutionTF.data & Eager Execution
TF.data & Eager Execution
 
tf.data: TensorFlow Input Pipeline
tf.data: TensorFlow Input Pipelinetf.data: TensorFlow Input Pipeline
tf.data: TensorFlow Input Pipeline
 
Project gnuplot
Project gnuplotProject gnuplot
Project gnuplot
 
Python gis
Python gisPython gis
Python gis
 
D3 data, user, interaction
D3  data, user, interactionD3  data, user, interaction
D3 data, user, interaction
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
16858 memory management2
16858 memory management216858 memory management2
16858 memory management2
 
R-Excel Integration
R-Excel IntegrationR-Excel Integration
R-Excel Integration
 
Chunked, dplyr for large text files
Chunked, dplyr for large text filesChunked, dplyr for large text files
Chunked, dplyr for large text files
 
Functional Programming, simplified
Functional Programming, simplifiedFunctional Programming, simplified
Functional Programming, simplified
 
3. basic data structures(2)
3. basic data structures(2)3. basic data structures(2)
3. basic data structures(2)
 
Build 2017 - B8037 - Explore the next generation of innovative UI in the Visu...
Build 2017 - B8037 - Explore the next generation of innovative UI in the Visu...Build 2017 - B8037 - Explore the next generation of innovative UI in the Visu...
Build 2017 - B8037 - Explore the next generation of innovative UI in the Visu...
 
We Must Go Deeper
We Must Go DeeperWe Must Go Deeper
We Must Go Deeper
 
The Algorithms of CSS @ CSSConf EU 2018
The Algorithms of CSS @ CSSConf EU 2018The Algorithms of CSS @ CSSConf EU 2018
The Algorithms of CSS @ CSSConf EU 2018
 
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...
 
The Daily Method: JS Arrays::flatMap()
The Daily Method: JS Arrays::flatMap()The Daily Method: JS Arrays::flatMap()
The Daily Method: JS Arrays::flatMap()
 

Similar to Matplotlib demo code

R (Shiny Package) - Server Side Code for Decision Support System
R (Shiny Package) - Server Side Code for Decision Support SystemR (Shiny Package) - Server Side Code for Decision Support System
R (Shiny Package) - Server Side Code for Decision Support SystemMaithreya Chakravarthula
 
Three Functional Programming Technologies for Big Data
Three Functional Programming Technologies for Big DataThree Functional Programming Technologies for Big Data
Three Functional Programming Technologies for Big Data
Dynamical Software, Inc.
 
Data visualization in python/Django
Data visualization in python/DjangoData visualization in python/Django
Data visualization in python/Django
kenluck2001
 
Python utan-stodhjul-motorsag
Python utan-stodhjul-motorsagPython utan-stodhjul-motorsag
Python utan-stodhjul-motorsagniklal
 
ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions
Dr. Volkan OBAN
 
Dex Technical Seminar (April 2011)
Dex Technical Seminar (April 2011)Dex Technical Seminar (April 2011)
Dex Technical Seminar (April 2011)
Sergio Gomez Villamor
 
Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout
source{d}
 
Poetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePoetry with R -- Dissecting the code
Poetry with R -- Dissecting the code
Peter Solymos
 
A Shiny Example-- R
A Shiny Example-- RA Shiny Example-- R
A Shiny Example-- R
Dr. Volkan OBAN
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
Nimrita Koul
 
PPT ON MACHINE LEARNING by Ragini Ratre
PPT ON MACHINE LEARNING by Ragini RatrePPT ON MACHINE LEARNING by Ragini Ratre
PPT ON MACHINE LEARNING by Ragini Ratre
RaginiRatre
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
rik0
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
PyCon Italia
 
Day 3 plotting.pptx
Day 3   plotting.pptxDay 3   plotting.pptx
Day 3 plotting.pptx
Adrien Melquiond
 
5 R Tutorial Data Visualization
5 R Tutorial Data Visualization5 R Tutorial Data Visualization
5 R Tutorial Data Visualization
Sakthi Dasans
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
Vikas Sharma
 
Introduction to R for data science
Introduction to R for data scienceIntroduction to R for data science
Introduction to R for data science
Long Nguyen
 
End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017
End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017
End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017
StampedeCon
 

Similar to Matplotlib demo code (20)

R (Shiny Package) - Server Side Code for Decision Support System
R (Shiny Package) - Server Side Code for Decision Support SystemR (Shiny Package) - Server Side Code for Decision Support System
R (Shiny Package) - Server Side Code for Decision Support System
 
Three Functional Programming Technologies for Big Data
Three Functional Programming Technologies for Big DataThree Functional Programming Technologies for Big Data
Three Functional Programming Technologies for Big Data
 
Data visualization in python/Django
Data visualization in python/DjangoData visualization in python/Django
Data visualization in python/Django
 
Python utan-stodhjul-motorsag
Python utan-stodhjul-motorsagPython utan-stodhjul-motorsag
Python utan-stodhjul-motorsag
 
ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions
 
JQuery Flot
JQuery FlotJQuery Flot
JQuery Flot
 
Dex Technical Seminar (April 2011)
Dex Technical Seminar (April 2011)Dex Technical Seminar (April 2011)
Dex Technical Seminar (April 2011)
 
Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout
 
Poetry with R -- Dissecting the code
Poetry with R -- Dissecting the codePoetry with R -- Dissecting the code
Poetry with R -- Dissecting the code
 
A Shiny Example-- R
A Shiny Example-- RA Shiny Example-- R
A Shiny Example-- R
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
 
Matlab workshop
Matlab workshopMatlab workshop
Matlab workshop
 
PPT ON MACHINE LEARNING by Ragini Ratre
PPT ON MACHINE LEARNING by Ragini RatrePPT ON MACHINE LEARNING by Ragini Ratre
PPT ON MACHINE LEARNING by Ragini Ratre
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Day 3 plotting.pptx
Day 3   plotting.pptxDay 3   plotting.pptx
Day 3 plotting.pptx
 
5 R Tutorial Data Visualization
5 R Tutorial Data Visualization5 R Tutorial Data Visualization
5 R Tutorial Data Visualization
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Introduction to R for data science
Introduction to R for data scienceIntroduction to R for data science
Introduction to R for data science
 
End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017
End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017
End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017
 

More from pythonsd

Pep 465 - Matrix Multiplication in Python
Pep 465 - Matrix Multiplication in PythonPep 465 - Matrix Multiplication in Python
Pep 465 - Matrix Multiplication in Python
pythonsd
 
Sqlalchemy lightning talk
Sqlalchemy lightning talkSqlalchemy lightning talk
Sqlalchemy lightning talk
pythonsd
 
PythonSD Test Driven Django Development Workshop
PythonSD Test Driven Django Development WorkshopPythonSD Test Driven Django Development Workshop
PythonSD Test Driven Django Development Workshop
pythonsd
 
Matplotlib presentation 20 apr2013 final
Matplotlib presentation 20 apr2013   finalMatplotlib presentation 20 apr2013   final
Matplotlib presentation 20 apr2013 final
pythonsd
 
Blaze the-evolution-of-numpy
Blaze the-evolution-of-numpyBlaze the-evolution-of-numpy
Blaze the-evolution-of-numpypythonsd
 
Django production
Django productionDjango production
Django production
pythonsd
 
Django Toolbox
Django ToolboxDjango Toolbox
Django Toolbox
pythonsd
 
Why Python 3
Why Python 3Why Python 3
Why Python 3
pythonsd
 

More from pythonsd (8)

Pep 465 - Matrix Multiplication in Python
Pep 465 - Matrix Multiplication in PythonPep 465 - Matrix Multiplication in Python
Pep 465 - Matrix Multiplication in Python
 
Sqlalchemy lightning talk
Sqlalchemy lightning talkSqlalchemy lightning talk
Sqlalchemy lightning talk
 
PythonSD Test Driven Django Development Workshop
PythonSD Test Driven Django Development WorkshopPythonSD Test Driven Django Development Workshop
PythonSD Test Driven Django Development Workshop
 
Matplotlib presentation 20 apr2013 final
Matplotlib presentation 20 apr2013   finalMatplotlib presentation 20 apr2013   final
Matplotlib presentation 20 apr2013 final
 
Blaze the-evolution-of-numpy
Blaze the-evolution-of-numpyBlaze the-evolution-of-numpy
Blaze the-evolution-of-numpy
 
Django production
Django productionDjango production
Django production
 
Django Toolbox
Django ToolboxDjango Toolbox
Django Toolbox
 
Why Python 3
Why Python 3Why Python 3
Why Python 3
 

Recently uploaded

GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
Jen Stirrup
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
UiPathCommunity
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
Globus
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 

Recently uploaded (20)

GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 

Matplotlib demo code

  • 1. # matplotlib demo from San Diego Python Data Analysis Workshop 20APR2013 # Drew Arnett # a.arnett@ieee.org # code from this file was copied and pasted in chunks to run # import libraries that will be used import matplotlib.mlab import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages # read in the data set] x = matplotlib.mlab.csv2rec("s_p_historical_closes.csv") # plot closing data plt.plot(x.date, x.close, ".") plt.show() # plot opening and closing data on one plot plt.plot(x.date, x.open, ".", label="open") plt.plot(x.date, x.close, ".", label="close") plt.legend() plt.show() # that wasn't very interesting, so... # plot daily range plt.plot(x.date, x.high-x.low, ".") plt.show() # that isn't very fair, so... # plot range scaled against close and in % dailyrange = 100.*(x.high-x.low)/x.close plt.plot(x.date, dailyrange, ".") plt.show() # use subplots to show more than one set of data at a time # can also say subplot(6,1,1) # subplot(number of subplot rows, number of subplot columns, specific subplot to use) plt.subplot(611) plt.plot(x.date, x.open, ".", label="open") plt.legend() plt.subplot(612) plt.plot(x.date, x.high, ".", label="high") plt.legend() plt.subplot(613) plt.plot(x.date, x.low, ".", label="low") plt.legend() plt.subplot(614) plt.plot(x.date, x.close, ".", label="close")
  • 2. plt.legend() plt.subplot(615) plt.plot(x.date, x.volume, ".", label="volume") plt.legend() plt.subplot(616) plt.plot(x.date, 100.*(x.high-x.low)/x.close, ".", label="range") plt.legend() plt.show() # the same thing, but more concise and maintainable code, perhaps a bit more pythonic for sub, item in enumerate("open,high,low,close,volume".split(",")): plt.subplot(5,1,sub+1) plt.plot(x.date, x[item], ".", label = item) plt.legend(loc="best") plt.show() # all of that was not interactive, plot shown only on show() # would like to see what happens with each plotting command # so turn on interactive mode. this might be more useful for either # interactive data analysis or refinement of a plot's formatting plt.isinteractive() plt.ion() plt.subplot(211) plt.plot(x.date, x.close, ".", label="close") plt.subplot(212) plt.plot(x.date, 100.*(x.high-x.low)/x.close, ".", label="range") plt.close() plt.ioff() # plot daily range to a file instead of interactive plt.plot(x.date, 100.*(x.high-x.low)/x.close, ".") plt.title("S&P Daily range (% of close") plt.xlabel("date") plt.ylabel("%") plt.savefig("snp range.png") plt.show() # plot numerous plots to a multipage PDF file # obvious pros and cons to raster versus vector image file formats pp = PdfPages("example.pdf") for item in "open,high,low,close,volume".split(","): plt.plot(x.date, x[item], ".", label = item) plt.title(item) plt.legend(loc="best") pp.savefig()
  • 3. plt.close() pp.close() # usually I'll use an image manipulation program to add annotation # but matplotlib supports a lot of annotation and this could be very useful # here the daily range is plotted with an annotation on the max point dailyrange = 100.*(x.high-x.low)/x.close peak = (x.date[dailyrange.argmax()], dailyrange[dailyrange.argmax()]) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x.date, dailyrange, ".") ax.annotate("WOW!", xy=peak, xytext = (peak[0], peak[1] + 3), arrowprops = dict(facecolor = "black")) plt.show() # now two examples not using the S&P data set # plotting two sets of data and with two scales for the vertical axis data1 = [1,2,3,4,5,6,5,4,3,2,1] data2 = [1,2,1,2,3,1,2,1,3,1,0] fig = plt.figure() ax1 = fig.add_subplot(111) ax2 = ax1.twinx() ax1.plot(data1, color = "red") ax1.set_ylabel("red") ax2.plot(data2, color = "blue") ax2.set_ylabel("blue") plt.show() # often I don't want autoscaling # it may be good to assert to find situations where data exceeds a fixed scale # and of course, now, the two scales are now the same and are redundant # plotting the same two sets of data with fixed identical scales data1 = [1,2,3,4,5,6,5,4,3,2,1] data2 = [1,2,1,2,3,1,2,1,3,1,0] fig = plt.figure() ax1 = fig.add_subplot(111) ax2 = ax1.twinx() ax1.plot(data1, color = "red") ax1.set_ylabel("red") ax1.set_ylim(0, 10) ax2.plot(data2, color = "blue") ax2.set_ylabel("blue") ax2.set_ylim(0, 10) plt.show()