Skip to content

Commit 7e77982

Browse files
committed
[python] Provide better examples of graphics in a Python script
Show two relevant use cases: - The user wants to plot static objects to a canvas and the canvas should survive and be displayed to screen. - The user wants to fill a graphical object and display updates live on a canvas, then make it survive before the function ends.
1 parent 2a0a8e5 commit 7e77982

1 file changed

Lines changed: 77 additions & 41 deletions

File tree

  • bindings/pyroot/pythonizations/python/ROOT/_pythonization

bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tcanvas.py

Lines changed: 77 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,106 @@
88
# For the list of contributors see $ROOTSYS/README/CREDITS. #
99
################################################################################
1010

11-
r'''
11+
r"""
1212
\pythondoc TCanvas
1313
14-
Functionality of TCanvas::Update() method was extended to support interactive
15-
graphics in the python scripts. If extra block parameter is True, script execution
16-
will be suspended until <space> key pressed by user. Simple example:
14+
The TCanvas class is used to create the canvas on which graphical objects such as histograms can be drawn. The following
15+
is a simple example of typical usage:
1716
1817
\code{.py}
1918
import ROOT
19+
import numpy
2020
21-
c = ROOT.TCanvas()
22-
h = ROOT.TH1I("h1", "h1", 100, -5, 5)
23-
h.FillRandom("gaus", 10000)
24-
h.Draw("")
2521
26-
# block here until space is pressed
27-
c.Update(True)
22+
def plot():
23+
h = ROOT.TH1D("h", "h", 100, -5, 5)
24+
h.Fill(numpy.random.normal(size=1000))
2825
29-
# continues after <space> is pressed
30-
c.SaveAs("canvas.root")
26+
c = ROOT.TCanvas()
27+
h.Draw()
28+
c.Draw(block=True)
29+
30+
31+
if __name__ == "__main__":
32+
print("Before plot function")
33+
plot()
34+
print("After plot function")
35+
\endcode
36+
37+
Note the optional argument `block` passed to the `Draw` method of the canvas. If set to `True`, it will block the script
38+
execution and run the ROOT graphics event loop until the <space> key is pressed. This allows interacting with the
39+
canvas and its content until necessary, then move on with the rest of the script.
40+
41+
Another relevant use case is drawing live updates on a canvas, shown in the example below. In this case, the canvas is
42+
setup by first creating the object to be drawn and drawing it once. Then, the object is updated in a for loop, which
43+
could represent for example an incoming stream of data with which the histogram should be filled. Each time the plot
44+
should be updated, the `ModifiedUpdate` function should be called. This will immediately show the new contents on the
45+
plot. Finally, the canvas is drawn again with `Draw(block=True)` at the end of the loop so that it stays visible and can
46+
be interacted with.
47+
48+
\code{.py}
49+
import ROOT
50+
import numpy
51+
52+
53+
def live_update():
54+
c = ROOT.TCanvas()
55+
h = ROOT.TH1D("h", "h", 100, -5, 5)
56+
57+
h.Draw()
58+
for _ in range(100):
59+
h.Fill(numpy.random.normal(size=10))
60+
c.ModifiedUpdate()
61+
c.Draw(block=True)
62+
63+
if __name__ == "__main__":
64+
print("Before plot function")
65+
live_update()
66+
print("After plot function")
3167
\endcode
68+
3269
\endpythondoc
33-
'''
70+
"""
3471

3572
from . import _run_root_event_loop, pythonization
3673

3774

38-
def _TCanvas_Update(self, block = False):
39-
"""
40-
Updates the canvas.
41-
Also blocks script execution and runs the ROOT graphics event loop until the <space> keyword is pressed,
42-
but only if the following conditions are met:
43-
* The `block` optional argument is set to `True`.
44-
* ROOT graphics are enabled, i.e. `ROOT.gROOT.IsBatch() == False`.
45-
* The script is running not in ipython notebooks.
46-
"""
75+
def _TCanvas_Update(self, block=False):
76+
"""
77+
Updates the canvas.
78+
Also blocks script execution and runs the ROOT graphics event loop until the <space> key is pressed,
79+
but only if the following conditions are met:
80+
* The `block` optional argument is set to `True`.
81+
* ROOT graphics are enabled, i.e. `ROOT.gROOT.IsBatch() == False`.
82+
* The script is not running in ipython notebooks.
83+
"""
4784

48-
self._Update()
85+
self._Update()
4986

50-
# run loop if block flag is set
51-
if block:
52-
_run_root_event_loop()
87+
# run loop if block flag is set
88+
if block:
89+
_run_root_event_loop()
5390

5491

5592
def _TCanvas_Draw(self, option: str = "", block: bool = False):
56-
"""
57-
Draw the canvas.
58-
Also blocks script execution and runs the ROOT graphics event loop until the <space> keyword is pressed,
59-
but only if the following conditions are met:
60-
* The `block` optional argument is set to `True`.
61-
* ROOT graphics are enabled, i.e. `ROOT.gROOT.IsBatch() == False`.
62-
* The script is running not in ipython notebooks.
63-
"""
93+
"""
94+
Draw the canvas.
95+
Also blocks script execution and runs the ROOT graphics event loop until the <space> is pressed,
96+
but only if the following conditions are met:
97+
* The `block` optional argument is set to `True`.
98+
* ROOT graphics are enabled, i.e. `ROOT.gROOT.IsBatch() == False`.
99+
* The script is not running in ipython notebooks.
100+
"""
64101

65-
self._Draw(option)
102+
self._Draw(option)
66103

67-
# run loop if block flag is set
68-
if block:
69-
self._Update()
70-
_run_root_event_loop()
104+
# run loop if block flag is set
105+
if block:
106+
self._Update()
107+
_run_root_event_loop()
71108

72109

73-
@pythonization('TCanvas')
110+
@pythonization("TCanvas")
74111
def pythonize_tcanvas(klass):
75112
# Parameters:
76113
# klass: class to be pythonized
@@ -79,4 +116,3 @@ def pythonize_tcanvas(klass):
79116
klass._Draw = klass.Draw
80117
klass.Update = _TCanvas_Update
81118
klass.Draw = _TCanvas_Draw
82-

0 commit comments

Comments
 (0)