Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Errorbars in the middle of the bins (1D-plots) #428

Open
sjjamsa opened this issue Nov 30, 2020 · 3 comments
Open

ENH Errorbars in the middle of the bins (1D-plots) #428

sjjamsa opened this issue Nov 30, 2020 · 3 comments
Assignees

Comments

@sjjamsa
Copy link

sjjamsa commented Nov 30, 2020

Dear Serpent Toolers,

I've included some 1D-plots produced with serpent tools to my journal article. My reviewer wrote:

the error bars should be in the middle on the bins and not at the side

The solution I'd like
A keyword argument to move the errorbars to the middle of the bins.

I can start digging and implementing this myself.
But, if there are any guidelines, tips, tricks, or historical developments, I would love to hear it before I waste my time on something stupid or unacceptable.

Thank you.

@drewejohnson
Copy link
Collaborator

Hey @sjjamsa thanks for using this package in a journal paper! Please do reach back when it is accepted and published and we'll add it to the publication list 🎉

But to the plotting issue at hand. The short answer is there isn't an easy way to do this, because we fall back to matplotlib errorbar method. That method only accepts the x and y data points, and the marks and errorbars are drawn according to those points. The drawstyle argument does not alter the placement of the errorbars 🙁 I'd prefer any fix to be relegated to matplotlib, since that is what handles all of our plotting. And this could be a very useful routine for people outside this project too!

For a short-term fix, you're probably going to have to implement this on your own. You could use our routines to draw the plot without errorbars, and then manually define the new x positions that corresponds to the midpoint of each bin. You can imagine how that could get complicated, trying to handle log or linear scaling, where the numeric midpoint may not be the visual midpoint of the bin as it is plotted.

Extra

There's some really good discussion by the MPL developers in some similar-but-not-really-related issues matplotlib/matplotlib#15065 and matplotlib/matplotlib#18275 if you want to dig a little deeper into maybe making this a PR for MPL

@sjjamsa
Copy link
Author

sjjamsa commented Dec 2, 2020

Thank you @drewejohnson!

I looked into the serpentTools implementation, and found out serpentTools uses the drawstyle='steps-post' for matplotlib.pyplot.plot(). This conveniently gets the bins correctly from low-limit to max-limit. And as you describe above, the errorbars are unfortunately shown on the low-limit points of the bins.

There are probably multiple ways this could be fixed, but the code would definitley become more complex.

So here is my work-around:

# first plot the normal spectrum plot without errorbars
det0.spectrumPlot(normalize=False,sigma=None,label="Full model",color='k')
# then plot the error bars only
plt.errorbar(det0.energy[:,2],det0.tallies,yerr=det0.tallies * 3 * det0.errors,fmt='.',markersize=0,capsize=3,color='k')

So do you want to close this ticket and wait for someone in upstream matplotlib to implement exactly what we want?

@drewejohnson
Copy link
Collaborator

I'll think on this some more, but your solution does indeed plot the errorbars at the midpoint of the bin.

And the code would become a bit more complicated, but we've attempted to market this project as a way to get publication quality plots with ease. Having MPL handle this upstream would be great, but I think we can shoulder the burden in a few select cases.

Our plot methods also know if we're plotting on a log-x scale, so we can place the errorbars in the logarithmic midpoint rather than arithmetic midpoint (as you've done here). Requires a little more logic, but that would place the errorbars directly in the center of the bin as it was drawn (e.g. the value 75 would appear closer to the value 50 than 100 if plotted on a log scale)

I'm good to leave this open for a time, both as a note to potential visitors, and also as a reminder to add this for at least the spectrum plot. No timeline at the moment though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants