Search code examples
pythonrangegraphing

How to create a horizontal bar graph graphing number ranges?


Does anyone know how to go about creating a horizontal bar graph that graphs number ranges? By which I mean the graphed values do not start at 0 on the x axis 0 and stop at another singular number but instead the values have a start and end values representing a range.

As far as I can see barh is just able to plot singular values instead of range values.

I have been trying to achieve this using matplotlib's barh however I cannot seem to figure out how to make it draw ranges.

Here is an example of what I mean:

Horizontal bar graph showing ranges of numbers within the range 0 to 1000

Can anyone advise me on how to draw this using matplotlib or another alternative Python module?


Solution

  • First you have to plot the bars you want to see and then you plot a white bar above the others. But this is more a hack than a good solution.

    import matplotlib.pyplot as plt
    import numpy as np
    
    
    target_bars_start = [10, 10, 15, 8]
    target_bars_end = [15, 23, 16, 10]
    N = len(target_bars_end)
    ind = np.arange(N)
    
    plt.barh(ind, target_bars_end, 0.35)
    plt.barh(ind, target_bars_start, 0.35, color='white')
    
    

    enter image description here