# Create a TGraph points with different colors

**URL:** <https://root-forum.cern.ch/t/create-a-tgraph-points-with-different-colors/50468>\
**Category:** ROOT\
**Created:** [June 18, 2022, 10:02am UTC](https://root-forum.cern.ch/t/create-a-tgraph-points-with-different-colors/50468 "2022-06-18T10:02:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javier\_Galan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/javier_galan/32/29662_2.png) [@Javier\_Galan](https://root-forum.cern.ch/u/Javier_Galan)\
**Post date:** [June 18, 2022, 10:02am UTC](https://root-forum.cern.ch/t/create-a-tgraph-points-with-different-colors/50468/1 "2022-06-18T10:02:00Z")

</div>

Hi @couet

I have seen the following solution to create a TGraph where each point has a different color.

> [@Make each point different colors](https://root-forum.cern.ch/t/make-each-point-different-colors/25072):
>
> Hello, I have a script which display graphs and I want to put a different color for each points. I know that with SetMarkerColor you can select the color for all points on the graph but I don’t know how can I put a different color for each points. Actually, my script display a canvas of 3 graphs and in each graph it is 3 points, I want to put 3 different colors for the 3 points on each graph. One other thing, I want to put a grid in those graphs, I put the punction SetGrid but it don’t worked.…

However, in that example, the color of each marker/point depends on the order of the point.

I wonder how the macro needs to be modified so that I can pass a property, such as the energy of the particle, and then implement the logic at `DrawCol( Double_t energy)` to define the color that corresponds to each energy range.

I imagine the line

```auto
TExec *ex = new TExec("ex","DrawCol();")

```

would need to be updated?

On top of that, I would like to implement those methods inside a class, lets say `A`. I could extrapolate this example easily?

I.e., at the implementation of `A::graphmulticol` I could simply update `TExec` line to the following?

```auto
   TExec *ex = new TExec("ex","A::DrawCol();");

```

Perhaps I need to make the method static?

On the other hand, would not be handy that the `TGraph` would have a method that allows to change each point color?

```auto
TGraph::SetPointColor( Int_t n, Int_t color); // Sets point "n" to color "color"

```

ThankYou!

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 20, 2022, 8:52am UTC](https://root-forum.cern.ch/t/create-a-tgraph-points-with-different-colors/50468/2 "2022-06-20T08:52:07Z")

</div>

This macro is just a demonstration of principle. You will need to map a value onto a colormap. Putting it in a class you will not need `TExec`. I am looking at implementing a `TScatter` class inheriting from `TGraph` and doing that.

---

<div class="post-metadata">

**Author:** ![Javier\_Galan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/javier_galan/32/29662_2.png) [@Javier\_Galan](https://root-forum.cern.ch/u/Javier_Galan)\
**Post date:** [June 20, 2022, 9:56am UTC](https://root-forum.cern.ch/t/create-a-tgraph-points-with-different-colors/50468/3 "2022-06-20T09:56:03Z")

</div>

Perhaps naming it `TScatterGraph` would make it clear it inherits from `TGraph`.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [July 4, 2022, 9:56am UTC](https://root-forum.cern.ch/t/create-a-tgraph-points-with-different-colors/50468/4 "2022-07-04T09:56:57Z")

</div>

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