Ok just for fun, I can now share. I’ve been working on a data visualization course for a computer science department and the standard text is Edward Tufte’s book “the Visual Display of Quantitative Information” which for anyone dealing with data is an interesting and relaxed read.
This excerpt is an implementation of one of the line plots found early on in the book (p68.) and seemed like a good challenge to implement in ROOT. So I wrote a quick tutorial to describe all the elements that this plot demonstrates. The full code now looks like this:
import ROOT
from array import array
#define some data
x = list(range(1967, 1978))
y = [310, 330, 370, 385, 385, 393, 387, 380, 390, 400, 380]
n = len(x)
#make the canvas
c = ROOT.TCanvas("c","c", 1400, 700)
pad = ROOT.TPad("p","p",0,0,1,1, 0)
pad.Draw()
pad.cd()
pad.SetFrameLineColor(0);
pad.SetTopMargin(.2)
frame = pad.DrawFrame(1966.5, 290,1977.5,410);
#set the axis style!
xaxis = frame.GetXaxis();
yaxis = frame.GetYaxis();
xaxis.SetLabelFont(132);
yaxis.SetLabelFont(132);
xaxis.SetNdivisions(11);
yaxis.SetNdivisions(10);
yaxis.ChangeLabel(1,-1,-1,-1,-1,-1,"$300");
yaxis.ChangeLabel(2,-1,-1,-1,-1,-1,"$320");
yaxis.ChangeLabel(3,-1,-1,-1,-1,-1,"$340");
yaxis.ChangeLabel(4,-1,-1,-1,-1,-1,"$360");
yaxis.ChangeLabel(5,-1,-1,-1,-1,-1,"$380");
yaxis.ChangeLabel(6,-1,-1,-1,-1,-1,"$400");
# Draw a white line over the axis bar
l = ROOT.TLine()
l.SetLineColor(ROOT.kWhite)
l.DrawLine(1966.5, 290,1966.5,410)
l.DrawLine(1966.5, 290,1977.5,290)
## Now add the data.
# Making the graph first.
gr = ROOT.TGraph(n,array('d',x),array('d',y))
gr.SetMarkerStyle(20)
# adding the connecting line
gr_line = gr.Clone()
gr.Draw("L")
# adding white dots to highlight the data points
gr_white = gr.Clone()
gr_white.SetMarkerSize(2.5)
gr_white.SetMarkerColor(0)
gr_white.Draw("P")
# and the actual datapoints
gr.Draw("P")
## Now text and highlights
#dotted line first
l2 = ROOT.TLine()
l2.SetLineStyle(2)
l2.DrawLine(1970, 380, 1977, 380)
l2.DrawLine(1970, 400, 1977, 400)
#arrow between the 5% bars
ar = ROOT.TArrow(1977.5, 380 ,1977.5, 400, 0.01, "<|>")
ar.SetAngle(178)
ar.SetArrowSize(.0001)
ar.Draw()
five = ROOT.TText(1978, 387.5, "5 %")
five.SetTextFont(132)
five.SetTextSize(.04)
five.Draw()
#and the text
title_text = ROOT.TPaveText(1967, 430, 1970, 400)
title_text.SetTextAlign(11)
title_text.SetTextFont(132)
title_text.SetFillColor(0)
title_text.SetBorderSize(0)
title_text.AddText("Per capita")
title_text.AddText("budget expenditures,")
title_text.AddText("in constant dollars")
title_text.Draw()
c.Draw()
c.SaveAs("tufte_root.png")