3

My data consists of a variety of volatility values, that is, decimal numbers between 0 and 1. Now, only values between 6% and 24% are particularly relevant and so I'm trying to build a histogram to show the relative counts of these values. I want a histogram that has bins of 0-6%, 6-8%, ... 22-24%, >24% but this proving to be very difficult. There should be 11 bins total, and the labels for the bins should be centered under the bins. The y-axis can't have any labels as only the relative counts matter, and including values on the y-axis would detract from the relativity that I'm trying to emphasize.

I have come crazy close to being able to do this by reading answers like Matplotlib xticks not lining up with histogram and Bin size in Matplotlib (Histogram).

If anyone can help me out with this, I would be immensely grateful. Thanks for your help out there.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.path as path

graph1, ax = plt.subplots()

n, bins = np.histogram(values, 11)

left = np.array(bins[:-1])
right = np.array(bins[1:])
bottom = np.zeros(len(left))
top = bottom + n

XY = np.array([[left, left, right, right], [bottom, top, top, bottom]]).T

barpath = path.Path.make_compound_path_from_polys(XY)

patch = patches.PathPatch(barpath, facecolor="blue", edgecolor="#FFFFFF", 
alpha = 0.75)
ax.add_patch(patch)

ax.set_xlim(left[0], right[-1])
ax.set_ylim(bottom.min(), top.max())

graph1.suptitle("1-Year Rolling Volatilities")
ax.axes.get_yaxis().set_visible(False)

plt.show()

This generates almost the right histogram, but the bins aren't on the intervals that I want, the xticks aren't centered, and there isn't one label for each bin.

Dan White
  • 533
  • 1
  • 8
  • 18
  • You need to show some code and explain how the output is different from what you want. – BrenBarn May 31 '17 at 18:51
  • I think the code I used is too far from what I actually want. I actively chose not to post code because I didn't want someone working from what I have already used. If you really think I should be including my code I'll post it, but I want a completely fresh solution, not a tweak of what I've already got. I don't like the method I've used and a new solution can only be better than what I have – Dan White May 31 '17 at 18:54
  • No, you are not meant to post *your* code, but a [mcve] of the problem. – ImportanceOfBeingErnest May 31 '17 at 18:55
  • Can you please post your python version and system? – Mia May 31 '17 at 18:59
  • I've got version 3.6.1, on a windows 7 machine – Dan White May 31 '17 at 19:01
  • This seems to be exactly the problem that is answered in [this question](https://stackoverflow.com/questions/27083051/matplotlib-xticks-not-lining-up-with-histogram). Have you even tried to implement the solutions from there? Unless you specify exactly what issue you have using predefined bins as seen in the answers, this question is useless as the answer would just be the same as over there. – ImportanceOfBeingErnest May 31 '17 at 19:03
  • @ImportanceOfBeingErnest yeah I tried that. It might just be because I'm not so bright, but because they're dealing with integer counts rather than continuous data, everything I tried over there seemed to fail. Also, you'll notice that they use equal bin widths, whereas I am interested in unequal bin widths. – Dan White May 31 '17 at 19:11
  • I'm not saying that you need be able to implement their solution. What I'm saying is that the question does not make it clear at which point you're having the problem. I.e. there is no coherent problem description which would allow to help you. Such questions should be closed immediately, because they would force people who try to answer into guessing at which level you have the problem. So, again, what is needed is a [mcve] of the issue in which you show what fails. (Unless you are happy with the solution below, which is also correct.) – ImportanceOfBeingErnest May 31 '17 at 19:25
  • Yeah, I've got too many problems wrapped up here, and this is literally my first day ever using python. I think you've correctly identified my biggest problem as well: that I don't understand where my problems come from. I'm going to have to rewrite the entire histogram section and try again. – Dan White May 31 '17 at 19:27

1 Answers1

3
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.path as path

graph1, ax = plt.subplots()

values = [0.15, 0.23, 0.061, 0.085, 0.06, 0.09, 0.07, 0.05, 0.04, 0.08]
#n, bins = np.histogram(values, 11)
plt.hist(values, bins=[0, 0.06, 0.08, 0.22, 0.24, 1.00])

You can also use somthing like this to set the tick under your bins. More about setting ticks at this stack overflow post Matplotlib - label each bin

Dest
  • 43
  • 7
  • Yeah, that's close, I expected that to be the solution too, but it's not quite right. I wasn't able to simply list the bin edges. Also, the code's up now, but I'm not sure whether I should be using numpy.histogram() or plt.hist(), I would use whichever is simpler. – Dan White May 31 '17 at 19:09
  • Although you posted the code it is not enough for me to run it on my system. So I can't test things. It says undefined name path, patches, values, etc. – Dest May 31 '17 at 19:16
  • I just included the relevant imports – Dan White May 31 '17 at 19:18
  • I would avoid using this method though, I tried to explain in another comment that I'm certain this can be done more elegantly; this seems like a pretty standard request. I've backed myself into a corner here by using the patches and path method though. If you can do this simpler, then I would be more interested in that solution than a tweak to my own. – Dan White May 31 '17 at 19:19
  • I have posted what I think is closer to what you want. I had to arbitrarily define a set for values. You could try defining different colors for your bars to more clearly see the data – Dest May 31 '17 at 19:32
  • I can't comment out n or bins as they're needed elsewhere, I need to rewrite this section so that I don't need either of them so I can manually designate the bins. – Dan White May 31 '17 at 19:39
  • Ok I deleted everything besides the code I showed you above and that alone plotted the histogram. I did not need to use Path. So I felt my code was simpler – Dest May 31 '17 at 19:45
  • I've kind of given up on this, but I'll tell you why this doesn't work: your x axis actually runs from 0 to 1 which I can't have. I don't want proportional representation of the bins on the x axis. I just want bins to display their relative heights. the display of the bins will show them as equal widths, but their actual width will be as stated. – Dan White May 31 '17 at 20:20