Mat plot vtk
From NA-Wiki
Line 13: | Line 13: | ||
[[Image:Plot 2d.png]] | [[Image:Plot 2d.png]] | ||
- | Let <tt>x</tt> and <tt>y</tt> be uBLAS vectors of the same length. Create an instance if <tt>Plot2D_VTK</tt> and call <tt>plot</tt>: | + | Let <tt>x</tt> and <tt>y</tt> be uBLAS/MTL4/<other> vectors of the same length. Create an instance if <tt>Plot2D_VTK</tt> and call <tt>plot</tt>: |
<pre> | <pre> | ||
Plot2D_VTK p; | Plot2D_VTK p; | ||
Line 40: | Line 40: | ||
[[Image:surf.png]] | [[Image:surf.png]] | ||
- | Let <tt>x</tt> and <tt>y</tt> of size <tt>Nx</tt> and <tt>Ny</tt> be | + | Let <tt>x</tt> and <tt>y</tt> of size <tt>Nx</tt> and <tt>Ny</tt> be vectors, and let <tt>z</tt> be a matrix of size <tt>Nx</tt> by <tt>Ny</tt>. The basic surfplot mechanism is: |
<pre> | <pre> | ||
Surfplot_VTK p; | Surfplot_VTK p; | ||
Line 80: | Line 80: | ||
</pre> | </pre> | ||
- | == Example program == | + | == Example program (using uBLAS) == |
+ | MTL4 example in the source tarball | ||
<pre> | <pre> |
Revision as of 15:08, 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 (http://www.osl.iu.edu/research/mtl/mtl4/) and uBLAS (http://www.boost.org/doc/libs/1_35_0/libs/numeric/ublas/doc/index.htm) matrices and vectors.
Download the source code here. It is documneted using Doxygen.
Contents |
2d plot
Let x and y be uBLAS/MTL4/<other> vectors of the same length. Create an instance if Plot2D_VTK and call plot:
Plot2D_VTK p; p.plot(x,y); p.show();
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}; p_2d.plot(x1,y1,color1,"-"); p_2d.plot(x2,y2,color2,".-"); p_2d.show();
Surface plot
Let x and y of size Nx and Ny be vectors, and let z be a matrix of size Nx by Ny. The basic surfplot mechanism is:
Surfplot_VTK p; p.surf(x,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); p_surf.surf(x,y,z,true,cam,focal);
Contour plot
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); p_cont.contour(x,y,z,false,50);
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 (using uBLAS)
MTL4 example in the source tarball
#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); t+=dt; } 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, ".-"); p_2d.show(); //p_2d.draw_to_png("plot_2d.png"); // 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); //p_cont.contour_to_file(x,y,z,true,20,"contours.png"); // EXAMPLE: Surf ========================================== double cam[3] = { -15.0, 10.0, 12.0 }; double focal[3] = { 0, 0, 0 }; Surf_VTK p_surf(800, 600); //p_surf.surf(x,y,z); //p_surf.surf(x,y,z,false); p_surf.surf(x, y, z, true, cam, focal); //p_surf.surf_to_file(x,y,z,true,"surf.png",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); //p_quiver.quiver_to_file(xx,yy,u,v,0.1,"quiver.png"); return 0; }