添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
逆袭的凉面  ·  LOFAR telescope: ...·  1 月前    · 
豪情万千的皮带  ·  java和PHP ...·  5 月前    · 
率性的山楂  ·  debian开启tun·  8 月前    · 
Community%20FAQs%20on%20Codecademy%20Exercises 1000×208 141 KB

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
  • What are some things that a bar chart with error can tell us?
  • 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')
                  

    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?
    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)) # 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 = fig.add_subplot()
    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.

    tgrtim:

    ax = fig.add_subplot() ax.bar(years,past_years_averages, yerr=error, capsize=5) ax.axis([-0.5, 6.5, 70, 95])

    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.

    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…

    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?