0

I have the following code where I produced a 2d histogram with matplotlib of a function f of two variables, namely x and y. x goes from 0 to 7 and y from 0 to np.pi/2. Below you can find related code. I would like to project onto the x-axis so that I have the function f only as function of x: f(x). How can I do that? I would like to plot it as a 1d histogram and possibly save the values in a list.

from matplotlib import cm
plt.figure(figsize=(20,16))
h = plt.hist2d(x, y, bins=[110,110],cmin=0.000001,cmap='jet')
plt.colorbar(h[3])
plt.title("SW Distribution for Pion Zero",fontsize=22)
plt.xlabel("Momentum (GeV/c)",fontsize=22)
plt.ylabel("Angle (rad)",fontsize=22)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.show()
Stefano555
  • 31
  • 3
  • Could you post the values of these variables `x`, `y`, and `h` – Anwarvic Jun 26 '20 at 15:46
  • You can use the returned `image` https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist2d.html. Maybe you can convert it to a Numpy array and sum along the vertical axis. – Joe Jun 26 '20 at 16:40
  • What works for sure is to use https://numpy.org/doc/stable/reference/generated/numpy.histogram2d.html and sum the returned `H` along the vertical axis. – Joe Jun 26 '20 at 16:41
  • 1
    [seaborn's jointplot](https://seaborn.pydata.org/generated/seaborn.jointplot.html) creates a 2d histogram with marginal histograms. You can remove one of the generated subplots if you only want one marginal histogram. – JohanC Jun 26 '20 at 17:37

0 Answers0