1.7.2 Adding Data Plots
Plots or Data plots are representations of your data within a graph layer. Each graph layer may contain one or more plots.
Contents
2D Plot (XY, YErr, Bar/Column)
Plot XY Scatter
The following code shows how to construct an XYYErr data range from the active worksheet, and then plot the data range in a newly created graph.
Worksheet wks = Project.ActiveLayer(); // The range name must be X, Y, Z or ED(for YErr) to make sense. DataRange dr; dr.Add(wks, 0, "X"); // 1st column for X data dr.Add(wks, 1, "Y"); // 2nd column for Y data dr.Add(wks, 2, "ED"); // Optional, 3th column for Y Error data // Create a graph window GraphPage gp; gp.Create("Origin"); GraphLayer gl = gp.Layers(); // Get active layer // Plot XY data range as scatter // IDM_PLOT_SCATTER is plot type id, see other types plot id in oPlotIDs.h file. int nPlotIndex = gl.AddPlot(dr, IDM_PLOT_SCATTER); // Returns plot index (offset is 0), else return -1 for error if( nPlotIndex >= 0 ) { gl.Rescale(); // Rescale axes to show all data points }
Attach YErr Plot
Attach YErr data to an existing XY data plot.
GraphLayer gl = Project.ActiveLayer(); DataPlot dp = gl.DataPlots(-1); // Get active data plot // Get Y Error column WorksheetPage wksPage("Book1"); Worksheet wks = wksPage.Layers(); Column colErrBar(wks, 2); // Plot Y Error column to the active data plot Curve crv(dp); int nErrPlotIndex = gl.AddErrBar(crv, colErrBar); out_int("nErrPlotIndex = ", nErrPlotIndex);
Bar/Column Plot
// before running make sure the active window is worksheet Worksheet wks = Project.ActiveLayer(); DataRange dr; dr.Add(wks, 1, "Y"); // Construct data range with one column GraphPage gp; gp.Create("BAR"); // Create graph with the specified template GraphLayer gl = gp.Layers(-1); // Get active graph layer int index = gl.AddPlot(dr, IDM_PLOT_BAR); if( index >= 0 ) { out_str("Plot bar"); gl.Rescale(); }
3D Plot
Plot a 3D surface from a matrix on a graph window.
// Prepare matrix data MatrixLayer ml; string strFile = GetAppPath(true) + "Samples\\Matrix Conversion and Gridding\\ 2D Gaussian.ogm"; ml.Open(strFile); MatrixObject mo = ml.MatrixObjects(0); // Create graph page with template GraphPage gp; gp.Create("CMAP"); GraphLayer gl = gp.Layers(0); // Plot 3D surface int nPlotIndex = gl.AddPlot(mo, IDM_PLOT_SURFACE_COLORMAP); if(0 == nPlotIndex) { gl.Rescale(); printf("3D Surface plotted successfully\n"); }
Contour Plot
Plot XYZ Contour
// Before running, make sure there are XYZ columns with data in the active // worksheet window. Or you can import \Samples\Matrix Conversion and Gridding\ // XYZ Random Gaussian.dat into worksheet. Worksheet wks = Project.ActiveLayer(); DataRange dr; dr.Add(wks, 0, "X"); dr.Add(wks, 1, "Y"); dr.Add(wks, 2, "Z"); // Create graph with template GraphPage gp; gp.Create("TriContour"); GraphLayer gl = gp.Layers(); // Plot XYZ contour with type id int nPlot = gl.AddPlot(dr, IDM_PLOT_TRI_CONTOUR); if( nPlot >= 0 ) { gl.Rescale(); printf("XYZ contour plotted successfully\n"); }
Plot Color Fill Contour
MatrixLayer ml = Project.ActiveLayer(); MatrixObject mo = ml.MatrixObjects(0); // Create graph window with template GraphPage gp; gp.Create("contour"); GraphLayer gl = gp.Layers(); int nPlot = gl.AddPlot(mo, IDM_PLOT_CONTOUR); if( nPlot >= 0 ) { gl.Rescale(); }
Image Plot
MatrixLayer ml = Project.ActiveLayer(); MatrixObject mo = ml.MatrixObjects(0); // Create graph window with template GraphPage gp; gp.Create("image"); GraphLayer gl = gp.Layers(); int nPlot = gl.AddPlot(mo, IDM_PLOT_MATRIX_IMAGE); if( nPlot >= 0 ) { gl.Rescale(); }
Multi-Axes
The following example code shows how to show/hide and set format on the four axes - left, bottom, right, and top in one graph layer.
#include <..\Originlab\graph_utils.h> // needed for AXIS_* GraphLayer gl = Project.ActiveLayer(); // Show all axes and labels. 0 or 1, 1 for show. vector<int> vnAxes(4), vnLabels(4), vnTitles(4); vnAxes[AXIS_BOTTOM] = 1; vnAxes[AXIS_LEFT] = 1; vnAxes[AXIS_TOP] = 1; vnAxes[AXIS_RIGHT] = 1; vnLabels = vnAxes; // Show axis titles of left and bottom axes. 0 or 1, 1 for show. vnTitles[AXIS_BOTTOM] = 1; vnTitles[AXIS_LEFT] = 1; vnTitles[AXIS_TOP] = 0; vnTitles[AXIS_RIGHT] = 0; // Set the major tick and minor tick of all axes as IN format // See other TICK_* items in graph_utils.h. vector<int> vnMajorTicks(4), vnMinorTicks(4); vnMajorTicks[AXIS_BOTTOM] = TICK_IN; vnMajorTicks[AXIS_LEFT] = TICK_IN; vnMajorTicks[AXIS_TOP] = TICK_IN; vnMajorTicks[AXIS_RIGHT] = TICK_IN; vnMinorTicks = vnMajorTicks; gl_smart_show_object(gl, vnAxes, vnLabels, vnTitles, vnMajorTicks, vnMinorTicks);
Multi-Panels (Multi-Layer, with Shared X-Axis)
The following example shows how to construct multiple graph layers in one graph page, all layers sharing the x axis in one layer, then plot XY data sets one by one from a worksheet to each graph layer.
Before compiling the following codes, you need to run this command to build the graph_utils.c file to your current workspace.
run.LoadOC(Originlab\graph_utils.c, 16);
Compile the following Origin C code. Before running, make sure there is a workbook named Book1, and it has one X column and at least two Y columns.
#include <..\Originlab\graph_utils.h> // needed for page_add_layer function // Construct data range from Book1 WorksheetPage wksPage("Book1"); Worksheet wks = wksPage.Layers(0); // get the first worksheet in Book1 DataRange dr; dr.Add(wks, 0, "X"); // 1st column as X data dr.Add(wks, 1, "Y", -1); // 2nd column to last one for Y data // Get the number of Y DWORD dwRules = DRR_GET_DEPENDENT | DRR_NO_FACTORS; int nNumYs = dr.GetNumData(dwRules); // Add more layers with right Axis and link to the 1st layer GraphPage gp; gp.Create("Origin"); while ( gp.Layers.Count() < nNumYs ) { page_add_layer(gp, false, false, false, true, ADD_LAYER_INIT_SIZE_POS_MOVE_OFFSET, false, 0, LINK_STRAIGHT); } // Loop and add plot from each XY data range to graph layer foreach(GraphLayer gl in gp.Layers) { int nLayerIndex = gl.GetIndex(); // Get the sub XY range from dr DataRange drOne; dr.GetSubRange(drOne, dwRules, nLayerIndex); // Plot one XY range to graph layer int nPlot = gl.AddPlot(drOne, IDM_PLOT_LINE); if( nPlot >= 0 ) { DataPlot dp = gl.DataPlots(nPlot); dp.SetColor(nLayerIndex); // Set data plot as different color // Set the ticks and ticklabels of right Y axis auto color gl.YAxis.AxisObjects(AXISOBJPOS_AXIS_SECOND).RightTicks.Color.nVal = gl.YAxis.AxisObjects(AXISOBJPOS_LABEL_SECOND).RightLabels.Color.nVal = INDEX_COLOR_AUTOMATIC; gl.Rescale(); } }