SlideShare uma empresa Scribd logo
1 de 24
Baixar para ler offline
Barcelona Python Meetup



Plotting data with python and
            pylab
        Giovanni M. Dall'Olio
Problem statement
   Let's say we have a table of data like this:
     name        country     apples      pears
     Giovanni    Italy       31          13
     Mario       Italy       23          33
     Luigi       Italy       0           5
     Margaret    England     22          13
     Albert      Germany     15          6

   How to read it in python?
   How to do some basic plotting?
Alternatives for plotting
          data in python
   Pylab (enthought)→ Matlab/Octave approach
   Enthought → extended version of Pylab (free for 
     academic use)
   rpy/rpy2 → allows to run R commands within 
      python
   Sage → interfaces python with Matlab, R, octave, 
      mathematica, ...
The Pylab system
   pylab is a system of three libraries, which together 
     transform python in a Matlab­like environment
   It is composed by:
          Numpy (arrays, matrices, complex numbers, etc.. in 
            python)
          Scipy (extended scientific/statistics functions)
          Matplotlib (plotting library)
          iPython (extended interactive interpreter)
How to install pylab
   There are many alternatives to install PyLab:
          use the package manager of your linux distro 
          use enthought's distribution (
             http://www.enthought.com/products/epd.php) (free 
             for academic use)
          compile and google for help!
   Numpy and scipy contains some Fortran libraries, 
     therefore easy_install doesn't work well with 
     them
ipython -pylab
   Ipython is an extended version of the standard 
      python interpreter
   It has a modality especially designed for pylab
   The standard python interpreter doesn't support 
     very well plotting (not multi­threading)
   So if you want an interactive interpreter, use 
     ipython with the pylab option:

           $: alias pylab=”ipython -pylab”
           $: pylab

        In [1]:
Why the python interpreter
is not the best for plotting




     Gets blocked when you create a plot
How to read a CSV file with
         python
   To read a file like this in pylab:
      name        country     apples     pears
      Giovanni    Italy       31         13
      Mario       Italy       23         33
      Luigi       Italy       0          5
      Margaret    England     22         13
      Albert      Germany     15         6

   → Use the function 'matplotlib.mlab.csv2rec'
         >>> data = csv2rec('exampledata.txt',
           delimiter='t')
Numpy - record arrays
   csv2rec stores data in a numpy recarray object, where 
      you can access columns and rows easily:
     >>> print data['name']
     ['Giovanni' 'Mario' 'Luigi' 'Margaret'
      'Albert']

     >>> data['apples']
     array([31, 23, 0, 22, 15])

     >>> data[1]
     ('Mario', 'Italy', 23, 33)
Alternative to csv2rec
   numpy.genfromtxt (new in 2009)
   More options than csv2rec, included in numpy
   Tricky default parameters: need to specify dtype=None

      >>> data = numpy.genfromtxt('datafile.txt',
     dtype=None)
      >>> data
      array....
Barchart
>>> data = csv2rec('exampledata.txt', delimiter='t')

>>> bar(arange(len(data)), data['apples'], color='red',
width=0.1, label='apples')

>>> bar(arange(len(data))+0.1, data['pears'],
color='blue', width=0.1, label='pears')

>>> xticks(range(len(data)), data['name'], )

>>> legend()

>>> grid('.')
Barchart
  >>> data = csv2rec('exampledata.txt',
delimiter='t')

>>> figure()
>>> clf()


 Read a CSV file and storing 
  it in a recordarray object


 Use figure() and cls() to 
  reset the graphic device
Barchart
>>> data = csv2rec('exampledata.txt',
delimiter='t')

>>> bar(x=arange(len(data)), y=data['apples'],
color='red', width=0.1, label='apples')

   The bar function creates a 
     barchart
Barchart
>>> data = csv2rec('exampledata.txt',
delimiter='t')

>>> bar(x=arange(len(data)), y=data['apples'],
color='red', width=0.1, label='apples')

>>> bar(arange(len(data))+0.1, data['pears'],
color='blue', width=0.1, label='pears')


   This is the second barchart
Barchart
>>> data = csv2rec('exampledata.txt',
delimiter='t')

>>> bar(x=arange(len(data)), y=data['apples'],
color='red', width=0.1, label='apples')

>>> bar(arange(len(data))+0.1, data['pears'],
color='blue', width=0.1, label='pears')


>>> xticks(range(len(data)), data['name'], )


   Re­defining the labels in the X axis 
     (xticks)
Barchart
>>> data = csv2rec('exampledata.txt',
delimiter='t')

>>> bar(x=arange(len(data)), y=data['apples'],
color='red', width=0.1, label='apples')

>>> bar(arange(len(data))+0.1, data['pears'],
color='blue', width=0.1, label='pears')

>>> xticks(range(len(data)), data['name'], )

>>> legend()
>>> grid('.')
>>> title('apples and pears by person')

   Adding legend, grid, title
Barchart (result)
Pie Chart
>>> pie(data['pears'], labels=data['name'])
>>> pie(data['pears'], labels=['%sn(%s
  pears)' % (i,j) for (i, j) in
  zip(data['name'], data['pears'])] )
Pie chart (result)
A plot chart
>>> x = linspace(1,10, 10)
>>> y = randn(10)
>>> plot(x,y, 'r.', ms=15)
 
An histogram
>>> x = randn(1000)
>>> hist(x, bins=40)
>>> title('histogram of random numbers')
 
Matplotlib gallery
Scipy Cookbook
Thanks for the attention!!
   PyLab ­ http://www.scipy.org/PyLab 
   matplotlib ­ http://matplotlib.sourceforge.net/ 
   scipy ­ http://www.scipy.org/ 
   numpy ­ http://numpy.scipy.org/ 
   ipython ­ http://ipython.scipy.org/moin/ 


   These slides: http://bioinfoblog.it 

Mais conteúdo relacionado

Mais procurados

Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesAndrew Ferlitsch
 
Introduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data AnalyticsIntroduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data AnalyticsPhoenix
 
Visualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptxVisualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptxSharmilaMore5
 
Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlibPiyush rai
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandasPiyush rai
 
Data Visualization(s) Using Python
Data Visualization(s) Using PythonData Visualization(s) Using Python
Data Visualization(s) Using PythonAniket Maithani
 
Data Analysis and Visualization using Python
Data Analysis and Visualization using PythonData Analysis and Visualization using Python
Data Analysis and Visualization using PythonChariza Pladin
 
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | EdurekaWhat is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | EdurekaEdureka!
 
Data Visualization using matplotlib
Data Visualization using matplotlibData Visualization using matplotlib
Data Visualization using matplotlibBruno Gonçalves
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
 
DATA VISUALIZATION USING MATPLOTLIB (PYTHON)
DATA VISUALIZATION USING MATPLOTLIB (PYTHON)DATA VISUALIZATION USING MATPLOTLIB (PYTHON)
DATA VISUALIZATION USING MATPLOTLIB (PYTHON)Mohammed Anzil
 

Mais procurados (20)

Numpy tutorial
Numpy tutorialNumpy tutorial
Numpy tutorial
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Introduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data AnalyticsIntroduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data Analytics
 
Visualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptxVisualization and Matplotlib using Python.pptx
Visualization and Matplotlib using Python.pptx
 
Data visualization
Data visualizationData visualization
Data visualization
 
Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlib
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
MatplotLib.pptx
MatplotLib.pptxMatplotLib.pptx
MatplotLib.pptx
 
Python pandas Library
Python pandas LibraryPython pandas Library
Python pandas Library
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
 
Python
PythonPython
Python
 
Data Visualization(s) Using Python
Data Visualization(s) Using PythonData Visualization(s) Using Python
Data Visualization(s) Using Python
 
Data Analysis and Visualization using Python
Data Analysis and Visualization using PythonData Analysis and Visualization using Python
Data Analysis and Visualization using Python
 
Python for data analysis
Python for data analysisPython for data analysis
Python for data analysis
 
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | EdurekaWhat is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
 
Data Visualization using matplotlib
Data Visualization using matplotlibData Visualization using matplotlib
Data Visualization using matplotlib
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
DATA VISUALIZATION USING MATPLOTLIB (PYTHON)
DATA VISUALIZATION USING MATPLOTLIB (PYTHON)DATA VISUALIZATION USING MATPLOTLIB (PYTHON)
DATA VISUALIZATION USING MATPLOTLIB (PYTHON)
 
Matplotlib
MatplotlibMatplotlib
Matplotlib
 

Semelhante a Plotting data with python and pylab

A Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonA Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonTariq Rashid
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...DRVaibhavmeshram1
 
Python For Scientists
Python For ScientistsPython For Scientists
Python For Scientistsaeberspaecher
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxjovannyflex
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxjovannyflex
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdfCBJWorld
 
Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib. Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib. yazad dumasia
 
Basic of python for data analysis
Basic of python for data analysisBasic of python for data analysis
Basic of python for data analysisPramod Toraskar
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using PythonNishantKumar1179
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Simplilearn
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnArnaud Joly
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python ProgrammingDozie Agbo
 
Python bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of NairobiPython bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of Nairobikrmboya
 

Semelhante a Plotting data with python and pylab (20)

A Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonA Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with Python
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Python For Scientists
Python For ScientistsPython For Scientists
Python For Scientists
 
iPython
iPythoniPython
iPython
 
Scientific Python
Scientific PythonScientific Python
Scientific Python
 
Biopython: Overview, State of the Art and Outlook
Biopython: Overview, State of the Art and OutlookBiopython: Overview, State of the Art and Outlook
Biopython: Overview, State of the Art and Outlook
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdf
 
Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib. Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib.
 
Basic of python for data analysis
Basic of python for data analysisBasic of python for data analysis
Basic of python for data analysis
 
Project gnuplot
Project gnuplotProject gnuplot
Project gnuplot
 
Lecture 9.pptx
Lecture 9.pptxLecture 9.pptx
Lecture 9.pptx
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
 
Course set three full notes
Course set three full notesCourse set three full notes
Course set three full notes
 
DS LAB MANUAL.pdf
DS LAB MANUAL.pdfDS LAB MANUAL.pdf
DS LAB MANUAL.pdf
 
Python bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of NairobiPython bootcamp - C4Dlab, University of Nairobi
Python bootcamp - C4Dlab, University of Nairobi
 

Mais de Giovanni Marco Dall'Olio

Thesis defence of Dall'Olio Giovanni Marco. Applications of network theory to...
Thesis defence of Dall'Olio Giovanni Marco. Applications of network theory to...Thesis defence of Dall'Olio Giovanni Marco. Applications of network theory to...
Thesis defence of Dall'Olio Giovanni Marco. Applications of network theory to...Giovanni Marco Dall'Olio
 
The true story behind the annotation of a pathway
The true story behind the annotation of a pathwayThe true story behind the annotation of a pathway
The true story behind the annotation of a pathwayGiovanni Marco Dall'Olio
 

Mais de Giovanni Marco Dall'Olio (20)

Fehrman Nat Gen 2014 - Journal Club
Fehrman Nat Gen 2014 - Journal ClubFehrman Nat Gen 2014 - Journal Club
Fehrman Nat Gen 2014 - Journal Club
 
Thesis defence of Dall'Olio Giovanni Marco. Applications of network theory to...
Thesis defence of Dall'Olio Giovanni Marco. Applications of network theory to...Thesis defence of Dall'Olio Giovanni Marco. Applications of network theory to...
Thesis defence of Dall'Olio Giovanni Marco. Applications of network theory to...
 
Agile bioinf
Agile bioinfAgile bioinf
Agile bioinf
 
Version control
Version controlVersion control
Version control
 
Linux intro 5 extra: awk
Linux intro 5 extra: awkLinux intro 5 extra: awk
Linux intro 5 extra: awk
 
Linux intro 5 extra: makefiles
Linux intro 5 extra: makefilesLinux intro 5 extra: makefiles
Linux intro 5 extra: makefiles
 
Linux intro 4 awk + makefile
Linux intro 4  awk + makefileLinux intro 4  awk + makefile
Linux intro 4 awk + makefile
 
Linux intro 3 grep + Unix piping
Linux intro 3 grep + Unix pipingLinux intro 3 grep + Unix piping
Linux intro 3 grep + Unix piping
 
Linux intro 2 basic terminal
Linux intro 2   basic terminalLinux intro 2   basic terminal
Linux intro 2 basic terminal
 
Linux intro 1 definitions
Linux intro 1  definitionsLinux intro 1  definitions
Linux intro 1 definitions
 
Wagner chapter 5
Wagner chapter 5Wagner chapter 5
Wagner chapter 5
 
Wagner chapter 4
Wagner chapter 4Wagner chapter 4
Wagner chapter 4
 
Wagner chapter 3
Wagner chapter 3Wagner chapter 3
Wagner chapter 3
 
Wagner chapter 2
Wagner chapter 2Wagner chapter 2
Wagner chapter 2
 
Wagner chapter 1
Wagner chapter 1Wagner chapter 1
Wagner chapter 1
 
Hg for bioinformatics, second part
Hg for bioinformatics, second partHg for bioinformatics, second part
Hg for bioinformatics, second part
 
Hg version control bioinformaticians
Hg version control bioinformaticiansHg version control bioinformaticians
Hg version control bioinformaticians
 
The true story behind the annotation of a pathway
The true story behind the annotation of a pathwayThe true story behind the annotation of a pathway
The true story behind the annotation of a pathway
 
Pycon
PyconPycon
Pycon
 
Makefiles Bioinfo
Makefiles BioinfoMakefiles Bioinfo
Makefiles Bioinfo
 

Último

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Último (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Plotting data with python and pylab

  • 1. Barcelona Python Meetup Plotting data with python and pylab Giovanni M. Dall'Olio
  • 2. Problem statement  Let's say we have a table of data like this: name country apples pears Giovanni Italy 31 13 Mario Italy 23 33 Luigi Italy 0 5 Margaret England 22 13 Albert Germany 15 6  How to read it in python?  How to do some basic plotting?
  • 3. Alternatives for plotting data in python  Pylab (enthought)→ Matlab/Octave approach  Enthought → extended version of Pylab (free for  academic use)  rpy/rpy2 → allows to run R commands within  python  Sage → interfaces python with Matlab, R, octave,  mathematica, ...
  • 4. The Pylab system  pylab is a system of three libraries, which together  transform python in a Matlab­like environment  It is composed by:  Numpy (arrays, matrices, complex numbers, etc.. in  python)  Scipy (extended scientific/statistics functions)  Matplotlib (plotting library)  iPython (extended interactive interpreter)
  • 5. How to install pylab  There are many alternatives to install PyLab:  use the package manager of your linux distro   use enthought's distribution ( http://www.enthought.com/products/epd.php) (free  for academic use)  compile and google for help!  Numpy and scipy contains some Fortran libraries,  therefore easy_install doesn't work well with  them
  • 6. ipython -pylab  Ipython is an extended version of the standard  python interpreter  It has a modality especially designed for pylab  The standard python interpreter doesn't support  very well plotting (not multi­threading)  So if you want an interactive interpreter, use  ipython with the pylab option:      $: alias pylab=”ipython -pylab” $: pylab In [1]:
  • 7. Why the python interpreter is not the best for plotting Gets blocked when you create a plot
  • 8. How to read a CSV file with python  To read a file like this in pylab: name country apples pears Giovanni Italy 31 13 Mario Italy 23 33 Luigi Italy 0 5 Margaret England 22 13 Albert Germany 15 6  → Use the function 'matplotlib.mlab.csv2rec' >>> data = csv2rec('exampledata.txt', delimiter='t')
  • 9. Numpy - record arrays  csv2rec stores data in a numpy recarray object, where  you can access columns and rows easily: >>> print data['name'] ['Giovanni' 'Mario' 'Luigi' 'Margaret' 'Albert'] >>> data['apples'] array([31, 23, 0, 22, 15]) >>> data[1] ('Mario', 'Italy', 23, 33)
  • 10. Alternative to csv2rec  numpy.genfromtxt (new in 2009)  More options than csv2rec, included in numpy  Tricky default parameters: need to specify dtype=None >>> data = numpy.genfromtxt('datafile.txt', dtype=None) >>> data array....
  • 11. Barchart >>> data = csv2rec('exampledata.txt', delimiter='t') >>> bar(arange(len(data)), data['apples'], color='red', width=0.1, label='apples') >>> bar(arange(len(data))+0.1, data['pears'], color='blue', width=0.1, label='pears') >>> xticks(range(len(data)), data['name'], ) >>> legend() >>> grid('.')
  • 12. Barchart >>> data = csv2rec('exampledata.txt', delimiter='t') >>> figure() >>> clf() Read a CSV file and storing  it in a recordarray object Use figure() and cls() to  reset the graphic device
  • 13. Barchart >>> data = csv2rec('exampledata.txt', delimiter='t') >>> bar(x=arange(len(data)), y=data['apples'], color='red', width=0.1, label='apples')  The bar function creates a  barchart
  • 14. Barchart >>> data = csv2rec('exampledata.txt', delimiter='t') >>> bar(x=arange(len(data)), y=data['apples'], color='red', width=0.1, label='apples') >>> bar(arange(len(data))+0.1, data['pears'], color='blue', width=0.1, label='pears')  This is the second barchart
  • 15. Barchart >>> data = csv2rec('exampledata.txt', delimiter='t') >>> bar(x=arange(len(data)), y=data['apples'], color='red', width=0.1, label='apples') >>> bar(arange(len(data))+0.1, data['pears'], color='blue', width=0.1, label='pears') >>> xticks(range(len(data)), data['name'], )  Re­defining the labels in the X axis  (xticks)
  • 16. Barchart >>> data = csv2rec('exampledata.txt', delimiter='t') >>> bar(x=arange(len(data)), y=data['apples'], color='red', width=0.1, label='apples') >>> bar(arange(len(data))+0.1, data['pears'], color='blue', width=0.1, label='pears') >>> xticks(range(len(data)), data['name'], ) >>> legend() >>> grid('.') >>> title('apples and pears by person')  Adding legend, grid, title
  • 18. Pie Chart >>> pie(data['pears'], labels=data['name']) >>> pie(data['pears'], labels=['%sn(%s pears)' % (i,j) for (i, j) in zip(data['name'], data['pears'])] )
  • 20. A plot chart >>> x = linspace(1,10, 10) >>> y = randn(10) >>> plot(x,y, 'r.', ms=15)  
  • 21. An histogram >>> x = randn(1000) >>> hist(x, bins=40) >>> title('histogram of random numbers')  
  • 24. Thanks for the attention!!  PyLab ­ http://www.scipy.org/PyLab   matplotlib ­ http://matplotlib.sourceforge.net/   scipy ­ http://www.scipy.org/   numpy ­ http://numpy.scipy.org/   ipython ­ http://ipython.scipy.org/moin/   These slides: http://bioinfoblog.it