CMS 3D CMS Logo

public_plots_tools.py
Go to the documentation of this file.
1 from __future__ import print_function
2 ######################################################################
3 ## File: public_plots_tools.py
4 ######################################################################
5 
6 import os
7 import math
8 from colorsys import hls_to_rgb, rgb_to_hls
9 
10 import matplotlib
11 from matplotlib.font_manager import FontProperties
12 from matplotlib._png import read_png
13 from matplotlib.offsetbox import OffsetImage
14 from matplotlib.offsetbox import AnnotationBbox
15 
16 import numpy as np
17 
18 ######################################################################
19 
20 FONT_PROPS_SUPTITLE = FontProperties(size="x-large", weight="bold", stretch="condensed")
21 FONT_PROPS_TITLE = FontProperties(size="large", weight="regular")
22 FONT_PROPS_AX_TITLE = FontProperties(size="x-large", weight="bold")
23 FONT_PROPS_TICK_LABEL = FontProperties(size="large", weight="bold")
24 
25 ######################################################################
26 
28  """Just some Matplotlib settings."""
29  matplotlib.rcParams["text.usetex"] = False
30  matplotlib.rcParams["legend.numpoints"] = 1
31  matplotlib.rcParams["figure.figsize"] = (8., 6.)
32  matplotlib.rcParams["figure.dpi"] = 300
33  matplotlib.rcParams["savefig.dpi"] = matplotlib.rcParams["figure.dpi"]
34  matplotlib.rcParams["font.size"] = 10.8
35  matplotlib.rcParams["pdf.fonttype"] = 42
36  # End of InitMatplotlib().
37 
38 ######################################################################
39 
40 def AddLogo(logo_name, ax, zoom=1.2):
41  """Read logo from PNG file and add it to axes."""
42 
43  logo_data = read_png(logo_name)
44  fig_dpi = ax.get_figure().dpi
45  fig_size = ax.get_figure().get_size_inches()
46  # NOTE: This scaling is kinda ad hoc...
47  zoom_factor = .1 / 1.2 * fig_dpi * fig_size[0] / np.shape(logo_data)[0]
48  zoom_factor *= zoom
49  logo_box = OffsetImage(logo_data, zoom=zoom_factor)
50  ann_box = AnnotationBbox(logo_box, [0., 1.],
51  xybox=(2., -3.),
52  xycoords="axes fraction",
53  boxcoords="offset points",
54  box_alignment=(0., 1.),
55  pad=0., frameon=False)
56  ax.add_artist(ann_box)
57  # End of AddLogo().
58 
59 ######################################################################
60 
62 
63  res = None
64  if val < 0.:
65  res = math.floor(val)
66  else:
67  res = math.ceil(val)
68 
69  # End of RoundAwayFromZero().
70  return res
71 
72 ######################################################################
73 
74 def LatexifyUnits(units_in):
75 
76  latex_units = {
77  "b^{-1}" : "$\mathbf{b}^{-1}$",
78  "mb^{-1}" : "$\mathbf{mb}^{-1}$",
79  "ub^{-1}" : "$\mu\mathbf{b}^{-1}$",
80  "nb^{-1}" : "$\mathbf{nb}^{-1}$",
81  "pb^{-1}" : "$\mathbf{pb}^{-1}$",
82  "fb^{-1}" : "$\mathbf{fb}^{-1}$",
83  "Hz/b" : "$\mathbf{Hz/b}$",
84  "Hz/mb" : "$\mathbf{Hz/mb}$",
85  "Hz/ub" : "$\mathbf{Hz/}\mathbf{\mu}\mathbf{b}$",
86  "Hz/nb" : "$\mathbf{Hz/nb}$",
87  "Hz/pb" : "$\mathbf{Hz/pb}$",
88  "Hz/fb" : "$\mathbf{Hz/fb}$"
89  }
90 
91  res = latex_units[units_in]
92 
93  # End of LatexifyUnits().
94  return res
95 
96 ######################################################################
97 
98 def DarkenColor(color_in):
99  """Takes a tuple (r, g, b) as input."""
100 
101  color_tmp = matplotlib.colors.colorConverter.to_rgb(color_in)
102 
103  tmp = rgb_to_hls(*color_tmp)
104  color_out = hls_to_rgb(tmp[0], .7 * tmp[1], tmp[2])
105 
106  # End of DarkenColor().
107  return color_out
108 
109 ######################################################################
110 
112  """A bit of a cludge, but a simple way to store color choices."""
113 
114  @classmethod
115  def InitColors(cls):
116 
117  #------------------------------
118  # For color scheme 'Greg'.
119  #------------------------------
120 
121  # This is the light blue of the CMS logo.
122  ColorScheme.cms_blue = (0./255., 152./255., 212./255.)
123 
124  # This is the orange from the CMS logo.
125  ColorScheme.cms_orange = (241./255., 194./255., 40./255.)
126 
127  # Slightly darker versions of the above colors for the lines.
128  ColorScheme.cms_blue_dark = (102./255., 153./255., 204./255.)
129  ColorScheme.cms_orange_dark = (255./255., 153./255., 0./255.)
130 
131  #------------------------------
132  # For color scheme 'Joe'.
133  #------------------------------
134 
135  # Several colors from the alternative CMS logo, with their
136  # darker line variants.
137 
138  ColorScheme.cms_red = (208./255., 0./255., 37./255.)
139  ColorScheme.cms_yellow = (255./255., 248./255., 0./255.)
140  ColorScheme.cms_purple = (125./255., 16./255., 123./255.)
141  ColorScheme.cms_green = (60./255., 177./255., 110./255.)
142  ColorScheme.cms_orange2 = (227./255., 136./255., 36./255.)
143  ColorScheme.cms_lightyellow = (255./255., 235./255., 215./255.)
144 
145  # End of InitColors().
146 
147  def __init__(self, name):
148 
149  self.name = name
150 
151  # Some defaults.
152  self.color_fill_del = "black"
153  self.color_fill_rec = "white"
154  self.color_fill_cert = "red"
155  self.color_fill_peak = "black"
160  self.color_by_year = {
161  2010 : "green",
162  2011 : "red",
163  2012 : "blue"
164  }
165  self.color_line_pileup = "black"
166  self.color_fill_pileup = "blue"
167  self.logo_name = "cms_logo.png"
168  self.file_suffix = "_%s" % self.name.lower()
169 
170  tmp_name = self.name.lower()
171  if tmp_name == "greg":
172  # Color scheme 'Greg'.
173  self.color_fill_del = ColorScheme.cms_blue
174  self.color_fill_rec = ColorScheme.cms_orange
175  self.color_fill_cert = ColorScheme.cms_lightyellow
176  self.color_fill_peak = ColorScheme.cms_orange
181  self.color_line_pileup = "black"
182  self.color_fill_pileup = ColorScheme.cms_blue
183  self.logo_name = "cms_logo.png"
184  self.file_suffix = ""
185  elif tmp_name == "joe":
186  # Color scheme 'Joe'.
187  self.color_fill_del = ColorScheme.cms_yellow
188  self.color_fill_rec = ColorScheme.cms_red
189  self.color_fill_cert = ColorScheme.cms_orange
190  self.color_fill_peak = ColorScheme.cms_red
195  self.color_line_pileup = "black"
196  self.color_fill_pileup = ColorScheme.cms_yellow
197  self.logo_name = "cms_logo_alt.png"
198  self.file_suffix = "_alt"
199  else:
200  print("ERROR Unknown color scheme '%s'" % self.name, file=sys.stderr)
201  sys.exit(1)
202 
203  # Find the full path to the logo PNG file.
204  # NOTE: This is a little fragile, I think.
205  logo_path = os.path.realpath(os.path.dirname(__file__))
206  self.logo_name = os.path.join(logo_path,
207  "../plotdata/%s" % self.logo_name)
208 
209  # End of __init__().
210 
211  # End of class ColorScheme.
212 
213 ######################################################################
214 
215 def SavePlot(fig, file_name_base):
216  """Little helper to save plots in various formats."""
217 
218  # DEBUG DEBUG DEBUG
219  # Check some assumptions.
220  assert len(fig.axes) == 2
221  assert len(fig.axes[0].artists) == 1
222  assert file_name_base.find(".") < 0
223  # DEBUG DEBUG DEBUG end
224 
225  # First save as PNG.
226  fig.savefig("%s.png" % file_name_base)
227 
228  # Then rescale and reposition the logo (which is assumed to be the
229  # only artist in the first pair of axes) and save as PDF.
230  tmp_annbox = fig.axes[0].artists[0]
231  tmp_offsetbox = tmp_annbox.offsetbox
232  fig_dpi = fig.dpi
233  tmp_offsetbox.set_zoom(tmp_offsetbox.get_zoom() * 72. / fig_dpi)
234  tmp = tmp_annbox.xytext
235  tmp_annbox.xytext = (tmp[0] + 1., tmp[1] - 1.)
236  fig.savefig("%s.pdf" % file_name_base, dpi=600)
237 
238  # End of SavePlot().
239 
240 ######################################################################
def DarkenColor(color_in)
def LatexifyUnits(units_in)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def AddLogo(logo_name, ax, zoom=1.2)
def SavePlot(fig, file_name_base)