Plotters Part II

October 7, 2010

Yesterday I started some ramblings about plotters. I am sorry to disappoint Alex who was looking foward to hearing about devious schemers. Instead, we have some brief thoughts about mathematical environments, plot libraries, and equation graphers. Tomorrow(ish) will be GUI apps.

Mathematical Environments. Many folk swear by gargantuan packages that provide a programming environment, algorithm libraries, graphics utilities, algebraic manipulation, and so on. Popular packages include IDL, Matlab, Maple and Mathematica . These are very powerful, but with a bit of a learning curve, and of course they all cost lots of dosh. The only free, open source, alternative I am aware of that tries to be a complete Matlab-like replacement is Sage. I haven’t tried this out myself, and would be interested to hear of experiences. The other thing worth mentioning is R which started life as a statistics package and seems to have ballooned into an all-singing all-dancing kind of thing. It has some fans, who have emerged in my recent comment stream, but I haven’t tried it yet. Well, I started trying to read some documentation and nearly lost the will to live.

Plot Libraries. For astronomers at least, the grandaddy is PGPlot, started in 1983 by Tim Pearson and still in everyday use by lots of people. The latest version is 5.2.2 from 2001. It is a library of primitive routines, callable in Fortran or C. Several people have written interactive front ends – WIP, PONGO, and QDP (see Part IV).  PLplot is intended as an updated version of PGPLOT. It has bindings to a very wide range languages, including C, Fortran, Python, Perl, Java, C++, and several others. If Java is your thing, you might also want to look at JFreeChart. If Python is your thing you might want Pyx. It has high level things as well as primitives, and because Python has an interactive mode, you can use it in somewhat the manner you might use Gnuplot. However, as you can see from this example it is distinctly less streamlined and friendly than Gnuplot. Matplotlib is another Python library with both primitives and high level stuff, and ambitions to be a complete environment, a la Matlab. It looks good, but I couldn’t make it work…  There are links to lots more Pythonesque bits and pieces here. So if you want to include plotting in your own programs, there is a lot of choice, but it looks to me like the standard choice these days is PLplot .

Equation Graphers. If all you want is a quick plot of a mathematical function, you can do with this with Gnuplot and cousins, but you are likely to want to explore it – pan and zoom, read out values. In this case an interactive GUI style application makes sense. On the Mac you have this pre-loaded with Grapher, which started life as Graphing Calculator, still independently available here for Mac and Windows. If you want an entertaining hour, you can listen to Ron Avitzur telling the strange story of this application. (There is also a written version of this story.) For Linux folk, there is the wonderfully easy Lybniz – why isn’t there a Macports version ? Another nice lightweight free MacApp is Edengraph but it can’t do some things that Grapher can – like 3D functions, vector fields, independent axis adjustment. Two fairly glitzy commercial applications are A&G Equation Grapher, its cousin Graphing Calculator 3D and Golden Software Grapher. Graphing Calculator 3D has a free version.

Enough for now. Part III will be about GUI-style plotting applications.

Plotters that do what you want

October 6, 2010

I have finally written up my thoughts on plotting packages, but its a bit long .. so I am going to do it in installments …

Just over a year ago I wrote a couple of posts (here and here) about looking for calculators that hit a sweet spot between laborious button-pushing GUI and full blown programming – something where you just type calculations, bring them back and edit them, save a few named variables and constants, maybe a few basic user-defined functions.  I ended up sticking with Python, but also found Gnuplot to be excellent for this purpose. I also really liked two Mac apps – PEMDAS and Speedcrunch –  that cleverly combine GUI and plain command line window interfaces. (Speedcrunch is available for Windows and Linux too.) These were both really nice but had significant gaps, being at an early stage of development. I had hoped by now that they would have progressed significantly, but in fact over a year later they still seem to be at the same version numbers – 0.2.4 and 0.10.1 respectively. (Speedcrunch has a version 0.11 alpha, but still no function definition…) I hope they get a kick start soon.


So what about scientific plotters ? There is a similar choice. You can cook your own, by using libraries of routines inside your own C programs etc, or you can go for a button driven GUI. But the former is often overkill, and the latter tends to be awfully laborious. What you really want most of the time is just a window where you can type “plot sin(x)/x” or whatever, or “read datafile” and “plot col2 vs col6″ etc. Then you can tart up the plot as much as you like with commands that set xmin and xmax, number of ticks etc. And finally of course you save all these commands in a script you can run and edit. So now all the oldies are saying “thats MONGO, man” and the kids are saying “thats Gnuplot, dude”. But after searching the interweb for a while I found some other really interesting possibilities. I am pleased to tell you that my two favourites are both written by Cambridge astronomers – Veusz and Pyxplot. Must be something they put in the East Anglian water. But lets work up towards these. Its going to be helpful to break this into sections :

  1. Mathematical Environments
  2. Plot Libraries
  3. Equation Graphers
  4. GUI plotters
  5. Script plotters

Tomorrow I will have a brief look at parts 1-3, and then a slightly more extensive look at parts 4 and 5 on following days (note vagueness regarding interval..)

Gnuplot calculator

July 27, 2009

A few weeks back I wrote a post on calculators, expressing my desire for something that was more than a push-button emulator, and less than a full programming language. Something where you just have a window  where you can type 2.3*Msun*c**2, define short expressions for re-use, etc. Python seems to be the obvious thing but it has some annoying quirks; I also recommended Plaincalc. The comment stream was fascinating and very educational, with pointers to Wolfram Alpha, the Statusbar calculator for Firefox, and iCosmo, as well as some Python tutorial from Ross C.

I have been thinking about a similar post on graph plotters. While playing with these, I suddenly realised that the plotting package I use most often, gnuplot, does exactly whats needed ! Doh ! You can do calculations – eg print 18.3 * 27.6. You can define and use variables – eg a=pi/6, b=18.3, and then c=b**2*sin(1.3*a).  You can define and use single expression functions – e.g. f1(a,b,x)=a*x**2+b*sin(x)/x;  then with a and b already set you can calculate g=f1(a,b,2.3)/f1(a,b,3.2). Finally of course you can plot f1(a,b,x) – gnuplot assumes x is the variable and other variables are parameters. Then you can change the values of a and b and replot.

So now I have a file of physics and astro constants which is loaded by my .gnuplot initialisation file, so I can type 2.3*Msun*c**2. Next up, some standard functions, eg RBB(nu,T) = (2.*pi/c**2) * nu**3  / (exp(h*nu/(k*T)) – 1.).

So gnuplot is now my standard calculator, rather than Python interactive mode. The Python fans will now tell me that there is a Python version of gnuplot. Indeedy… but thats straying into the graph plotter shoot-out territory…. Watch this space.

Calculators that do what you want

June 7, 2009

I have been on a hunt for the sort of calculator a scientist really wants, and thought I would share some results with youse all. But first let me explain :

I often feel the need for a quick calculation whose complexity is somewhere  between a simple sum and a full blown programme. The sort of thing I have in mind is “how much rest mass energy is in that star ?”, or “what would be the black body luminosity from that object?”. You don’t want to write and compile a C program with loops and so on; but neither do you want to navigate lots of menus and laborious button clickings. You just want to jump in and type 2.3 * Msun * c^2 and get the answer. Or for the second example, something like R=3.28e6 and then T=11000 and then 4*pi*R^2 * BB(R,T); and then T=12000 and repeat. You get the idea.

Unix purists would now explain that you have some of this functionality pre-supplied with bc , and in fact you can do calculations with awk, but these are pretty clunky. These days I do this sort of thing with Python in interactive mode. After firing up Python I load up a short module containing some constants – i.e. I put this file in my PYTHONPATH and type  from astroconst import *.  Then indeed I can just type 2.3 * Msun * c**2. I keep meaning to add some simple functions to address the second type of example, but haven’t gotten round to it. Then I began to wonder the other day whether there are tools out there somewhere that have already done this, so I did a bit of a web crawl. There are gazillions of online and downloadable things that emulate traditional button calculators, some of which have good maths and science-y buttons, but this is not what I was after. There are also web based things for specific purposes – like Ned Wright’s excellent cosmocalc – but again not what I was looking for.

I turned up several very interesting and impressive tools. Give ’em a go.

Plain Calc. Mac only. Plain is the word. When you launch it, there is nothing but an empty white window. Beautiful ! But you can type expressions, create variables and use them, and define one-line functions. The history of what you have done scrolls back up, just like a terminal window. You can save your current worksheet and reload it later; so this way you can save your favourite astro constants etc, and get them back tomorrow. However, what you save and reload is literally the worksheet you were in the middle of, complete with the whole history, whereas what you really want is to simply save the variable and function definitions. So… this app is really fantastic and almost exactly what you want, but … maybe just too plain for some; Mac only; and creating your saved “astro-world” is a bit clunky.

PEMDAS Another Mac App. Similar to Plain Calc but with just a little more GUI-ness, so you can for example see your saved variables at a glance. On the negative side, it doesn’t have user-defined functions. Its at an early stage (0.2.4) so a lot of things could improve… Once its finished its going to cost money – but not much.

Google calculator Now why didn’t I know this before ? In any Google search bar, try typing 3.42*sqrt(13)/(4.2*sin(16)). Even weirder, try G/h. The beast obviously knows some physical constants as well as maths functions. But the “more about calc” link above doesn’t tell you what constants are available….

SpeedCrunch This is available for Windows, Mac, and Linux. Like PEMDAS it combines a plain typing window with GUI elements, but you can switch these on and off. (e.g. you can have it with or without a keypad). It is very nicely designed and easy to use. You can define variables and display their current state. It has an extensive set of built-in constants, but you apply these by clicking on them. They don’t have variable-like names that you can type (e.g. “Msun”).  SpeedCrunch has a very extensive built in function list, but doesn’t have user defined functions, although it seems this is on the to-do list. Like Plain Calc, you can save your session, and when re-loaded you get the whole history. So… this a fantastic app, and is clearly going to get even better, but … its a shame it doesn’t have user-defined functions, and the lack of names for the constants is a niggle.

Python As discussed above, running Python in interpreter mode does pretty much what you want. It comes pre-supplied with Mac and most Linux distros, and is pretty easy to install on Windows.  It has a wide set of functions, you can create variables as you wish, and you can define functions. You can write your own modules with a library of constants or simple functions which you can then use. So thats pretty much everything..  But … there are some useability niggles. (i) Integer division gives the “floor”, i.e. 7/2 gives 3, and 7/9 gives 0. If you really want 7.0/2.0 = 3.5 you have to explicitly put the decimal points in. (ii) Results automatically come with seventeen figs, until a number is too big. So 1.3e7*2.4e9 gives 31200000000000000.0 whereas 1.3e8*2.4e9 gives 3.12e17. If you are writing a module, you can format output however you like, with all that %f stuff etc. But in interpreter mode you are stuck with the default. There doesn’t seem to be a command for “switch to 3 sig figs” or “output everything in exponential notation”. (iii) Python stores reals as integer fractions. This produces some peculiar results. If you define “x=0.3” and then type “x” the interpreter returns 0.29999999999999999. This is all perfectly logical, but like the 7/2 business, the result is that the Python interpreter does not behave like a human would like his/her calculator to behave.

Calc. This is a Linux thing, but is also installable on Mac with Macports or Fink. I haven’t had time to look at this properly yet, but it seems to be a mini-programming language with a syntax pretty much like C and the ability to save and load scripts as well as variables. So it may well be the bees knees, but I couldn’t see an obvious advantage over Python, or a nice tool like Speedcrunch. But may I was just running out of steam.

So, lots of really good things but nothing quite right. Can any Python pundits out there tell me how to fix those interpreter mode niggles ?