| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section is devoted to visualization of 1D data arrays. 1D means the data which depend on single index (parameter) like curve in parametric form {x(i),y(i),z(i)}, i=1...n. Most of samples will use the same data for plotting. So, I put its initialization in separate function
void mgls_prepare1d(mglData *y, mglData *y1=0, mglData *y2=0, mglData *x1=0, mglData *x2=0)
{
register long i,n=50;
if(y) y->Create(n,3);
if(x1) x1->Create(n); if(x2) x2->Create(n);
if(y1) y1->Create(n); if(y2) y2->Create(n);
mreal xx;
for(i=0;i<n;i++)
{
xx = i/(n-1.);
if(y)
{
y->a[i] = 0.7*sin(2*M_PI*xx) + 0.5*cos(3*M_PI*xx) + 0.2*sin(M_PI*xx);
y->a[i+n] = sin(2*M_PI*xx);
y->a[i+2*n] = cos(2*M_PI*xx);
}
if(y1) y1->a[i] = 0.5+0.3*cos(2*M_PI*xx);
if(y2) y2->a[i] = 0.3*sin(2*M_PI*xx);
if(x1) x1->a[i] = xx*2-1;
if(x2) x2->a[i] = 0.05+0.03*cos(2*M_PI*xx);
}
}
or using C functions
void mgls_prepare1d(HMDT y, HMDT y1=0, HMDT y2=0, HMDT x1=0, HMDT x2=0)
{
register long i,n=50;
if(y) mgl_data_create(y,n,3,1);
if(x1) mgl_data_create(x1,n,1,1);
if(x2) mgl_data_create(x2,n,1,1);
if(y1) mgl_data_create(y1,n,1,1);
if(y2) mgl_data_create(y2,n,1,1);
mreal xx;
for(i=0;i<n;i++)
{
xx = i/(n-1.);
if(y)
{
mgl_data_set_value(y, 0.7*sin(2*M_PI*xx) + 0.5*cos(3*M_PI*xx) + 0.2*sin(M_PI*xx), i,0,0);
mgl_data_set_value(y, sin(2*M_PI*xx), i,1,0);
mgl_data_set_value(y, cos(2*M_PI*xx), i,2,0);
}
if(y1) mgl_data_set_value(y1, 0.5+0.3*cos(2*M_PI*xx), i,0,0);
if(y2) mgl_data_set_value(y2, 0.3*sin(2*M_PI*xx), i,0,0);
if(x1) mgl_data_set_value(x1, xx*2-1, i,0,0);
if(x2) mgl_data_set_value(x2, 0.05+0.03*cos(2*M_PI*xx), i,0,0);
}
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function plot is most standard way to visualize 1D data array. By default, Plot use colors from palette. However, you can specify manual color/palette, and even set to use new color for each points by using ‘!’ style. Another feature is ‘ ’ style which draw only markers without line between points. The sample code is:
int sample(mglGraph *gr)
{
mglData y; mgls_prepare1d(&y); gr->SetOrigin(0,0,0);
gr->SubPlot(2,2,0,""); gr->Title("Plot plot (default)");
gr->Box(); gr->Plot(y);
gr->SubPlot(2,2,2,""); gr->Title("'!' style; 'rgb' palette");
gr->Box(); gr->Plot(y,"o!rgb");
gr->SubPlot(2,2,3,""); gr->Title("just markers");
gr->Box(); gr->Plot(y," +");
gr->SubPlot(2,2,1); gr->Title("3d variant");
gr->Rotate(50,60); gr->Box();
mglData yc(30), xc(30), z(30); z.Modify("2*x-1");
yc.Modify("sin(pi*(2*x-1))"); xc.Modify("cos(pi*2*x-pi)");
gr->Plot(xc,yc,z,"rs");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function radar plot is variant of Plot one, which make plot in polar coordinates and draw radial rays in point directions. If you just need a plot in polar coordinates then I recommend to use Curvilinear coordinates or Plot in parabolic form with x=r*cos(fi); y=r*sin(fi);. The sample code is:
int sample(mglGraph *gr)
{
mglData yr(10,3); yr.Modify("0.4*sin(pi*(2*x+y))+0.1*rnd");
gr->SubPlot(1,1,0,""); gr->Title("Radar plot (with grid, '\\#')");
gr->Radar(yr,"#");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function step plot data as stairs. It have the same options as Plot. The sample code is:
int sample(mglGraph *gr)
{
mglData y; mgls_prepare1d(&y); gr->SetOrigin(0,0,0);
mglData yc(30), xc(30), z(30); z.Modify("2*x-1");
yc.Modify("sin(pi*(2*x-1))"); xc.Modify("cos(pi*2*x-pi)");
gr->SubPlot(2,2,0,""); gr->Title("Step plot (default)");
gr->Box(); gr->Step(y);
gr->SubPlot(2,2,1); gr->Title("3d variant"); gr->Rotate(50,60);
gr->Box(); gr->Step(xc,yc,z,"r");
gr->SubPlot(2,2,2,""); gr->Title("'!' style");
gr->Box(); gr->Step(y,"s!rgb");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function tens is variant of plot with smooth coloring along the curves. At this, color is determined as for surfaces (see Color scheme). The sample code is:
int sample(mglGraph *gr)
{
mglData y; mgls_prepare1d(&y); gr->SetOrigin(0,0,0);
gr->SubPlot(2,2,0,""); gr->Title("Tens plot (default)");
gr->Box(); gr->Tens(y.SubData(-1,0), y.SubData(-1,1));
gr->SubPlot(2,2,2,""); gr->Title("' ' style");
gr->Box(); gr->Tens(y.SubData(-1,0), y.SubData(-1,1),"o ");
gr->SubPlot(2,2,1); gr->Title("3d variant"); gr->Rotate(50,60); gr->Box();
mglData yc(30), xc(30), z(30); z.Modify("2*x-1");
yc.Modify("sin(pi*(2*x-1))"); xc.Modify("cos(pi*2*x-pi)");
gr->Tens(xc,yc,z,z,"s");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function area fill the area between curve and axis plane. It support gradient filling if 2 colors per curve is specified. The sample code is:
int sample(mglGraph *gr)
{
mglData y; mgls_prepare1d(&y); gr->SetOrigin(0,0,0);
gr->SubPlot(2,2,0,""); gr->Title("Area plot (default)");
gr->Box(); gr->Area(y);
gr->SubPlot(2,2,1,""); gr->Title("2 colors");
gr->Box(); gr->Area(y,"cbgGyr");
gr->SubPlot(2,2,2,""); gr->Title("'!' style");
gr->Box(); gr->Area(y,"!");
gr->SubPlot(2,2,3); gr->Title("3d variant");
gr->Rotate(50,60); gr->Box();
mglData yc(30), xc(30), z(30); z.Modify("2*x-1");
yc.Modify("sin(pi*(2*x-1))"); xc.Modify("cos(pi*2*x-pi)");
gr->Area(xc,yc,z,"r");
yc.Modify("-sin(pi*(2*x-1))"); gr->Area(xc,yc,z,"b#");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function region fill the area between 2 curves. It support gradient filling if 2 colors per curve is specified. Also it can fill only the region y1<y<y2 if style ‘i’ is used. The sample code is:
int sample(mglGraph *gr)
{
mglData y; mgls_prepare1d(&y);
mglData y1 = y.SubData(-1,1), y2 = y.SubData(-1,2); gr->SetOrigin(0,0,0);
gr->SubPlot(2,2,0,""); gr->Title("Region plot (default)");
gr->Box(); gr->Region(y1,y2); gr->Plot(y1,"k2"); gr->Plot(y2,"k2");
gr->SubPlot(2,2,1,""); gr->Title("2 colors");
gr->Box(); gr->Region(y1,y2,"yr"); gr->Plot(y1,"k2"); gr->Plot(y2,"k2");
gr->SubPlot(2,2,2,""); gr->Title("'!' style");
gr->Box(); gr->Region(y1,y2,"!"); gr->Plot(y1,"k2"); gr->Plot(y2,"k2");
gr->SubPlot(2,2,3,""); gr->Title("'i' style");
gr->Box(); gr->Region(y1,y2,"ir"); gr->Plot(y1,"k2"); gr->Plot(y2,"k2");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function stem draw vertical bars. It is most attractive if markers are drawn too. The sample code is:
int sample(mglGraph *gr)
{
mglData y; mgls_prepare1d(&y); gr->SetOrigin(0,0,0);
mglData yc(30), xc(30), z(30); z.Modify("2*x-1");
yc.Modify("sin(pi*(2*x-1))"); xc.Modify("cos(pi*2*x-pi)");
gr->SubPlot(2,2,0,""); gr->Title("Stem plot (default)");
gr->Box(); gr->Stem(y);
gr->SubPlot(2,2,1); gr->Title("3d variant"); gr->Rotate(50,60);
gr->Box(); gr->Stem(xc,yc,z,"rx");
gr->SubPlot(2,2,2,""); gr->Title("'!' style");
gr->Box(); gr->Stem(y,"o!rgb");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function bars draw vertical bars. It have a lot of options: bar-above-bar (‘a’ style), fall like (‘f’ style), 2 colors for positive and negative values, wired bars (‘#’ style), 3D variant. The sample code is:
int sample(mglGraph *gr)
{
mglData ys(10,3); ys.Modify("0.8*sin(pi*(2*x+y/2))+0.2*rnd");
gr->SetOrigin(0,0,0);
gr->SubPlot(3,2,0,""); gr->Title("Bars plot (default)");
gr->Box(); gr->Bars(ys);
gr->SubPlot(3,2,1,""); gr->Title("2 colors");
gr->Box(); gr->Bars(ys,"cbgGyr");
gr->SubPlot(3,2,4,""); gr->Title("'\\#' style");
gr->Box(); gr->Bars(ys,"#");
gr->SubPlot(3,2,5); gr->Title("3d variant");
gr->Rotate(50,60); gr->Box();
mglData yc(30), xc(30), z(30); z.Modify("2*x-1");
yc.Modify("sin(pi*(2*x-1))"); xc.Modify("cos(pi*2*x-pi)");
gr->Bars(xc,yc,z,"r");
gr->SetRanges(-1,1,-3,3);
gr->SubPlot(3,2,2,""); gr->Title("'a' style");
gr->Box(); gr->Bars(ys,"a");
gr->SubPlot(3,2,3,""); gr->Title("'f' style");
gr->Box(); gr->Bars(ys,"f");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function barh is the similar to Bars but draw horizontal bars. The sample code is:
int sample(mglGraph *gr)
{
mglData ys(10,3); ys.Modify("0.8*sin(pi*(2*x+y/2))+0.2*rnd");
gr->SetOrigin(0,0,0);
gr->SubPlot(2,2,0,""); gr->Title("Barh plot (default)");
gr->Box(); gr->Barh(ys);
gr->SubPlot(2,2,1,""); gr->Title("2 colors");
gr->Box(); gr->Barh(ys,"cbgGyr");
gr->SetRanges(-3,3,-1,1);
gr->SubPlot(2,2,2,""); gr->Title("'a' style");
gr->Box(); gr->Barh(ys,"a");
gr->SubPlot(2,2,3,""); gr->Title("'f' style");
gr->Box(); gr->Barh(ys,"f");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function cones is similar to Bars but draw cones. The sample code is:
int sample(mglGraph *gr)
{
mglData ys(10,3); ys.Modify("0.8*sin(pi*(2*x+y/2))+0.2*rnd");
gr->Light(true); gr->SetOrigin(0,0,0);
gr->SubPlot(2,2,0); gr->Title("Cones plot");
gr->Rotate(50,60); gr->Box(); gr->Cones(ys);
gr->SubPlot(2,2,1); gr->Title("2 colors");
gr->Rotate(50,60); gr->Box(); gr->Cones(ys,"cbgGyr");
gr->SubPlot(2,2,2); gr->Title("'#' style");
gr->Rotate(50,60); gr->Box(); gr->Cones(ys,"#");
gr->SubPlot(2,2,3); gr->Title("'a' style");
gr->SetRange('z',-2,2); // increase range since summation can exceed [-1,1]
gr->Rotate(50,60); gr->Box(); gr->Cones(ys,"a");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function chart draw colored boxes with width proportional to data values. Use ‘ ’ for empty box. Plot looks most attractive in polar coordinates – well known pie chart. The sample code is:
int sample(mglGraph *gr)
{
mglData ch(7,2); for(int i=0;i<7*2;i++) ch.a[i]=mgl_rnd()+0.1;
gr->SubPlot(2,2,0); gr->Title("Chart plot (default)");
gr->Light(true); gr->Rotate(50,60); gr->Box(); gr->Chart(ch);
gr->SubPlot(2,2,1); gr->Title("'\\#' style");
gr->Rotate(50,60); gr->Box(); gr->Chart(ch,"#");
gr->SubPlot(2,2,2); gr->Title("Pie chart; ' ' color");
gr->SetFunc("(y+1)/2*cos(pi*x)","(y+1)/2*sin(pi*x)","");
gr->Rotate(50,60); gr->Box(); gr->Chart(ch,"bgr cmy#");
gr->SubPlot(2,2,3); gr->Title("Ring chart; ' ' color");
gr->SetFunc("(y+2)/3*cos(pi*x)","(y+2)/3*sin(pi*x)","");
gr->Rotate(50,60); gr->Box(); gr->Chart(ch,"bgr cmy#");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function boxplot draw box-and-whisker diagram. The sample code is:
int sample(mglGraph *gr)
{
mglData a(10,7); a.Modify("(2*rnd-1)^3/2");
gr->SubPlot(1,1,0,""); gr->Title("Boxplot plot");
gr->Box(); gr->BoxPlot(a);
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function candle draw candlestick chart. This is a combination of a line-chart and a bar-chart, in that each bar represents the range of price movement over a given time interval. The sample code is:
int sample(mglGraph *gr)
{
mglData y(30); gr->Fill(y,"sin(pi*x/2)^2");
mglData y1(30); gr->Fill(y1,"v/2",y);
mglData y2(30); gr->Fill(y2,"(1+v)/2",y);
gr->SubPlot(1,1,0,""); gr->Title("Candle plot (default)");
gr->SetRange('y',0,1); gr->Box(); gr->Candle(y,y1,y2);
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function error draw error boxes around the points. You can draw default boxes or semi-transparent symbol (like marker, see Line styles). Also you can set individual color for each box. The sample code is:
int sample(mglGraph *gr)
{
mglData y; mgls_prepare1d(&y);
mglData x0(10), y0(10), ex0(10), ey0(10);
mreal x;
for(int i=0;i<10;i++)
{
x = i/9.;
x0.a[i] = 2*x-1 + 0.1*mgl_rnd()-0.05;
y0.a[i] = 0.7*sin(2*M_PI*x)+0.5*cos(3*M_PI*x)+0.2*sin(M_PI*x)+0.2*mgl_rnd()-0.1;
ey0.a[i]=0.2; ex0.a[i]=0.1;
}
gr->SubPlot(2,2,0,""); gr->Title("Error plot (default)");
gr->Box(); gr->Plot(y.SubData(-1,0)); gr->Error(x0,y0,ex0,ey0,"ko");
gr->SubPlot(2,2,1,""); gr->Title("'!' style; no e_x");
gr->Box(); gr->Plot(y.SubData(-1,0)); gr->Error(x0,y0,ey0,"o!rgb");
gr->SubPlot(2,2,2,""); gr->Title("'\\@' style");
gr->Box(); gr->Plot(y.SubData(-1,0)); gr->Error(x0,y0,ex0,ey0,"@","alpha 0.5");
gr->SubPlot(2,2,3); gr->Title("3d variant"); gr->Rotate(50,60);
for(int i=0;i<10;i++)
gr->Error(mglPoint(2*mgl_rnd()-1,2*mgl_rnd()-1,2*mgl_rnd()-1),
mglPoint(0.2,0.2,0.2),"bo");
gr->Axis();
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function mark draw markers at points. It is mostly the same as Plot but marker size can be variable. The sample code is:
int sample(mglGraph *gr)
{
mglData y,y1; mgls_prepare1d(&y,&y1);
gr->SubPlot(1,1,0,""); gr->Title("Mark plot (default)");
gr->Box(); gr->Mark(y,y1,"s");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function textmark like Mark but draw text instead of markers. The sample code is:
int sample(mglGraph *gr)
{
mglData y,y1; mgls_prepare1d(&y,&y1);
gr->SubPlot(1,1,0,""); gr->Title("TextMark plot (default)");
gr->Box(); gr->TextMark(y,y1,"\\gamma","r");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function label print text at data points. The string may contain ‘%x’, ‘%y’, ‘%z’ for x-, y-, z-coordinates of points, ‘%n’ for point index. The sample code is:
int sample(mglGraph *gr)
{
mglData ys(10); ys.Modify("0.8*sin(pi*2*x)+0.2*rnd");
gr->SubPlot(1,1,0,""); gr->Title("Label plot");
gr->Box(); gr->Plot(ys," *"); gr->Label(ys,"y=%y");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function table draw table with data values. The sample code is:
int sample(mglGraph *gr)
{
mglData ys(10,3); ys.Modify("0.8*sin(pi*(2*x+y/2))+0.2*rnd");
gr->SubPlot(2,2,0); gr->Title("Table plot");
gr->Table(ys,"y_1\ny_2\ny_3"); gr->Box();
gr->SubPlot(2,2,1); gr->Title("no borders, colored");
gr->Table(ys,"y_1\ny_2\ny_3","r|");
gr->SubPlot(2,2,2); gr->Title("no font decrease");
gr->Table(ys,"y_1\ny_2\ny_3","#");
gr->SubPlot(2,2,3); gr->Title("manual width, position");
gr->Table(0.5, 0.95, ys,"y_1\ny_2\ny_3","#", "value 0.7"); gr->Box();
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function tube draw tube with variable radius. The sample code is:
int sample(mglGraph *gr)
{
mglData y,y1,y2; mgls_prepare1d(&y,&y1,&y2); y1/=20;
gr->SubPlot(2,2,0,""); gr->Title("Tube plot (default)");
gr->Light(true); gr->Box(); gr->Tube(y,0.05);
gr->SubPlot(2,2,1,""); gr->Title("variable radius");
gr->Box(); gr->Tube(y,y1);
gr->SubPlot(2,2,2,""); gr->Title("'\\#' style");
gr->Box(); gr->Tube(y,0.05,"#");
mglData yc(50), xc(50), z(50); z.Modify("2*x-1");
yc.Modify("sin(pi*(2*x-1))"); xc.Modify("cos(pi*2*x-pi)");
gr->SubPlot(2,2,3); gr->Title("3d variant"); gr->Rotate(50,60);
gr->Box(); gr->Tube(xc,yc,z,y2,"r");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function tape draw tapes which rotate around the curve as normal and binormal. The sample code is:
int sample(mglGraph *gr)
{
mglData y; mgls_prepare1d(&y);
mglData xc(50), yc(50), z(50);
yc.Modify("sin(pi*(2*x-1))");
xc.Modify("cos(pi*2*x-pi)"); z.Fill(-1,1);
gr->SubPlot(2,2,0,""); gr->Title("Tape plot (default)");
gr->Box(); gr->Tape(y); gr->Plot(y,"k");
gr->SubPlot(2,2,1); gr->Title("3d variant, 2 colors");
gr->Rotate(50,60); gr->Light(true);
gr->Box(); gr->Plot(xc,yc,z,"k"); gr->Tape(xc,yc,z,"rg");
gr->SubPlot(2,2,2); gr->Title("3d variant, x only"); gr->Rotate(50,60);
gr->Box(); gr->Plot(xc,yc,z,"k");
gr->Tape(xc,yc,z,"xr"); gr->Tape(xc,yc,z,"xr#");
gr->SubPlot(2,2,3); gr->Title("3d variant, z only"); gr->Rotate(50,60);
gr->Box(); gr->Plot(xc,yc,z,"k");
gr->Tape(xc,yc,z,"zg"); gr->Tape(xc,yc,z,"zg#");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Function torus draw surface of the curve rotation. The sample code is:
int sample(mglGraph *gr)
{
mglData y1,y2; mgls_prepare1d(0,&y1,&y2);
gr->SubPlot(2,2,0); gr->Title("Torus plot (default)");
gr->Light(true); gr->Rotate(50,60); gr->Box(); gr->Torus(y1,y2);
if(mini) return;
gr->SubPlot(2,2,1); gr->Title("'x' style"); gr->Rotate(50,60);
gr->Box(); gr->Torus(y1,y2,"x");
gr->SubPlot(2,2,2); gr->Title("'z' style"); gr->Rotate(50,60);
gr->Box(); gr->Torus(y1,y2,"z");
gr->SubPlot(2,2,3); gr->Title("'\\#' style"); gr->Rotate(50,60);
gr->Box(); gr->Torus(y1,y2,"#");
return 0;
}
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] |
This document was generated by Build Daemon user on December 22, 2013 using texi2html 1.82.