mplhep
mplhep copied to clipboard
[feat] Could orientation="vertical" be added?
Hello,
We are designing a lib based on boost-histogram and mplhep. We want to build a plotting function that can draw the 2d histogram and its projections with respect to different axes - plot2d_full
in https://github.com/scikit-hep/hist/pull/80.
Everything goes well except this:
import boost_histogram as bh
import mplhep
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms
h = bh.Histogram(bh.axis.Regular(50, -5, 5),
bh.axis.Regular(50, -5, 5))
h.fill(
np.random.normal(size=50_000), np.random.normal(size=50_000)
)
fig = plt.figure(figsize=(5, 5))
grid = fig.add_gridspec(5, 5, hspace=0, wspace=0)
ax1 = fig.add_subplot(grid[1:5, 0:4])
ax2 = fig.add_subplot(grid[0:1, 0:4], sharex=ax1)
ax3 = fig.add_subplot(grid[1:5, 4:5], sharey=ax1)
ax1 = mplhep.hist2dplot(h, ax=ax1, cbar=False)
ax2 = mplhep.histplot(
h.project(1),
ax=ax2,
lw=4
)
base = plt.gca().transData
rot = transforms.Affine2D().rotate_deg(270)
ax3 = mplhep.histplot(
h.project(0),
ax=ax3,
transform=rot + base,
lw=4,
)
We expect an output like this:
But actually it comes to this:
And I find the rotation is not properly displayed:
mplhep.histplot(
h.project(0),
transform=rot + base,
lw=4
)
Could you help to find if there is any problem with histplot
or where I do mistakenly?
By the way, @henryiii thought using Line2d and QuadMesh, etc. would be a better idea for the return types of histplot/2d, do you think so?
Thanks!
Hi @LovelyBuggies, what you get pretty much makes sense. histplot
does not have a "horizontal" option, but I think it's also a bit of an overkill to use in this case, since you basically won't use any of its other features.
I would rather think the 2d plot you already have implemented could be factored out and put into mplhep
, that way it would be a free-standing function you could use on any 2d histogram. What do you think?
Nice if you have a function named histplot2d_full
, then I can directly call it in hist and it wouldn't be "overkill" as you said.
Btw, we have another plotting function plot_pull
and plotting methods that you can refer to ref., if you want to add them to mplhep.
@LovelyBuggies Would you like to contribute that or should I just scoop it out of hist? I think this could well be an option of the histplot2d
fcn rather than a standalone thing.
@andrzejnovak You can scope it out of hist 😀 . Feel free to update this thread, we are waiting to update a new version of hist that based on this feature. I am willing to help if you are meeting some problems.
I think having the ability to rotate could be useful standalone, and a histplot2d(skylines=True) could then potentially be composed of two of these, or even if it isn't, it might be useful to have it first?
Request: plt.hist
has orientation="vertical"
, histplot should have it too!
There's a mistake in the original problem code:
-base = plt.gca().transData
+base = ax3.transData
With this correction, the code above works fine. I would still recommend an orientation="vertical"
shortcut (which basically does exactly the code above), but I think otherwise, it is fine.
I'm going to wait for matplotlib 3.4 to come out with plt.stairs
which will make the vertical implementation cleaner.