CMS 3D CMS Logo

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