Display 1D data with Python

From Wicare

Jump to: navigation, search


Contents

Description

This page details an example of plotting a graph with Python and the Matplotlib library. In this particular case, the data are read in a HDF4 file, but it can also be read from any source as long as the data are passed to Matplotlib in a numpy array.

Prerequisites

Language

  • Python

Libraries

  • pyhdf
  • matplotlib
  • numpy

Input File

The data file used in the example below is : sinus_cardinal_1D.hdf

Output Image

The program creates the image

Representation (1D) of the sinc function
Representation (1D) of the sinc function

Source Code

from pylab    import *
from numpy    import *
from pyhdf.SD import *
 
# Path to the file
fileName = "sinus_cardinal_1D.hdf"
 
# Open HDF file for reading
filehdf = SD ( fileName )
 
# Reading attributes
print "file : ", fileName
 
# Read the SDS data
sds = filehdf.select ( 0 ).get ( )
 
# Display image
plot ( sds )
 
# Image title
title ( "Representation of the sinc function", weight = "bold", size = 22 )
 
# Axes title
ylabel ( "Amplitude", weight = "bold", size = 22 )
xlabel ( "X",         weight = "bold", size = 22 )
 
# Save the current figure to output image
savefig( "./sinus_cardinal_1D.png" )
 
# eventually display it
show()
 
# Close access to the HDF file
filehdf.end()

Personal tools
<
October 2017
>
MTWTFSS
1
2345678
9101112131415
16171819202122
23242526272829
3031