# 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:

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

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

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

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?

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]

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]

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.

6 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.

Thank you josephgibbons3799856 for the solution!!

I have a new issue related to the same ex:

When I tried to replicate the exercise in jupyter, even I get the same result I do not understand the following warning message:

Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
if name == ‘main’:

Blockquote
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]
plt.figure(figsize=(10, 8))
plt.bar(range(len(past_years_averages)), 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)

If i put ‘years’ in plt.bar() i get directly what i get when i do :

Create an `ax` object using `plt.subplot()` . Use `ax` to set the x-axis ticks to be `range(len(years))` and the x-axis labels to be the `years` list.
so why use 'years ’
can u help me out with this?

the following code allows me to move on to the next exercise, but the graph figure is empty. I see the title, the x and y labels and correct tick labels. Yet the bars do not show up at all.
What am I missing?
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]

plt.figure(figsize=(10,8)) # Create a figure with height 8 and width 10
plt.bar(years,past_years_averages, yerr=error, capsize=5) # Plot bars in past_years_average, with error bar, and capsize 5
plt.axis([-0.5, 6.5, 70, 95]) # set axis to -0.5 to 6 on x-axis, and 70-95 on y-axis
ax = plt.subplot() # Create an object, use ax to set x-axis ticks and x-axis labels
ax.set_xticks(range(len(years)))
ax.set_xticklabels(years)
ax.set_title(‘Final Exam Averages’) # title the graph
ax.set_xlabel(‘Year’) # set xlabel to ‘Year’
ax.set_ylabel(‘Test average’) # set ylabel to ‘Test average’
plt.savefig(‘my_bar_chart.png’) # save figure
plt.show()

Hi @serg911.

As for why you don’t see anything- What happens when you adjust your axis limits with plt.axis()? What were the original limits? Is your data in the new range?

I’d also be very careful about calling subplot after the axis has already been created. In fact, having just copy/pasted this code quickly it throws out a warning that in the future this will create an entirely new axis instance (I’m kinda surprised this wasn’t already the case tbh).

To ensure your code is still supported in future versions of matplotlib (and IMO just generally for a more correct way) define the axis first, then plot and then start fiddling with the values.

``````fig = plt.figure(figsize=(10,8))
ax.bar(years,past_years_averages, yerr=error, capsize=5)
ax.axis([-0.5, 6.5, 70, 95])  # This is still an issue...
# Then start chaging ticks etc.
... etc.
``````

I’m not certain that you need to go about chaging x-axis ticks and labels in this isntance but plot it and find out.

Thank you tgrtim. I think it had to do with the way I was setting the plt.axis line. Learning this stuff is certainly challenging and I need to be more methodical in my debugging.

1 Like

For task 4, why can’t we do something like `plt.set(xlim=(-0.5, 6.5), ylim=(70, 95))` ?

What is the point of creating the subplot? Why not instead have the axis x_values be a little before and after the years presented?

From what I can find online, there does not appear to be a set function for matplotlib. Set is a function for python in general, but for a much different purpose than you’re looking for. There is a pet.setp() function, which might be what you’re thinking of. However, from what I can tell you can only pass in parameters that you would pass into the plot/bar/pie/hist function, such as marker, linestyle, etc…

I have the exact same issue and my code is exactly as yours. I followed the instructions but there must be something I am doing wrong.

If the first argument of `plt.bar()` in your code is `years`, probably it’s the same problem that was posted earlier in this FAQ.

That was it. Thank you.

You deserve a medal sir, I was about to throw my laptop through the window because I couldn’t get it done. Now I feel stupid for such a simple bug.

Million thanks! It worked for me. I would have never noticed such a bug

Hello! I am confused in which situations do we need to use range(len(data_list)) and when just data_list.

I have bumped into the same error as other students here writing a code plt.bar(years, past_years_averages, yerr=error, capsize=5), when it should be as below:

``````past_years_averages = [82, 84, 83, 86, 74, 84, 90]
years = [2000, 2001, 2002, 2003, 2004, 2005, 2006]

plt.bar(range(len(years)), past_years_averages, yerr=error, capsize=5)
plt.axis([-0.5, 6.5, 70, 95])
ax = plt.subplot()
plt.show()
``````

I understand that without range(len(years)) everything would be messed up at the line [plt.axis([-0.5, 6.5, 70, 95])].
Would it be safe to consider that with plt.bar we always want to use range(len(dataset)) and we don’t need to do the same thing with y axis values?

Just out of curiosity, what’s the problem with using `years` as the first argument in `plt.bar` ? It fits into the bar chart well and comes out looking the same as if we use `range(len(past_years_averages)`