1

I have gridded temperature data for a particular point pulled into an list, and am plotting this on a bar chart, as follows:

Example Bar Graph

Inevitably, temperatures from this gridded dataset will fall below 0 degrees, and I am looking to scale the Y-axis such that a mix of bars with both positive and negative values will both ascend towards the top of the image.

I have looked into the example code found here, however to the best of my understanding, this code assumes that all values in the array are negative, which the dataset I am working with will not have.

The code assembled to generate the graph seen below (excluding construction of the initial list) is as follows:

objects = (''+ mon0 +' '+ date0 +'', ''+ mon1 +' '+ date1 +'', ''+ mon2 +' '+ date2 +'', ''+ mon3 +' '+ date3 +'', ''+ mon4 +' '+ date4 +'', ''+ mon5 +' '+ date5 +'', ''+ mon6 +' '+ date6 +'', ''+ mon7 +' '+ date7 +'', ''+ mon8 +' '+ date8 +'', ''+ mon9 +' '+ date9 +'', ''+ mon10 +' '+ date10 +'')
y_pos = np.arange(len(objects))

fig, ax = plt.subplots(figsize=(14,8))

bar_width = 0.40

rects = plt.bar(y_pos, btv_list, bar_width, color='#cc0000', edgecolor='black')
plt.xticks(y_pos, objects)
plt.ylabel('Temperature (°F)')
plt.xticks(y_pos, objects)

for rect in rects:
    y_value = rect.get_height()
    x_value = rect.get_x() + rect.get_width() / 2
    space = 3
    va = 'bottom'
    label = y_value
    plttxt = plt.annotate(label, (x_value, y_value), xytext=(0, space), textcoords="offset points", ha='center', va=va)
    plttxt.set_fontsize(14)
    plttxt.set_weight('semibold')

fig.tight_layout()
TornadoEric
  • 399
  • 3
  • 16
  • "a mix of bars with both positive and negative values will both ascend towards the top of the image."... Could you perhaps explain this more OR show some sample output figure in your question? – Sheldore Oct 04 '18 at 18:12
  • Sure, I fed this script a randomized list, with a mix of both positive and negative values. The result (see image here: https://imgur.com/hPjcXYF) has negative numbers in the list descend from "0" on the y-axis. My goal is to have all bars ascend on the chart from the smallest value in the list, which happened to be -8 in this example. – TornadoEric Oct 04 '18 at 19:07

1 Answers1

3

Is this what you are trying to get?

h = np.random.normal(size=(10,))
min_val = min(h)

plt.figure()
rects = plt.bar(x=range(10),height=h-min_val, bottom=min_val)
plt.axhline(0.,ls='--', lw=1, color='grey', zorder=-1)

for rect in rects:
    y_value = rect.get_height()+min_val
    x_value = rect.get_x() + rect.get_width() / 2
    space = 3
    va = 'bottom'
    label = '{:.2f}'.format(y_value)
    plttxt = plt.annotate(label, (x_value, y_value), xytext=(0, space), textcoords="offset points", ha='center', va=va)
    plttxt.set_fontsize(8)
    plttxt.set_weight('semibold')

enter image description here

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • This is exactly what I'm looking for, thank you very much for solving this issue! An additional question, if I wanted the smallest value in the dataset to have a bar displayed (albeit small in scope of the other date), would I subtract a specific number from 'bottom=min_val'? – TornadoEric Oct 04 '18 at 20:23
  • I would round down `min_val` using `np.floor` – Diziet Asahi Oct 04 '18 at 20:43