How to draw a 3D polygon


Please read tips for efficient and successful posting and posting code

Please fill also the fields below. Note that root -b -q will tell you this info, and starting from 6.28/06 upwards, you can call .forum bug from the ROOT prompt to pre-populate a topic.

ROOT Version: 6.30/04
Platform: windows11
Compiler: MSVC 19.29.30154.0


I now want to draw a 3D elevation data and overlay some polygons. I have now implemented the drawing of elevation data through H2F, but now there are two problems:

  1. How do I customize the color palette and apply it to the Z value.
  2. How do I draw polygonal data in a chart?
    My code is like this:
float* pBuf = new float[nXSize * nYSize];
   TH2F* dt = new TH2F("h2", "h2", nXSize, 100, 110, nYSize, 25, 30);
   dt->SetTitle("Graph title; X axis title; Y axis title; Z axis title");
   Int_t N = 0;
   for (size_t i = 0; i < nYSize; i++)
   {
       y = i;
       for (size_t j= 0; j < nXSize; j++)
       {
           x = j;
           Int_t bin = dt->GetBin(j + 1, i + 1);
           z = pBuf[i * nXSize + j];
           dt->SetBinContent(bin, z/1000.0);
       }
   }
   dt->SetMinimum(0);
   dt->SetMaximum(100);
   Double_t Red[3] = { 1.00, 0.00, 0.00 };
   Double_t Green[3] = { 0.00, 1.00, 0.00 };
   Double_t Blue[3] = { 1.00, 0.00, 1.00 };
   Double_t Length[3] = { 0.00, 0.50, 1.00 };
   Int_t nb = 50;
   TColor::CreateGradientColorTable(3, Length, Red, Green, Blue, nb);
   dt->SetContour(nb);
   gStyle->SetPalette(1);
   dt->Draw("surf4");

Hope to get your help

For 1, you also have to pass your colours and contour edges to SetContour and SetPalette; you can search for examples in this forum, e.g. Search results for 'user defined palette' - ROOT Forum
like this one: Set User defined Palette of 2D histogram - #4 by OSchaile
Not sure what you mean in the second question; what kind of “polygonal data”? and what kind of “chart”?

A 3D polygon or rectangle like this

Maybe a TGraph2D can draw such polygons.

Yes I also thought of TGraph2D and did a test with the code below, but I know how to style both H2F and TGraph2D at the same time.

TH2F* dt = new TH2F("h2", "h2", nXSize, 100, 110, nYSize, 25, 30);
   dt->SetTitle("Graph title; X axis title; Y axis title; Z axis title");
   Int_t N = 0;
   for (size_t i = 0; i < nYSize; i++)
   {
       y = i;
       for (size_t j= 0; j < nXSize; j++)
       {
           x = j;
           Int_t bin = dt->GetBin(j + 1, i + 1);
           z = pBuf[i * nXSize + j];
           dt->SetBinContent(bin, z/1000.0);
       }
   }
  
   dt->SetMinimum(0);
   dt->SetMaximum(30);
   Double_t Red[3] = { 1.00, 0.00, 0.00 };
   Double_t Green[3] = { 0.00, 1.00, 0.00 };
   Double_t Blue[3] = { 1.00, 0.00, 1.00 };
   Double_t Length[3] = { 0.00, 0.50, 1.00 };
   Int_t nb = 50;
   Int_t MyPalette[50];
   Int_t FI = TColor::CreateGradientColorTable(3, Length, Red, Green, Blue, nb);
   for (int i = 0; i < nb; i++) MyPalette[i] = FI + i;
   Double_t clevels[50];
   clevels[0] = 0;
   clevels[1] = 0.4;
   MyPalette[0] = kWhite;
   Double_t dz = (dt->GetMaximum() - 0.4) / (Double_t)(50 - 2);
   for (Int_t k = 2; k < 50; k++) {
       clevels[k] = 0.4 + (k - 1) * dz;
   }
   dt->SetContour(50, clevels);
   gStyle->SetPalette(50,MyPalette);
   dt->Draw("surf2");


   auto g2 = new TGraph2D();
   g2->SetPoint(0, 105, 26, 0);
   g2->SetPoint(1, 106, 26, 0);
   g2->SetPoint(2, 105, 27, 20);
   g2->SetPoint(3, 106, 27, 20);
   g2->SetPoint(4, 105, 26, 3);
   g2->Draw("surf2 same TRI2");


They seem to be using the same color palette now,But I want to set two separately。

I can’t run your code; it’s missing the definition of many variables (most importantly, pBuf, apart from nXSize, nYSize, etc). Anyway, one thing to try is the option “TRI” instead of “TRI2” for the graph (and setting the fill colour to whatever you want), e.g.

  g2->SetFillColor(7);
  // ...
  g2->Draw("same TRI");

(remove surf2 too)

The color palette is indeed different, but SetFillColor doesn’t seem to work. The picture I got is like this


I wanted a rectangle, but now it’s two triangles. And there is no fill color and border color set.

// g2->Draw("same TRI");
 g2->Draw("same LINE");

This can achieve a rectangle, but SetFillColor is still useless

Hi,

You are using TWebCanvas and it missing support of plain “TRI” draw option for TGraph2D.
I will apply fix soon in v6-30-00-patches branch. Then it will work for you.

Regards,
Sergey

Thank you very much, I hope to try it soon. I hope you’ll let me know when you’re done.

Another problem, the pdf I saved is not consistent with what I display
Below is what is rendered in Qt:


but Pdf

I saved it directly using fCanvas->SaveAs(“D:\ttt.pdf”);. fCanvas is an object of TCanvas. Did I miss something?

Saved PDF file produced with normal ROOT graphics.
Main difference with web - so-called “orthographic” projection used by normal graphics.
And all text labels are made differently.

If you like it - “orthographic” projection can be enabled in the web canvas as well (via context menu).

And with your special use-case of QtWeb - I need to provide method to store files produced in embed canvas. For the moment it only works with normal web browsers. I will check how it can be done.

The Y-axis range and color are both incorrect.

This set min/max values for TGraph2D drawing.
I see that it corresponds that you see in the qtweb canvas.

To produce PNG/SVG file you should be able to use canvas context menu.
For me it creates “canvas.png” and “canvas.svg” images in the “Download” directory.
Production of PDF files from web browser is now available only in ROOT master branch.

And I finally merge “TRI” draw option fix into 6.30 branch

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.