PSPlot function creation


MPD78
09-12-2009, 11:55 AM
Hello all,

I have been using the psplot.h file quite a bit and it has been very helpful in the presentation of graphical data. However, I need to send information contained in a variable (a single number of typedef Doub or Int) out the the plot as well and thier isnt't a function for this in the psplot.h file. I thought that I could mimic one of the existing functions that send text out to the plot, but I am not having any luck with it.

Could anyone point me the correct direction on this?

Thanks
Matt

davekw7x
09-12-2009, 04:55 PM
...information contained in a variable (a single number of typedef Doub or Int) ...

Do you mean that you want to put some text on the plot, and the text contains the value of a variable?

You can use sprintf to convert numerical data into a char array and then use PsPlot::label() to place the result:


#include "../code/nr3.h"
#include "../code/psplot.h"

Doub max(const VecDoub & v)
{
Doub retval = 0;
if (v.size() > 0) {
retval = v[0];
for (Int i = 1; i < v.size(); i++) {
if (v[i] > retval) {
retval = v[i];
}
}
}
return retval;
}

int main()
{

Doub xmin = 0.0;
Doub xmax = 4.0;
Doub a = 2.0;
Doub b = -0.5;
Int npoints = 100;
VecDoub x(npoints), y(npoints);
Doub deltax = (xmax - xmin) / (npoints - 1.0);

Int i;
Doub xx;

char lab[100];

for (i = 0, xx = 0; i < npoints; i++, xx += deltax) {
x[i] = xx;
y[i] = a * exp(b * x[i]);
}

PSpage pg("myplot.ps");
PSplot plot1(pg, 100., 500., 100., 500.);
plot1.setlimits(xmin, xmax, 0.0, max(y));
plot1.frame();
plot1.autoscales();
plot1.xlabel("x");

sprintf(lab, "%.2f * exp(%.2f)\n", a, b);
plot1.ylabel(lab);

plot1.lineplot(x, y);
plot1.dot(x[npoints / 3], y[npoints / 3], 5.0);

sprintf(lab, "(x,y) = (%.2f,%.2f)\n", x[npoints / 3], y[npoints / 3]);
plot1.label(lab, x[npoints / 3] + 0.1, y[npoints / 3]);

pg.close();
pg.display();

return 0;
}


See attachment.


Regards,

Dave

MPD78
09-12-2009, 06:14 PM
Do you mean that you want to put some text on the plot, and the text contains the value of a variable?

That is exactly what I am trying to do.

Thanks alot.
Matt

MPD78
09-14-2009, 08:31 AM
You can use sprintf to convert numerical data into a char array and then use PsPlot::label() to place the result:

Dave,

I used the sprintf as you indicated above, used your code as an example, and all worked out great. Now I have all of the variable information that I need going out to the graph.

Thanks
Matt