The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
 All Forums
 Origin Forum for Programming
 Forum for Origin C
 histograms

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
ovince Posted - 09/27/2006 : 12:32:24 PM
hi All,

Is there a method for making and ploting histograms in OriginC? I have noticed that there was no similar question in Forum's "Search".

The idea is to use plenty of worksheets (with A, B colums for graph and C, D columns for 2 histograms) and to (using loop) put graph+histograms onto the same Layout. Something like:



I have some general ideas how to do it in LabTalk (because I have seen examples in Origin Forum) but I would like to do it in OriginC (all other related routines are in Origin C). From where to start?

Thanks
Oliver
12   L A T E S T    R E P L I E S    (Newest First)
Mike Buess Posted - 10/08/2006 : 12:18:30 PM
Hi Oliver,

The functions compile with no errors in 7.5E SR6. error_report("Error message") just prints "Error Message" to script window and returns false so it can be replaced with false.

Mike Buess
Origin WebRing Member

Edited by - Mike Buess on 10/08/2006 12:26:00 PM
ovince Posted - 10/08/2006 : 12:04:06 PM
hi Mike,


I have Origin 7.5 and the error is steel reported. This is the message

(71) :Error, function or variable error_report not found
(71) :Error, general compile error
(71) :Error, general compile error
(65) :Error, error(s) found in compiling function plot_overlap_hist

When excluded the command line with error_report() than it is OK

Oliver
Mike Buess Posted - 10/08/2006 : 11:39:21 AM
Oliver,

Function error_report is in sys_utils.c which should be in CodeBuilder's System folder in Origin 7.5. The function does not exist in Origin 7.0 where you can safely replace error_report(...) with false.

Mike Buess
Origin WebRing Member
ovince Posted - 10/08/2006 : 09:51:05 AM
hello Deanna,

Thank you.

In the code you wrote an error is reported with the function error_report() as missing. I thing I should #include<> somthing or you have some your-own-written function. The later seems more probable because I could not find this function in Origin's help. Could you send me the function?

Oliver
Deanna Posted - 10/08/2006 : 04:43:16 AM
Another way to do it:


Is it better?

If you need, here is the code:

void plot_and_format_column_graph(Worksheet wks, GraphLayer gl, int iColumn, int iStyle=0, int iColor=0, int iPattern=0)
{
Curve crv(wks, 0, iColumn);
int iPlot = gl.AddPlot( crv, IDM_PLOT_COLUMN );
DataPlot dp = gl.DataPlots(iPlot);

Tree tr;
tr = dp.Curve;
tr.Pattern.Fill.FillColor.nVal = -4; //No filling color
tr.Pattern.Fill.Pattern.Style.nVal = iPattern;
tr.Pattern.Fill.Pattern.Width.nVal = 1;
tr.Spacing.Gap.nVal = 0; //No gaps between the columns
tr.Pattern.Border.Color.nVal = iColor;
tr.Pattern.Border.Style.nVal = iStyle;
dp.Curve = tr;


}


Replace the lines:

plot_and_format_line_graph(wksResult, gl, 3);
plot_and_format_line_graph(wksResult, gl, 1, 1, 1);
plot_and_format_line_graph(wksResult, gl, 2, 2, 3);
in the function plot_overlap_hist with

plot_and_format_column_graph(wksResult, gl, 3);
plot_and_format_column_graph(wksResult, gl, 1, 1, 1, 3);
plot_and_format_column_graph(wksResult, gl, 2, 2, 3, 6);

You will have a graph like the one that is shown above.

Deanna
OriginLab Technical Services
Deanna Posted - 10/08/2006 : 04:30:23 AM
I hope the function below, plot_overlap_hist will be useful. It plots the histogram of two columns from the same worksheet in a single graph, as well as the sum of the counts in each bin, as shown here.


Maybe some modification such as scaling needs to be done.


void plot_and_format_line_graph(Worksheet wks, GraphLayer gl, int iColumn, int iStyle=0, int iColor=0, int iConnect=6)
{
Curve crv(wks, 0, iColumn);
int iPlot = gl.AddPlot( crv, IDM_PLOT_LINE );
DataPlot dp = gl.DataPlots(iPlot);

Tree tr;
tr = dp.Curve;
tr.Line.Color.nVal = iColor;
tr.Line.Style.nVal = iStyle;
tr.Line.Connect.nVal = iConnect;
dp.Curve = tr;


}

bool plot_overlap_hist(string strWksName, uint iColumnIndex1, uint iColumnIndex2, double dStart, double dEnd, double dBinSize, int iColor=1)
{
Worksheet wks(strWksName);
if ( !wks.IsValid() ) return error_report("Invalid worksheet name");

uint iNumCol = wks.Columns.Count();
if ((iColumnIndex1 >= iNumCol) || (iColumnIndex2 >= iNumCol)) return error_report("Invalid column index");

//Create a new worksheet for histogram result
WorksheetPage wpNew;
wpNew.Create("origin");
if( !wpNew.IsValid() ) return error_report("Cannot create a new worksheet page");

Worksheet wksResult(wpNew.Layers(0));

//Customise the result worksheet
while (wksResult.Columns.Count() > 0) wksResult.DeleteCol(0);
wksResult.AddCol("BinCenter");
wksResult.AddCol("Counts1");
wksResult.AddCol("Counts2");
wksResult.AddCol("Sum");
wksResult.SetColDesignations("xyyy");

//Calculate the bin centers
string strCmd;
strCmd.Format("col(1) = data(%f, %f, %f);", dStart + dBinSize / 2, dEnd - dBinSize/2, dBinSize);
LT_execute(strCmd);

//The first histogram
strCmd.Format("(%s, %d);", strWksName, iColumnIndex1 + 1);
LT_execute("%a = %"+strCmd);
strCmd.Format("%f, %f, %f", dBinSize, dStart, dEnd);
LT_execute("col(2) = histogram(%a, " + strCmd + ");");

//The second histogram
strCmd.Format("(%s, %d);", strWksName, iColumnIndex2 + 1);
LT_execute("%a = %"+strCmd);
strCmd.Format("%f, %f, %f", dBinSize, dStart, dEnd);
LT_execute("col(3) = histogram(%a, " + strCmd + ");");

//The sum of the counts
LT_execute("col(4) = col(3) + col(2)");


// Create a graph page
GraphPage gpg;
gpg.Create( "Origin.OTP" );
GraphLayer gl = gpg.Layers( 0 );

//Plot
plot_and_format_line_graph(wksResult, gl, 3);
plot_and_format_line_graph(wksResult, gl, 1, 1, 1);
plot_and_format_line_graph(wksResult, gl, 2, 2, 3);

gl.Rescale();
gl.LT_execute("legend -s;");

return true;
}


Deanna
OriginLab Technical Services
ovince Posted - 09/30/2006 : 03:48:09 AM
hi,

It would be ideal to have something like this



With "ideal" I mean to have 2 (or more) histograms on each-other (as illustrated on the picture) in order to be able to compare histograms range and peak positions quickly by eye. Since I have (again) more than 100 cases to analyse I was thinking to automatize it in Origin C. So about 100 Worksheets are created with

column1: x - independent variable
column2: y - dependent variable
column3: data for 1st histogram
column4: data for 2nd histogram

Then the routine is wtitten (not so large):

void graph_hist_deanna()
{
uint iWksCount = Project.WorksheetPages.Count();
vector<string> vWksName(iWksCount);

int ii = 0;
foreach (WorksheetPage pg in Project.WorksheetPages)
{
vWksName[ii++] = pg.GetName();
}

for (ii=0; ii<iWksCount; ii++)
{
string name = vWksName[ii];
out_str(name);
Worksheet wks(name);


////////// graph ///////////////
// define curves
Curve crvAll(wks, 0, 1);

// Create a graph page
GraphPage gpg;
gpg.Create( "Origin.OTP" );

// create graph Layer
GraphLayer glyg = gpg.Layers( 0 );

// add and format All plot
int iAll = glyg.AddPlot( crvAll, IDM_PLOT_SCATTER );
DataPlot dpAll = glyg.DataPlots(iAll);
dpAll.Curve.Symbol.Shape.nVal = 2;
dpAll.Curve.Symbol.Size.nVal = 4;
dpAll.Curve.Symbol.EdgeColor.nVal = 1;
glyg.Rescale();

//histograms
Dataset dsHist(wks, 2);
double dEnd = max(dsHist);
double dStart = min(dsHist);
double bin=0.05;

int iii = 0;
for (iii=0; iii<2; iii++)
{
plot_hist(name, iii+3, bin, dEnd, dStart,iii+1);
}

//merge_two_graphs();
}

void plot_hist(string strWksName, int iColumnIndex, double dStart, double dEnd, double dBinSize, int iColor)
{
string strCmd;

LT_execute("window -a " + strWksName);
strCmd.Format("worksheet -s %d -1 %d -1;", iColumnIndex, iColumnIndex);
LT_execute(strCmd);

LT_execute("run.section(Plot, Histogram);");

strCmd.Format("%f", dStart);
LT_execute("set %c -hbb " + strCmd);
strCmd.Format("%f", dEnd);
LT_execute("set %c -hbe " + strCmd);
strCmd.Format("%f", dBinSize);
LT_execute("set %c -hbs " + strCmd);
strCmd.Format("%d;", iColor);
LT_execute("set %c -k " + strCmd);

}

void merge_two_graphs()
{
LT_execute("window -m");

//resize and re-position the first layer
LT_execute("layer.width/=2");
LT_execute("layer.left-=2.5");

//resize and re-position the second layer
LT_execute("page.active = 2");
LT_execute("layer.width/=2");
LT_execute("layer.left+=2.5+layer.width");

}


This gives me 3 individual graphs (one plot and 2 unscaled histograms). Is there a posibility to overlap histograms like on the picture?

thanks.


Oliver



Deanna Posted - 09/30/2006 : 01:47:27 AM
How about putting two histograms in a graph like this?


This could be done by plotting two histogram graphs seperately and then merging them.

Here are two small functions which may help. The first is plotting a histogram and setting the starting value, ending value, bin size and color. The second is merging two graphs (Please make sure other graph windows are minimized or hidden. You can use labtalk command: window -i to minimize the active window).



void plot_hist(string strWksName, int iColumnIndex, double dStart, double dEnd, double dBinSize, int iColor=1)
{
string strCmd;

LT_execute("window -a " + strWksName);

strCmd.Format("worksheet -s %d -1 %d -1;", iColumnIndex, iColumnIndex);
LT_execute(strCmd);

LT_execute("run.section(Plot, Histogram);");

strCmd.Format("%f", dStart);
LT_execute("set %c -hbb " + strCmd);

strCmd.Format("%f", dEnd);
LT_execute("set %c -hbe " + strCmd);

strCmd.Format("%f", dBinSize);
LT_execute("set %c -hbs " + strCmd);

strCmd.Format("%d;", iColor);
LT_execute("set %c -k " + strCmd);

}


void merge_two_graphs()
{
LT_execute("window -m");

//resize and re-position the first layer
LT_execute("layer.width/=2");
LT_execute("layer.left-=2.5");

//resize and re-position the second layer
LT_execute("page.active = 2");
LT_execute("layer.width/=2");
LT_execute("layer.left+=2.5+layer.width");

}


If you are not familiar with the color code in Labtalk, you can resort to this function:
color(name)

The color(name) function returns a number corresponding to the index in the color list of the color specified by name.

For example: color(red)=2

P.S. I found that we use a lot of Labtalk commands in this task... Next time when so many Labtalk commands are required, maybe we should consider writing a Labtalk script instead of OC functions, because it will be more convininent.


Deanna
OriginLab Technical Services
ovince Posted - 09/29/2006 : 4:00:57 PM
hello,

thanks many

with this 3 command lines:

//Plotting histogram
LT_execute("window -a "+name);
LT_execute("worksheet -s 2 -1 2 -1;");
LT_execute("run.section(Plot, Histogram)" );

I really obtain 1 histogram defined by Column 3. But how to put 2 histograms (defined by column 3 and column 4) into 1 single graph? How to change ranges and bins for histograms (they are different)? May I color differently two histograms?

I tryed with this but does not work

//Plotting histogram
Dataset dsHist1(wks, 2);
Dataset dsHist2(wks, 3);
double dEnd = max(dsHist);
double dStart = min(dsHist);
double bin1=(dEnd-dStart)/20;
double bin2=(dEnd-dStart)/10;


LT_execute("window -a "+name);
LT_execute("hist_1 = histogram(dsHist1, bin1, dMin, dMax);");
LT_execute("hist_2 = histogram(dsHist2, bin2, dMin, dMax);");
LT_execute("worksheet -s hist_1 -1 hist_1 -1;");
LT_execute("worksheet -s hist_2 -1 hist_2 -1;");
LT_execute("run.section(Plot, Histogram)" );

I make big mistake somewhere.

oliver
Deanna Posted - 09/28/2006 : 11:34:43 PM
I have modified your code. It should be able to make a scatter plot and a histogram from Column 2 of each worksheet.



void graph_hist()
{
uint iWksCount = Project.WorksheetPages.Count();
vector<string> vWksName(iWksCount);

int ii = 0;
foreach (WorksheetPage pg in Project.WorksheetPages)
{
vWksName[ii++] = pg.GetName();
}


for (ii=0; ii<iWksCount; ii++)
{
string name = vWksName[ii];
out_str(name);

Worksheet wks(name);

////////// graph ///////////////
// define curves
Curve crvAll(wks, 0, 1);

// Create a graph page
GraphPage gpg;
gpg.Create( "Origin.OTP" );

// create graph Layer
GraphLayer glyg = gpg.Layers( 0 );

// add and format All plot
int iAll = glyg.AddPlot( crvAll, IDM_PLOT_SCATTER );
DataPlot dpAll = glyg.DataPlots(iAll);
dpAll.Curve.Symbol.Shape.nVal = 2;
dpAll.Curve.Symbol.Size.nVal = 4;
dpAll.Curve.Symbol.EdgeColor.nVal = 1;
glyg.Rescale();

//Plotting histogram
LT_execute("window -a "+name);
LT_execute("worksheet -s 2 -1 2 -1;");
LT_execute("run.section(Plot, Histogram)" );

}



}




Deanna
OriginLab Technical Services
ovince Posted - 09/28/2006 : 10:44:59 AM
hello Zachary,

I am not sure I can make connection between LabTalk commands and OriginC alone. So far I have this:


void graph_hist()
{
foreach (PageBase pg in Project.Pages)
{
if( pg.GetType()==EXIST_WKS )
{

string name = pg.GetName();
out_str(name);


Worksheet wks(pg.GetName());

////////// graph ///////////////
// define curves
Curve crvAll(wks, 0, 1);

// Create a graph page
GraphPage gpg;
gpg.Create( "Origin.OTP" );

// create graph Layer
GraphLayer glyg = gpg.Layers( 0 );

// add and format All plot
int iAll = glyg.AddPlot( crvAll, IDM_PLOT_SCATTER );
DataPlot dpAll = glyg.DataPlots(iAll);
dpAll.Curve.Symbol.Shape.nVal = 2;
dpAll.Curve.Symbol.Size.nVal = 4;
dpAll.Curve.Symbol.EdgeColor.nVal = 1;

THIS PART WORKS FINE

/////// histogram /////////
//define columns for histograms
Column colHist1(name, 2);
Column colHist2(name, 3);

// Create histogram
GraphPage gph;
gph.Create();
gph.LoadTemplate("Hist");

//GraphLayer glyh = gph.Layers(1);

I HAVE LEAVED OUT ALL DIFFERENT ATTEMPT TO APPLY LABTALK COMMANDS THAT WOULD WORK OK

}
}
}

This should make two individual graphs. It is steel far from the idea that should be realized (as on the attached picture) but would be usefull step for sure.


Could you help me with this part?

Thanks a lot
oliver
zachary_origin Posted - 09/27/2006 : 10:17:51 PM
Hi Oliver,

You can plot a histogram using the GraphPage::LoadTemplate. The template for histogram is Hist. The codes will be something like:
GraphPage gp;
gp.Create();
gp.LoadTemplate("Hist");


For the Layout, Origin provides multiple layers to do this job. GraphPage::AppendLayers
can be used to append layers to a graph page.


However, sometimes it is more convenient to fulfil the task in LabTalk than in OriginC.
For example, you can use these scripts:
run.section(PlotProf, Panel4);
run.section(Plot, Histogram);

Then you can use these scripts in OC through the method:
PUBLIC BOOL LT_execute(LPCSTR lpcszScript, int wCtrl = 0).
E.g.
.....
LT_execute("run.section(PlotProf, Panel4)"); //create 4 panels
GraphLayer gl = gp.Layers(2); // Get layer 2
set_active_layer(gl); //set layer 2 as active,
LT_execute("run.section(Plot, Histogram)" );
....

So that you can merge OriginC and LabTalk together for your convenience.


zachary
OriginLab Technical Services.

Edited by - zachary_origin on 09/27/2006 10:19:32 PM

The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000