Display 1D data with Python


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


  • Python
  • 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

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
# Close access to the HDF file


More news


How to convert a matplotlib figure to a numpy array or a PIL image

Language/Format: Python
Description: For manipulating a figure build with matplotlib, it is sometimes requested to convert it in a format understandable by other python libraries. This can be useful for using scipy image filters or manually adding annotations for example.
This page details how to convert a matplotlib figure to a numpy 3D array of RGBA values, or directly to a PIL ( Python Imaging Library ) Image.
Author(s): Nicolas Pascal (ICARE)



Reading a NetCDF file with Matlab and R

Language/Format: MATLAB
Description: This page gives pieces of code to read data in a NetCDF file
Author(s): Aminata NDIAYE (ICARE)



Writing an HDF file with C, FORTRAN, Python, MATLAB and R

Language/Format: Fortran,MATLAB,Python
Description: This page gives pieces of code to write data in an HDF4 file
Author(s): Nicolas PASCAL, Nicolas THOMAS, Aminata NDIAYE (CARE )