Home > other >  bar x-tick not as same as the image
bar x-tick not as same as the image

Time:03-29

Im not sure if i use the wrong data or if there is and edit i need to do and not seeing it. It would be nice if someone could take a look at the code. The problem here is that yerr at the first bar is at x=0 and in the image the yerr is somewhere around 2.5

Does someone know what i did wrong or forgot to edit?

the end result should be: picture how the end result should look like

my code:

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)

y_raw = np.random.randn(1000).cumsum()   15
x_raw = np.linspace(0, 24, y_raw.size)

x_pos = x_raw.reshape(-1, 100).min(axis=1)
y_avg = y_raw.reshape(-1, 100).mean(axis=1)
y_err = y_raw.reshape(-1, 100).ptp(axis=1)
bar_width = x_pos[1] - x_pos[0]

x_pred = np.linspace(0, 30)
y_max_pred = y_avg[0]   y_err[0]   2.3 * x_pred
y_min_pred = y_avg[0] - y_err[0]   1.2 * x_pred

barcolor, linecolor, fillcolor = 'wheat', 'salmon', 'lightblue'

fig, axes = fig, ax = plt.subplots()
axes.set_title(label="Future Projection of Attitudes", fontsize=15)
plt.xlabel('Minutes since class began', fontsize=12)
plt.ylabel('Snarkiness (snark units)', fontsize=12)
fig.set_size_inches(8, 6, forward=True)



axes.fill_between(x_pred, y_min_pred, y_max_pred ,color='lightblue')

axes.plot(x_raw, y_raw, color='salmon')

vert_bars = axes.bar(x_pos, y_avg, yerr=y_err, color='wheat', width = bar_width, edgecolor='grey',error_kw=dict(lw=1, capsize=5, capthick=1, ecolor='gray'))
axes.set(xlim=[0, 30], ylim=[0,100])

plt.show()

CodePudding user response:

yerr is meant to be the difference between the mean and the min/max. Now you're using the full difference between max and min. You might divide it by 2 to get a better approximation. To obtain the exact values, you could calculate them explicitly (see code example).

Further, by default, the bars are center aligned vs their x-position. You can use align='edge' to left-align them (as x_pos is calculated as the minimum of the range the bar represents). You could also set clip_on=False in the err_kw to make sure the error bars are never clipped by the axes.

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(1)

y_raw = np.random.randn(1000).cumsum()   15
x_raw = np.linspace(0, 24, y_raw.size)

x_pos = x_raw.reshape(-1, 100).min(axis=1)
y_avg = y_raw.reshape(-1, 100).mean(axis=1)
y_min = y_raw.reshape(-1, 100).min(axis=1)
y_max = y_raw.reshape(-1, 100).max(axis=1)
bar_width = x_pos[1] - x_pos[0]

x_pred = np.linspace(0, 30)
y_max_pred = y_avg[0]   y_err[0]   2.3 * x_pred
y_min_pred = y_avg[0] - y_err[0]   1.2 * x_pred

barcolor, linecolor, fillcolor = 'wheat', 'salmon', 'lightblue'

fig, ax = plt.subplots(figsize=(8, 6))
ax.set_title(label="Future Projection of Attitudes", fontsize=15)
ax.set_xlabel('Minutes since class began', fontsize=12)
ax.set_ylabel('Snarkiness (snark units)', fontsize=12)

ax.fill_between(x_pred, y_min_pred, y_max_pred, color='lightblue')

ax.plot(x_raw, y_raw, color='salmon')

vert_bars = ax.bar(x_pos, y_avg, yerr=(y_avg - y_min, y_max - y_avg),
                   color='wheat', width=bar_width, edgecolor='grey', align='edge',
                   error_kw=dict(lw=1, capsize=5, capthick=1, ecolor='grey', clip_on=False))
ax.set(xlim=[0, 30], ylim=[0, 100])
plt.tight_layout()
plt.show()

bar plot left aligned bars, explicit yerr calculation

  • Related