Mat plot vtk

From NA-Wiki

(Difference between revisions)
Jump to: navigation, search
Line 8: Line 8:
matrices and vectors.
matrices and vectors.
Download the source code [ here]. It is documneted using Doxygen.
Download the source code [ here]. It is documneted using Doxygen.
== 2d plot ==
== 2d plot ==

Revision as of 15:02, 1 August 2008

Plotting data can be important when developing numerical codes. Here are some simple classes that allow data in vectors and matrices in a C++ program to be plotted in a similar fashion as they would in MATLAB. It uses the Visualization Toolkit (VTK)

Matrix types need to support indexing with A(i,j). Vector types need to support indexing with v[k].

Templates tested with MTL4 ( and uBLAS ( matrices and vectors.

Download the source code here. It is documneted using Doxygen.


2d plot

Image:Plot 2d.png

Let x and y be uBLAS vectors of the same length. Create an instance if Plot2D_VTK and call plot:

Plot2D_VTK p;

Additional features:

  • Output to PNG (see example code at bottom)
  • Multiple curves in the same plot: Call plot with the second data set before show().
  • Specify line style and color
  • Axis labels and randering resolution
Plot2D_VTK p_2d("x(t)","y(t)",800,600);

double color1[3] = {0.0,0.0,1.0};
double color2[3] = {1.0,0.0,0.0};


Surface plot


Let x and y of size Nx and Ny be uBLAS vectors, and let z be a uBLAS matrix of size Nx by Ny. The basic surfplot mechanism is:

Surfplot_VTK p;,y,z);  

Additional features:

  • Set rendering resolution
  • Output to PNG
  • Control the scaling of the z-axis
  • Control the camera
double cam[3] = {-15.0, 10.0, 12.0};
double focal[3] = {0,0,0}; 
Surf_VTK p_surf(800,600);,y,z,true,cam,focal);

Contour plot

Image:contours.png Image:contours2.png

The contour class works like the surface class. There is an additional argument specifying how many contour lines t draw, and the fourth argument (boolean) set whether to color the plane or the contours.

Contour_VTK p_cont(800,600);

Quiver plot


The quiver class plots a vector field in the plane. It works like the contour class, with two exceptions:

  • Call with two matrices (x and y components of the vector field), and the axes vectors.
  • It is possible to scale the arrow heads.
Quiver_VTK p_quiver(800, 600);
p_quiver.quiver(xx, yy, u, v, 0.1);

Example program

#include "matplot.h"

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>

namespace ublas = boost::numeric::ublas;
typedef ublas::vector<double> Vector;
typedef ublas::matrix<double> Matrix;

using namespace std;
using namespace matplot;

// parameters for 2D plot
const int NN = 40;
const double t_low = 0;
const double t_upp = 5;
const double dt = (t_upp-t_low)/(NN-1);

// parameters for 3D stuff
const int Nx = 200;
const int Ny = 250;
const double x_low = -2.5;
const double x_upp = 1.5;
const double y_low = -2.5;
const double y_upp = 2.5;
const double dx = (x_upp-x_low)/(Nx-1);
const double dy = (y_upp-y_low)/(Ny-1);

#define G(x,y) exp(-(x*x+y*y))*sin(x)*cos(y);

// parameters for quiver plot
const int NNx = 20;
const int NNy = 37;
const double xx_low = 0;
const double xx_upp = 1;
const double yy_low = 0;
const double yy_upp = 1.5;
const double dxx = (xx_upp-xx_low)/(NNx-1);
const double dyy = (yy_upp-yy_low)/(NNy-1);

int main(void)
	int i, j;

	// EXAMPLE: Plot_2D =======================================
	Vector x1(NN);
	Vector y1(NN);
	Vector x2(NN);
	Vector y2(NN);

	double t = t_low;

	for (i=0; i<NN; i++)
		x1(i) = cos(t)*t;
		y1(i) = sin(t);
		x2(i) = sin(t)*t+1;
		y2(i) = cos(t)*(0.1-t);

	Plot2D_VTK p_2d("x(t)", "y(t)", 800, 600);

	double color1[3] =
	{ 0.0, 0.0, 1.0 };
	double color2[3] =
	{ 1.0, 0.0, 0.0 };

	p_2d.plot(x1, y1, color1, "-");
	p_2d.plot(x2, y2, color2, ".-");;

	// EXAMPLE: Contour =======================================

	Vector x(Nx);
	Vector y(Ny);
	Matrix z(Nx, Ny);

	for (i=0; i<Nx; i++)
		x(i) = x_low+i*dx;

	for (i=0; i<Ny; i++)
		y(i) = y_low + i*dy;

	for (i=0; i<Nx; i++)
		for (j=0; j<Ny; j++)
			z(i, j) = G(x(i),y(j));

	Contour_VTK p_cont(800, 600);
	p_cont.contour(x, y, z, true, 20);

	// EXAMPLE: Surf ==========================================

	double cam[3] =
	{ -15.0, 10.0, 12.0 };
	double focal[3] =
	{ 0, 0, 0 };
	Surf_VTK p_surf(800, 600);
	//,y,z,false);, y, z, true, cam, focal);

	// Example: Quiver ========================================
	Vector xx(NNx);
	Vector yy(NNy);
	Matrix u(NNx, NNy);
	Matrix v(NNx, NNy);

	for (i=0; i<NNx; i++)
		xx(i) = xx_low+i*dxx;

	for (i=0; i<NNy; i++)
		yy(i) = yy_low + i*dyy;

	for (i=0; i<NNx; i++)
		for (j=0; j<NNy; j++)
			u(i, j) = -yy(j);
			v(i, j) = xx(i);

	Quiver_VTK p_quiver(800, 600);
	p_quiver.quiver(xx, yy, u, v, 0.1);

	return 0;
Personal tools