Saturday, November 16, 2013

gnuplot, octave, and python

Both Octave and Python use gnuplot to produce graphs.  I don't know why I've stayed away from graphs for so long.  They're really quite simple to produce.  Since I'm trying to relearn Octave right now I'm going to show a couple of issues with Octave's implementation.  It might be a problem with gnuplot, I'll do some testing this afternoon.  I only have a few minutes right now so here goes.

This code:


figure(1)
clf
x=-10:0.1:10;

subplot(2,1,1)
plot(x,sin(x))
xlabel('x','fontsize',12)
ylabel('Sin(x)','fontsize',12)
text(-13,1,'(1)','fontsize',12) 

subplot(2,1,2)
plot(x,2 * sin(x))
xlabel('x','fontsize',12)
ylabel('2 * Sin(x)','fontsize',12)
text(-13,1,'(2)','fontsize',12) 


produces this graph:
Notice the text at (-13,1)?  It is positioned based upon the coordinate system used for the plot.  Unless you adjust for this you're going to get bad results like I've shown here.  Fontsize isn't affected so getting the position correct can be complicated if one doesn't use some tricks.  I'll be doing some tests this afternoon to see if I can turn hold on and build the plot using one axis setting and position the text using another.  I'll let you know what I find.

The second issue is the xlabel on the second subplot.  For some reason it gets chopped off.  There may be quite a few details one will need to remember to produce nice plots.  One might want to build a library of sample plots  so the code to produce them is kept handy.

No comments:

Post a Comment