Matplotlib - Stepped histogram with already binned data

前端 未结 4 1661
温柔的废话
温柔的废话 2020-12-15 06:13

I am trying to get a histogram with already binned data. I have been trying to use bar() for this, but I can\'t seem to figure out how to make it a stepped hist

4条回答
  •  攒了一身酷
    2020-12-15 06:52

    For some reason, the last bin isn't properly closed when I try it. It is not visible from the previous answers if the last line is showed, so I decided to make my own function, which does what I want.

    def make_bar_contour_plot(ax,x_input,y_input):
    
        x = list(np.ravel(zip(x_input[:-1],x_input[:-1]+1)))[1:]
        x += [x[-1]+20] + [300] 
        y = list(np.ravel(zip(y_input,y_input))) +[0]
        ax.plot(x,y,ls='steps')
    
        return ax
    

    The 20 and 300 that are added are my binsize and ending value respectively, and need to be adjusted if anyone wants to use this. x_input and y_input are the returning values from np.histogram. My resulting plot (in blue the contour, plotted with above function. In red, the barplot of the same data):

    My result in contourplotting a histogram

提交回复
热议问题