Skip to content

Commit

Permalink
custom scalarbar label format
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Aug 24, 2022
1 parent a276921 commit a94b3c1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
1 change: 1 addition & 0 deletions examples/volumetric/numpy2volume2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

vol = Volume(data_matrix, c=['white','b','g','r'], mapper='gpu')
vol.addScalarBar3D()
vol.print()

# optionally mask some parts of the volume (needs mapper='gpu'):
# data_mask = np.zeros_like(data_matrix)
Expand Down
14 changes: 9 additions & 5 deletions vedo/addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def ScalarBar(
c='k',
horizontal=False,
useAlpha=True,
tformat='%-#6.3g',
labelFormat=':6.3g',
):
"""
A 2D scalar bar for the specified obj.
Expand Down Expand Up @@ -639,8 +639,11 @@ def ScalarBar(

c = getColor(c)
sb = vtk.vtkScalarBarActor()

# print(sb.GetLabelFormat())
sb.SetLabelFormat(tformat)
labelFormat = labelFormat.replace(":","%-#")
sb.SetLabelFormat(labelFormat)

sb.SetLookupTable(lut)
sb.SetUseOpacity(useAlpha)
sb.SetDrawFrame(0)
Expand Down Expand Up @@ -724,6 +727,7 @@ def ScalarBar3D(
labelSize=1,
labelOffset=0.375,
labelRotation=0,
labelFormat="",
italic=0,
c=None,
drawBox=True,
Expand Down Expand Up @@ -848,11 +852,11 @@ def ScalarBar3D(
lut10.DeepCopy(lut)
lut10.SetScaleToLinear()
scale.cmap(lut10, cscals, on="cells")
ticks_pos, ticks_txt = utils.makeTicks(vmin, vmax, nlabels, logscale=True)
tk = utils.makeTicks(vmin, vmax, nlabels, logscale=True, useformat=labelFormat)
else:
scale.cmap(lut, cscals, on="cells")
ticks_pos, ticks_txt = utils.makeTicks(vmin, vmax, nlabels, logscale=False)

tk = utils.makeTicks(vmin, vmax, nlabels, logscale=False, useformat=labelFormat)
ticks_pos, ticks_txt = tk
scale.lw(0).wireframe(False).lighting("off")

scales = [scale]
Expand Down
10 changes: 7 additions & 3 deletions vedo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ def addScalarBar(
c=None,
horizontal=False,
useAlpha=True,
tformat='%-#6.3g',
labelFormat=':6.3g',
):
"""
Add a 2D scalar bar for the specified obj.
Expand Down Expand Up @@ -1314,9 +1314,8 @@ def addScalarBar(
c,
horizontal,
useAlpha,
tformat,
labelFormat,
)

self.scalarbar = sb
return self

Expand All @@ -1335,6 +1334,7 @@ def addScalarBar3D(
labelSize=1,
labelOffset=0.375,
labelRotation=0,
labelFormat="",
italic=0,
c=None,
drawBox=True,
Expand Down Expand Up @@ -1382,6 +1382,9 @@ def addScalarBar3D(
labelRotation : float
label rotation in degrees
labelFormat : str
label format for floats and integers (e.g. ':.2f')
drawBox : bool
draw a box around the colorbar
Expand Down Expand Up @@ -1415,6 +1418,7 @@ def addScalarBar3D(
labelSize,
labelOffset,
labelRotation,
labelFormat,
italic,
c,
drawBox,
Expand Down
25 changes: 16 additions & 9 deletions vedo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,9 +821,7 @@ def isNumber(n):
def roundToDigit(x, p):
"""Round a real number to the specified number of significant digits."""
if not x:
return x
# k = int(np.floor(np.log10(abs(x)))) + (p - 1)
# r = np.around(x, -k)
return 0
r = np.round(x, p - int(np.floor(np.log10(abs(x)))) - 1)
if int(r) == r:
return int(r)
Expand Down Expand Up @@ -877,6 +875,7 @@ def precision(x, p, vrange=None, delimiter="e"):
# Based on the webkit javascript implementation
# `from here <https://code.google.com/p/webkit-mirror/source/browse/JavaScriptCore/kjs/number_object.cpp>`_,
# and implemented by `randlet <https://github.com/randlet/to-precision>`_.
# Modified for vedo by M.Musy 2020

if isinstance(x, str): # do nothing
return x
Expand Down Expand Up @@ -1735,8 +1734,10 @@ def vtkCameraToK3D(vtkcam):
return np.array(kam).ravel()


def makeTicks(x0, x1, N=None, labels=None, digits=None, logscale=False, expformat=False):
def makeTicks(x0, x1, N=None, labels=None, digits=None, logscale=False, useformat=""):
"""Internal use."""
# useformat eg: ":.2f", check out https://mkaz.blog/code/python-string-format-cookbook/

# Copyright M. Musy, 2021, license: MIT.

if x1 <= x0:
Expand Down Expand Up @@ -1770,7 +1771,7 @@ def makeTicks(x0, x1, N=None, labels=None, digits=None, logscale=False, expforma
ticks_float.append(tickn)

else:
# ..now comes one of the shortest and most painful pieces of code i ever wrote:
# ..here comes one of the shortest and most painful pieces of code:
# automatically choose the best natural axis subdivision based on multiples of 1,2,5
dstep = (x1 - x0) / N # desired step size, begin of the nightmare

Expand Down Expand Up @@ -1808,7 +1809,12 @@ def makeTicks(x0, x1, N=None, labels=None, digits=None, logscale=False, expforma
fulaxis = np.unique(np.clip(np.concatenate([negaxis, posaxis]), x0, x1))
# end of the nightmare

if digits is None:
if useformat:
sf = "{" + f"{useformat}" + "}"
sas = ""
for x in fulaxis:
sas += sf.format(x) + " "
elif digits is None:
np.set_printoptions(suppress=True) # avoid zero precision
sas = str(fulaxis).replace("[", "").replace("]", "")
sas = sas.replace(".e", "e").replace("e+0", "e+").replace("e-0", "e-")
Expand All @@ -1833,10 +1839,11 @@ def makeTicks(x0, x1, N=None, labels=None, digits=None, logscale=False, expforma
tickn = linInterpolate(tp, [x0, x1], [0, 1])
ticks_float.append(tickn)
if logscale:
if expformat:
ticks_str.append(f"10^{ts}")
val = np.power(10, tp)
if useformat:
sf = "{" + f"{useformat}" + "}"
ticks_str.append(sf.format(val))
else:
val = np.power(10, tp)
if val >= 10:
val = int(val+0.5)
else:
Expand Down
2 changes: 1 addition & 1 deletion vedo/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_version='2022.2.3.dev13'
_version='2022.2.3.dev14'

0 comments on commit a94b3c1

Please sign in to comment.