scikit-hep / histbook

Versatile, high-performance histogram toolkit for Numpy.
BSD 3-Clause "New" or "Revised" License
109 stars 9 forks source link

statelessness has changed with logscale #39

Closed imandr closed 6 years ago

imandr commented 6 years ago

Showing dynamically updated histograms, I have been using this feature of histbook, which I found very useful, quite cool and "pythonic":

from histbook import Hist, bin, beside
from vega import VegaLite
import numpy as np

h=Hist(bin("x", 50, -5, 5))

display = beside(      # this looks (and used to act) like plotting instructions without actual plotting
    h.step(),
    h.step(yscale={"type":"log"})
)

# fill and show the histogram in iterations

x = np.random.normal(size=110)
h.fill(x=x)
display.to(VegaLite)        # now do actual plotting using "instructions" defined earlier

visualization

# 2nd iteration
x = np.random.normal(size=10000)
h.fill(x=x)
display.to(VegaLite)        # display again using same instructions

visualization 1

The left histogram looks good, while the right (logscale) histogram would not update its y axis scale to accommodate for new counts

jpivarski commented 6 years ago

It should be stateless: everything about the histogram is recomputed each time (because the user might change some of the properties of the step or other objects).

This happened because of a subtle bug: you pass in yscale={"type": "log"} and I attached it to the plot, then modified it in place to add the y log domain. Since you're not calling step again with a fresh yscale, I find an explicit domain on the yscale I thought I got from you and let the user override what I have computed for the new y log domain.

Now I defensively copy.deepcopy the user arguments.