FAQ: Recreate graphs using Matplotlib! - Bar Chart with Error

This community-built FAQ covers the “Bar Chart with Error” exercise from the lesson “Recreate graphs using Matplotlib!”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Data Visualization in Python

FAQs on the exercise Bar Chart with Error

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

So, I’ve been working on recreating the bar chart with error and I’m able to get through a good chunk of it, but I’m having a problem with the code. Once I run the plt.axis code, my bars get scrunched up into the upper right hand corner of the chart and the dates get squished into the lower left hand corner. Same thing happens when if I comment out that line and run the ax.set_xticks or ax.set_ticklabels on the code. When I run it in python command line, I have the same problem, but I’m able to zoom to see the bars (but still can’t see the dates). Any thoughts on why this might be happening?

import codecademylib
from matplotlib import pyplot as plt

past_years_averages = [82, 84, 83, 86, 74, 84, 90]
years = [2000, 2001, 2002, 2003, 2004, 2005, 2006]
error = [1.5, 2.1, 1.2, 3.2, 2.3, 1.7, 2.4]

Make your chart here

plt.figure(figsize=(10,8))
plt.bar(years, past_years_averages, yerr = error, capsize = 5)
plt.axis([-0.5, 6.5, 70, 95])
ax = plt.subplot()
ax.set_xticks(range(len(years)))
ax.set_xticklabels(years)
plt.title(‘Final Exam Averages’)
plt.xlabel(‘Year’)
plt.ylabel(‘Test Average’)
plt.savefig(‘my_bar_chart.png’)
plt.show()

I am having the same problem. here is my code:

import codecademylib
from matplotlib import pyplot as plt

past_years_averages = [82, 84, 83, 86, 74, 84, 90]
years = [2000, 2001, 2002, 2003, 2004, 2005, 2006]
error = [1.5, 2.1, 1.2, 3.2, 2.3, 1.7, 2.4]

# Make your chart here
plt.figure(figsize=(10, 8))
plt.bar(years, past_years_averages, yerr=error, capsize=5)
plt.axis([-0.5, 6.5, 70, 95])
ax = plt.subplot()
ax.set_xticks(range(len(past_years_averages)))
ax.set_xticklabels(years)
plt.title('Final Exam Averages')
plt.xlabel('Year')
plt.ylabel('Test average')

plt.show()
plt.savefig('my_bar_chart.png')

I know this is late but I finally saw the problem. In the original plt.bar() call, you don’t put “years” as the x_values… it should be range(len(past_years_averages)) to get a list of numbers.

2 Likes

that makes sense if we’re referring to it in that way with the axis adjusting, thank you!
Alternatively, could we adjust the axis differently? what does that even do, give the chart more space to the left and right? because as soon as I do it all my data disappears if I don’t use the len as you suggested.