# How to plot two variables from one root file

**URL:** <https://root-forum.cern.ch/t/how-to-plot-two-variables-from-one-root-file/43242>\
**Category:** Newbie\
**Created:** [January 24, 2021, 7:02pm UTC](https://root-forum.cern.ch/t/how-to-plot-two-variables-from-one-root-file/43242 "2021-01-24T19:02:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![abaf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@abaf](https://root-forum.cern.ch/u/abaf)\
**Post date:** [January 24, 2021, 7:02pm UTC](https://root-forum.cern.ch/t/how-to-plot-two-variables-from-one-root-file/43242/1 "2021-01-24T19:02:22Z")

</div>

Hi,

I have a root file from which I have to plot two variables against each other, for example, velocity on y axis and momentum on the x axis.

How would I proceed with this?

Any help would be greatly appreciated!

Thank you.

---

<div class="post-metadata">

**Author:** ![ROOTer1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/977dab/32.png) [@ROOTer1](https://root-forum.cern.ch/u/ROOTer1)\
**Post date:** [January 24, 2021, 9:20pm UTC](https://root-forum.cern.ch/t/how-to-plot-two-variables-from-one-root-file/43242/2 "2021-01-24T21:20:07Z")

</div>

Hi,

What type of data do you have?

> **[ROOT: TGraph Class Reference](https://root.cern.ch/doc/master/classTGraph.html)**

  
In general, a TGraph is useful to plot two arrays of values (in your case here the velocity and the momentum, see first example of the link)

---

<div class="post-metadata">

**Author:** ![abaf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@abaf](https://root-forum.cern.ch/u/abaf)\
**Post date:** [January 24, 2021, 9:27pm UTC](https://root-forum.cern.ch/t/how-to-plot-two-variables-from-one-root-file/43242/3 "2021-01-24T21:27:07Z")

</div>

Hi,

Thank you for your reply!

The data file in question contains kinematic variables produced at the generator level. (px,py,pz,en,ma).

I want to draw beta(v/c) on the y axis and momentum on the x axis, however, I can’t seem to change the y axis to beta as it only seems to plot the number of events.

Should I still process with the link you kindly provided?

Thank you!

---

<div class="post-metadata">

**Author:** ![ROOTer1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/977dab/32.png) [@ROOTer1](https://root-forum.cern.ch/u/ROOTer1)\
**Post date:** [January 24, 2021, 10:32pm UTC](https://root-forum.cern.ch/t/how-to-plot-two-variables-from-one-root-file/43242/4 "2021-01-24T22:32:47Z")

</div>

If your data are stored in a tree, I would suggest TChain [https://root.cern.ch/doc/master/classTChain.html](https://root.cern.ch/doc/master/classTChain.html) to read the values of your variables per event, then you can use TGraph to plot

---

<div class="post-metadata">

**Author:** ![renqipan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/renqipan/32/22738_2.png) [@renqipan](https://root-forum.cern.ch/u/renqipan)\
**Post date:** [January 25, 2021, 2:25am UTC](https://root-forum.cern.ch/t/how-to-plot-two-variables-from-one-root-file/43242/5 "2021-01-25T02:25:58Z")

</div>

Hi try like tree-\>Draw(“y:x”), where tree is your own tree name.

---

<div class="post-metadata">

**Author:** ![abaf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@abaf](https://root-forum.cern.ch/u/abaf)\
**Post date:** [January 25, 2021, 11:17am UTC](https://root-forum.cern.ch/t/how-to-plot-two-variables-from-one-root-file/43242/7 "2021-01-25T11:17:27Z")

</div>

Hi,

Thank you @ROOTer1 and @renqipan your help!

I learned two ways to solve my problem !

Kind regards
