Skip to content

Commit 5606696

Browse files
committed
Updating extents to include some padding. Fixing watermark issues.
1 parent 8850b3d commit 5606696

5 files changed

Lines changed: 36 additions & 24 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ post, it can be solved by explicitly install the `matplotlib` dependency `dvipng
6464

6565
### Update History
6666

67+
##### 0.33.0
68+
* Adding extra padding to bin extents for KDE and smoothing
69+
* Updating watermarking to work with matplotlib v3.0.0+
70+
6771
##### 0.32.0
6872
* Fixing matplotlib axis formatter issue.
6973

chainconsumer/analysis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,14 @@ def get_covariance_table(self, chain=0, parameters=None, caption="Parameter Cova
314314
parameters, cov = self.get_covariance(chain=chain, parameters=parameters)
315315
return self._get_2d_latex_table(parameters, cov, caption, label)
316316

317-
def _get_smoothed_histogram(self, chain, parameter):
317+
def _get_smoothed_histogram(self, chain, parameter, pad=False):
318318
data = chain.get_data(parameter)
319319
smooth = chain.config["smooth"]
320320
if chain.grid:
321321
bins = get_grid_bins(data)
322322
else:
323323
bins = chain.config["bins"]
324-
bins, smooth = get_smoothed_bins(smooth, bins, data, chain.weights)
324+
bins, smooth = get_smoothed_bins(smooth, bins, data, chain.weights, pad=pad)
325325

326326
hist, edges = np.histogram(data, bins=bins, density=True, weights=chain.weights)
327327
if chain.power is not None:

chainconsumer/chainconsumer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ChainConsumer(object):
2020
2121
"""
2222

23-
__version__ = "0.32.0"
23+
__version__ = "0.33.0"
2424

2525
def __init__(self):
2626
logging.basicConfig(level=logging.INFO)

chainconsumer/helpers.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33

44

5-
def get_extents(data, weight, plot=False, wide_extents=True, tiny=False):
5+
def get_extents(data, weight, plot=False, wide_extents=True, tiny=False, pad=False):
66
hist, be = np.histogram(data, weights=weight, bins=2000)
77
bc = 0.5 * (be[1:] + be[:-1])
88
cdf = hist.cumsum()
@@ -17,16 +17,22 @@ def get_extents(data, weight, plot=False, wide_extents=True, tiny=False):
1717
threshold = 0.3
1818
i1 = np.where(cdf > threshold)[0][0]
1919
i2 = np.where(icdf > threshold)[0][0]
20-
return bc[i1], bc[-i2]
20+
lower = bc[i1]
21+
upper = bc[-i2]
22+
if pad:
23+
width = upper - lower
24+
lower -= 0.2 * width
25+
upper += 0.2 * width
26+
return lower, upper
2127

2228

2329
def get_bins(chains):
2430
proposal = [max(35, np.floor(1.0 * np.power(chain.chain.shape[0] / chain.chain.shape[1], 0.25))) for chain in chains]
2531
return proposal
2632

2733

28-
def get_smoothed_bins(smooth, bins, data, weight, marginalised=True, plot=False):
29-
minv, maxv = get_extents(data, weight, plot=plot)
34+
def get_smoothed_bins(smooth, bins, data, weight, marginalised=True, plot=False, pad=False):
35+
minv, maxv = get_extents(data, weight, plot=plot, pad=pad)
3036
if smooth is None or not smooth or smooth == 0:
3137
return np.linspace(minv, maxv, int(bins)), 0
3238
else:

chainconsumer/plotter.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import matplotlib.pyplot as plt
55
import matplotlib
6+
from matplotlib.font_manager import FontProperties
67
from matplotlib.ticker import MaxNLocator, ScalarFormatter, LogLocator
78
from matplotlib.textpath import TextPath
89
from numpy import meshgrid
@@ -315,31 +316,33 @@ def _add_watermark(self, fig, axes, figsize, text, dpi=300, size_scale=1.0): #
315316
dx, dy = figsize
316317
dy, dx = dy * dpi, dx * dpi
317318
rotation = 180 / np.pi * np.arctan2(-dy, dx)
318-
fontdict = self.parent.config["watermark_text_kwargs"]
319-
if "usetex" in fontdict:
320-
usetex = fontdict["usetex"]
321-
else:
322-
usetex = self.parent.config["usetex"]
323-
fontdict["usetex"] = usetex
324-
if fontdict["usetex"]:
319+
property_dict = self.parent.config["watermark_text_kwargs"]
320+
321+
keys_in_font_dict = ["family", "style", "variant", "weight", "stretch", "size"]
322+
fontdict = {k: property_dict[k] for k in keys_in_font_dict if k in property_dict}
323+
font_prop = FontProperties(**fontdict)
324+
usetex = property_dict.get("usetex", self.parent.config["usetex"])
325+
if usetex:
325326
px, py, scale = 0.5, 0.5, 1.0
326327
else:
327-
px, py, scale = 0.45, 0.55, 0.8
328-
bb0 = TextPath((0, 0), text, size=50, prop=fontdict, usetex=usetex).get_extents()
329-
bb1 = TextPath((0, 0), text, size=51, prop=fontdict, usetex=usetex).get_extents()
328+
px, py, scale = 0.5, 0.5, 0.8
329+
330+
bb0 = TextPath((0, 0), text, size=50, prop=font_prop, usetex=usetex).get_extents()
331+
bb1 = TextPath((0, 0), text, size=51, prop=font_prop, usetex=usetex).get_extents()
330332
dw = (bb1.width - bb0.width) * (dpi / 100)
331333
dh = (bb1.height - bb0.height) * (dpi / 100)
332334
size = np.sqrt(dy ** 2 + dx ** 2) / (dh * abs(dy / dx) + dw) * 0.6 * scale * size_scale
333335
if axes is not None:
334-
if fontdict["usetex"]:
336+
if usetex:
335337
size *= 0.7
336338
else:
337-
size *= 0.85
338-
fontdict["size"] = int(size)
339+
size *= 0.8
340+
size = int(size)
341+
print(f"Font size is {size}")
339342
if axes is None:
340-
fig.text(px, py, text, fontdict=fontdict, rotation=rotation)
343+
fig.text(px, py, text, fontdict=property_dict, rotation=rotation, fontsize=size)
341344
else:
342-
axes.text(px, py, text, transform=axes.transAxes, fontdict=fontdict, rotation=rotation)
345+
axes.text(px, py, text, transform=axes.transAxes, fontdict=property_dict, rotation=rotation, fontsize=size)
343346

344347
def plot_walks(
345348
self,
@@ -1215,11 +1218,10 @@ def _plot_bars(self, ax, parameter, chain, flip=False, summary=False): # pragma
12151218
kde = chain.config["kde"]
12161219
zorder = chain.config["zorder"]
12171220
title_size = self.parent.config["label_font_size"]
1218-
12191221
chain_row = chain.get_data(parameter)
12201222
weights = chain.weights
12211223
if smooth or kde:
1222-
xs, ys, _ = self.parent.analysis._get_smoothed_histogram(chain, parameter)
1224+
xs, ys, _ = self.parent.analysis._get_smoothed_histogram(chain, parameter, pad=True)
12231225
if flip:
12241226
ax.plot(ys, xs, color=colour, ls=linestyle, lw=linewidth, zorder=zorder)
12251227
else:

0 commit comments

Comments
 (0)