-2

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?

  • The linked documentation clearly shows that there is a parameter: *"left: sequence of scalars - The x coordinates of the left sides of the bars (default: 0)."*, which should enable what you require. At the moment, however, your question is being down-voted (and may be closed) because you have not shown any effort yourself. – SiHa Mar 06 '19 at 08:24
  • Hey SiHa, I had tried to research this but the closest example to what I was trying to achieve I could find was: https://stackoverflow.com/questions/11042290/how-can-i-use-xaxis-date-with-barh but that had code specific to date_time and I was unsure how to make this work with arbitrary non date time values. – Alan Jenkins Mar 06 '19 at 09:35

1 Answers1

1

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

dl.meteo
  • 1,658
  • 15
  • 25