Scientific Plotting?

Travis Oliphant olipt at mayo.edu
Mon Jul 19 22:51:14 EDT 1999


> People at my current work use MatLab or IDL for chomping data through
> data calculations and plotting the results in an environment that lets
> you get GUI's up and going very quickly (among other things).  It's been
> a goal of mine that one day I could show them that this could be done
> with as little effort (with greater room for expansion) using Python.  
> Plus I'd like to put a strong case forward to our physics department for
> our up-and-coming new scientific programmers to learn something with a
> broader scope like Python than the now used IDL and recently abandoned
> Fortran-77.
> 

I use gist quite a bit. It is availale for Unix and integrated nicely with
NumPy.  I've attached some code which emulates the Matlab plot command
(mostly) using gist.  It is quite handy for interactive plotting.  I've
used gnuplot before too and it works well.  Look at the scientific
computing topic page at www.python.org there is a link to Janko Hauser's
page on plotting packages.  

The rapid GUI stuff will require using graphite on a Tk or Gtk or wxPython
canvas.  It is much, much more flexible than Matlab.

> 
> And lastly, can array operations (using NumPy) be done without for loops
> as in IDL/MatLab?  eg Multiplying 2 arrays is easy in these two packages
> because you don't need array subscripting and iteration.
> 

Yes, yes, yes.  The NumPy array is quite nice.  After using Matlab for
several years, I switched to Python and have really enjoyed the added
power and flexibility of Python with NumPy.

Oh, don't count out Fortan 77.  With the recently released PyFort and f2py
(check the matrix-sig) you can link Fortran codes into NumPy easily.  A
linked-in subroutine in Fortran can be used to get compiled speed and ease
of development.


-------------- next part --------------

import gist
import Numeric

_types = {'-':'solid','--':'dash',':':'dot','-.':'dashdot','-:':'dashdotdot'}
_corder = ['b','r','m','g','c','k','y']
_colors = {'k':'black','r':'red','b':'blue','m':'magenta','g':'green','y':'yellow','c':'cyan'}
_markers = { '+':'\2','.':'\1','*':'\3','o':'\4','x':'\5'}


def _find_and_set(dict, str, default):
    import string
    value = default
    for k in dict.keys():
        if string.find(str,k) >= 0:
            value = dict[k]
            break
    return value

def _parse_type_arg(thearg,nowplotting):
    indx = nowplotting % len(_corder)
    if type(thearg) is type(''):
        tomark = 1

        thetype = _find_and_set(_types,thearg,'none')
        thecolor = _find_and_set(_colors,thearg,_colors[_corder[indx]])
        themarker = _find_and_set(_markers,thearg,None)

        if (themarker == None):
            tomark = 0
            if thetype == 'none':
                thetype = 'solid'        

        return (thetype, thecolor, themarker, tomark)

    else:  # no string this time
        return ('solid',_colors[_corder[indx]],'Z',0)

global hold_on
hold_on = 0

def plot(x,y=None,*args):
    global hold_on
    if hold_on == 0:
        gist.fma()
    gist.animate(0)
    nargs = len(args)
    if nargs == 0:
        if y == None:
            y = x
            x = Numeric.arange(0,len(y))
        gist.plg(y,x,type='solid',color='blue',marks=0)
        return
    argpos = 0
    nowplotting = 0
    while 1:
        try:
            thearg = args[argpos]
        except:
            thearg = 0
        thetype,thecolor,themarker,tomark = _parse_type_arg(thearg,nowplotting)
        if themarker == 'Z':  # args[argpos] was data or non-existent.
            pass
        else:                 # args[argpos] was a string
            argpos = argpos + 1    
        gist.plg(y,x,type=thetype,color=thecolor,marker=themarker,marks=tomark)

        if argpos+1 >= nargs: break      # no more data
        nowplotting = nowplotting + 1
        x = args[argpos]
        y = args[argpos+1]
        argpos = argpos+2
    return

def imagesc(z):
    gist.fma()
    gist.animate(0)
    gist.pli(z)

def xlabel(text):
    gist.plt(text,0.40,0.40,justify='CC',opaque=1)






More information about the Python-list mailing list