Plplot绘制随时间变换的动态图

Plplot绘制随时间变换的动态图_第1张图片

这是一张随时间变换的动态图像,用到的plot函数有:

// Create 1d stripchart,可同时绘4条函数
    void stripc( PLINT *id, const char *xspec, const char *yspec,
                 PLFLT xmin, PLFLT xmax, PLFLT xjump, PLFLT ymin, PLFLT ymax,
                 PLFLT xlpos, PLFLT ylpos,
                 bool y_ascl, bool acc,
                 PLINT colbox, PLINT collab,
                 const PLINT colline[], const PLINT styline[], const char *legline[],
                 const char *labx, const char *laby, const char *labtop );

// Add a point to a stripchart.
    void stripa( PLINT id, PLINT pen, PLFLT x, PLFLT y );
// Deletes and releases memory used by a stripchart.
    void stripd( PLINT id );

程序代码如下:

#include "plc++demos.h"

#ifdef PL_HAVE_UNISTD_H
# include 
#else
# ifdef PL_HAVE_POLL
#  include 
# endif
#endif

#ifdef PL_USE_NAMESPACE
using namespace std;
#endif
plstream *pls;
static PLINT pl_errcode= 0;
static char  errmsg[160]= "";

int main( int argc, const char ** argv )
{
	PLINT           id_StripChart,  nsteps = 1000;
	bool            autoy, acc;
	PLFLT           y1, y2, y3, y4, init_ymin, init_ymax, xlab, ylab;
	PLFLT           t, init_tmin, init_tmax, tjump, dt, noise;
	PLINT           colbox, collab, colline[4]={0}, styline[4]={0};
	const char      *legline[4] ;
	// plplot initialization

	pls = new plstream();

	// Parse and process command line arguments.
	pls->parseopts( &argc, argv, PL_PARSE_FULL );
	// Specify some reasonable defaults for ymin and ymax
	// The plot will grow automatically if needed (but not shrink)

	init_ymin = -0.1;
	init_ymax = 0.1;

	// Specify initial tmin and tmax -- this determines length of window.
	// Also specify maximum jump in t
	// This can accomodate adaptive timesteps

	init_tmin  = 0.;
	init_tmax  = 10.;
	tjump = 0.3;      // percentage of plot to jump

	// Axes options same as plbox.
	// Only automatic tick generation and label placement allowed
	// Eventually I'll make this fancier

	colbox     = 1;
	collab     = 3;
	styline[0] = colline[0] = 2;      // pens color and line style
	styline[1] = colline[1] = 2;
	styline[2] = colline[2] = 2;
	styline[3] = colline[3] = 2;

	legline[0] = "sin";                       // pens legend
	legline[1] = "";
	legline[2] = "";
	legline[3] = "";

	xlab = 0.; ylab = 0.25;   // legend position

	autoy = true;             // autoscale y
	acc   = true;             // don't scrip, accumulate

	// Initialize PLplot.
	pls->sdev("qtwidget");
	pls->init();

	pls->adv( 0 );
	pls->vsta();

	// Register our error variables with PLplot
	// From here on, we're handling all errors here

	pls->sError( &pl_errcode, errmsg );

	pls->stripc( &id_StripChart, "bcnst", "bcnstv",
		init_tmin, init_tmax, tjump, init_ymin, init_ymax,
		xlab, ylab,
		autoy, acc,
		colbox, collab,
		colline, styline, legline,
		"t", "", "Strip chart demo" );

	if ( pl_errcode )
	{
		cout << errmsg << endl;
		delete pls;
		exit( 1 );
	}

	// Let plplot handle errors from here on

	pls->sError( NULL, NULL );

	autoy = false; // autoscale y
	acc   = true;  // accumulate/

	// This is to represent a loop over time
	// Let's try a random walk process

	y1 = y2 = y3 = y4 = 0.0;
	dt = 0.1;
	for ( PLINT n = 0; n < nsteps; n++ )
	{
		t     = (double) n * dt;
		y1    = sin( t * M_PI / 18. );
		// There is no need for all pens to have the same number of
		// points or beeing equally time spaced.
		pls->stripa( id_StripChart, 0, t, y1 );
	}
	// Destroy strip chart and it's memory
	pls->stripd( id_StripChart );
	delete pls;
	return 0;
}
vs2010工程地址:plplot_time
参考文献:http://plplot.sourceforge.net/examples.php?demo=17&lbind=C%2B%2B

你可能感兴趣的:(plplot)