Is it an easy way to draw error bar on bar histogram?
Thanks,
Michal
Is it an easy way to draw error bar on bar histogram?
Thanks,
Michal
For example:
h.Draw(“bar2 e”);
Rene
Thank you, Rene,
I still have minor problems. How to center error bars and make the error
bar the same color as bar? The perimeter of the frame is yellow as well
as extension of X axis.
Below is a macro.
Michal
void TestBarHistBB()
{
// Bar histogram
Int_t ColorTable[] = {5, 2, 3, 4, 6, 7, 9, 8, 4, 7, 28, 30, 46, 49, 50};
const Int_t BackgroundColor = 18;
const Int_t CanvasFillStyle = 0;
gStyle -> SetOptStat(0);
gStyle -> SetTitleBorderSize(0);
gStyle -> SetTitleX(0.25);
gStyle -> SetTitleY(0.98);
gStyle -> SetFrameLineColor(BackgroundColor);
gStyle -> SetFrameFillStyle(CanvasFillStyle);
gStyle -> SetFrameBorderMode(0);
gStyle -> SetCanvasColor(BackgroundColor);
TCanvas *CC = new TCanvas(“TestCanvas”, “TestCanvas”, 800, 600);
Int_t NX = 10, NY = 10;
Int_t NHists = 4;
TH1F *HH1[4];
TString HTitle = “Bar Histogram “;
for (Int_t il = 0; il < NHists; il++) {
ostringstream is;
is.str(””);
is << il;
HTitle += is.str();
HH1[il] = new TH1F(“Bar Histogram 0”, “”, 10, 0, 10);
}
HH1[0] -> SetBinContent(2, 24.9); // SD 4.11 4
HH1[1] -> SetBinContent(4, 5.19); // SD 1.13 6
HH1[2] -> SetBinContent(6, 1.5); // SD 0.23 6
HH1[3] -> SetBinContent(8, 0.3); // SD 0.63 6
HH1[0] -> SetBinError(2, 4.11); // SD 4.11 4
HH1[1] -> SetBinError(4, 1.13); // SD 1.13 6
HH1[2] -> SetBinError(6, 0.23); // SD 0.23 6
HH1[3] -> SetBinError(8, 0.063); // SD 0.063 6
Int_t ColorIndx = 0;
Double_t Max = 30.0;
Double_t BarWidth = 2.00;
for (Int_t il = 0; il < NHists; il++) {
HH1[il] -> SetMaximum(Max);
HH1[il] -> GetXaxis() -> SetNdivisions(0);
HH1[il] -> GetXaxis() -> SetAxisColor(1);
HH1[il] -> GetYaxis() -> SetNdivisions(110);
HH1[il] -> SetFillColor(ColorTable[ColorIndx]);
HH1[il] -> SetBarWidth(BarWidth);
ColorIndx++;
}
HH1[3] -> Draw(“bar”);
HH1[3] -> Draw(“bar2 e”);
HH1[2] -> Draw(“bar, same”);
HH1[1] -> Draw(“bar, same”);
HH1[0] -> Draw(“bar, same”);
HH1[0] -> Draw(“same axis”);
CC -> Update();
char OutPS[40] = “TestBarHistBB.gif”;
cout << " OutPS " << OutPS << endl;
CC -> SaveAs(OutPS);
}
I am not convinced that is is a good idea to make the errors the same colors as the bars. Anyhow, below, you have what you request
Rene
[code]#include “TCanvas.h”
#include “TStyle.h”
#include “TH1.h”
#include “Riostream.h”
void junk()
{
// Bar histogram
Int_t ColorTable[] = {5, 2, 3, 4, 6, 7, 9, 8, 4, 7, 28, 30, 46, 49, 50};
const Int_t BackgroundColor = 18;
const Int_t CanvasFillStyle = 0;
gStyle -> SetOptStat(0);
gStyle -> SetTitleBorderSize(0);
gStyle -> SetTitleX(0.25);
gStyle -> SetTitleY(0.9);
gStyle -> SetFrameLineColor(BackgroundColor);
gStyle -> SetFrameFillStyle(CanvasFillStyle);
gStyle -> SetFrameBorderMode(0);
gStyle -> SetCanvasColor(BackgroundColor);
TCanvas *CC = new TCanvas(“TestCanvas”, “TestCanvas”, 800, 600);
Int_t NX = 10, NY = 10;
Int_t NHists = 4;
TH1F *HH1[4];
TString HTitle = “Bar Histogram “;
for (Int_t il = 0; il < NHists; il++) {
ostringstream is;
is.str(””);
is << il;
HTitle += is.str();
HH1[il] = new TH1F(“Bar Histogram 0”, “”, 10, 0, 10);
}
HH1[0] -> SetBinContent(2, 24.9); // SD 4.11 4
HH1[1] -> SetBinContent(4, 5.19); // SD 1.13 6
HH1[2] -> SetBinContent(6, 1.5); // SD 0.23 6
HH1[3] -> SetBinContent(8, 0.3); // SD 0.63 6
HH1[0] -> SetBinError(2, 4.11); // SD 4.11 4
HH1[1] -> SetBinError(4, 1.13); // SD 1.13 6
HH1[2] -> SetBinError(6, 0.23); // SD 0.23 6
HH1[3] -> SetBinError(8, 0.063); // SD 0.063 6
Int_t ColorIndx = 0;
Double_t Max = 30.0;
Double_t BarWidth = 2.00;
for (Int_t il = 0; il < NHists; il++) {
HH1[il] -> SetMaximum(Max);
HH1[il] -> GetXaxis() -> SetNdivisions(0);
HH1[il] -> GetXaxis() -> SetAxisColor(1);
HH1[il] -> GetYaxis() -> SetNdivisions(110);
HH1[il] -> SetFillColor(ColorTable[ColorIndx]);
HH1[il] -> SetLineColor(ColorTable[ColorIndx]);
HH1[il] -> SetLineWidth(5);
HH1[il] -> SetBarWidth(BarWidth);
HH1[il] -> SetBarOffset(-0.5);
ColorIndx++;
}
HH1[3] -> Draw(“bar”);
HH1[3] -> Draw(“bar2 e”);
HH1[2] -> Draw(“bar, same”);
HH1[1] -> Draw(“bar, same”);
HH1[0] -> Draw(“bar, same”);
HH1[0] -> Draw(“same axis”);
CC -> Update();
char OutPS[40] = “TestBarHistBB.gif”;
cout << " OutPS " << OutPS << endl;
CC -> SaveAs(OutPS);
} [/code]