Forums

AttributeError: 'AxesSubplot' object has no attribute 'bar_label'

Hi there,

I'm a beginner python learner and pythonanywhere user. I uploaded a previously working script (yes I know it's not very efficient...I'm a beginner) - in short it pulls my bike ride data from Strava and plots it on a bar chart. When trying it on pythonanywhere I get the above error when tying to add value labels to the bars, which worked fine previously on Jupyter notebook. The relevant section is below. Any advice on why the error is happening and how to fix would be greatly appreciated since a quick google would suggest it should work https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar_label.html and indeed it did work for me elsewhere.

May thanks, Taz

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

Plot the planned kms in black

p1 =  ax.bar(br1, ride_plan, color ='black', width = -barWidth, label='Planned',  align ='edge')
p2 = ax.bar(np.arange(len(ride_actual)), ride_actual_rounded, color ='green', width = barWidth, label='Actual',  align ='edge')

add x axis label ticks by month name instead of number pulled from dataframe

ax.set_xticks(br1, labels=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])

remove the auto labelling of x-axis of 'month', add second y-axis and label y axes

plt.xlabel("")
plt.ylabel('kms ridden')
ax2=ax.twinx()
ax2.plot(np.arange(len(rides_number)), rides_number, color ='red', label="# rides")
plt.ylabel ('Number of rides')

autoscale the axes

plt.autoscale()

add legend labels which were set before

ax.legend()
ax2.legend(loc='upper left')

add value labels on top of the bars

ax.bar_label(p1, label_type='edge')
ax.bar_label(p2, label_type='edge')

save chart as .png file

plt.savefig('2022_ride_totals.png')

That looks like you're running code for one version of matplotlib in a different version of matplotlib. Make sure that the version that you wrote the code for is the same one that you are using on PythonAnywhere.

Thanks Glenn. Sorry for belated response. I managed to figure out how to install version 3.5.1 of matplotlib on my PythonAnywhere account and now all works as intended :)

Excellent, thanks for confirming that!