There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
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!
In step 4 of 5 the visualization it kept throwing an error for me, telling me to be sure to set the x tick marks to the length of average order. I did that but it wasn’t accepted. in the solution however, it is set to the length of bar heights, which makes more sense. you should probably make sure the error messages match the actual expectations.
yerr=std_order is not correct.
check question number #3 and read carefully the instructions for question #4 and you will know what to put there
also, you are missing the x element for the plt.bar.
the x element is the first parameter that should go in there, before the bar_heights… it will create one tick for each month.
What is the best way to convert the month numbers into month names? I used a dictionary and a list comprehension, which worked less cleanly than I had hoped.
I had to put quotes around the keys in my dictionary, which makes me think that the numbers in my months variable were stored as strings rather than integers. How could I have done this better?
`#A list of months, which are numbers (ie, April is stored as 4).
months = avg_order.month
#Why are these stored as strings (ie "4") instead of integers (ie 4)?
#A dictionary which can be used to convert from month numbers to month names
months_dict = {"4":"April","5":"May","6":"June","7":"July","8":"August","9":"September"}
#These have quotes around the keys since the months variable stores these as strings
#A new list of months, which are month names (ie, April).
#These can be used to create the plot.
month_names = [months_dict.get(i) for i in months]
#Creating the Bar Chart
ax = plt.subplot()
bar_heights = avg_order.price
bar_errors = std_order.price
plt.bar(range(len(month_names)), bar_heights, yerr = bar_errors, capsize = 5)
#Labels
ax.set_xticks(range(len(month_names)))
ax.set_xticklabels(month_names)
plt.title("Order Price by Month")
plt.ylabel("Price")
plt.show()`
Question1: Why do you need to do range(len( before bar_heights. ?
Question2: How does the barchart know what months are it needs, because I don’t see month numbers in bar_heights?
Thanks! Just learned about the pd.to_numeric() function, which offers an easy way to solve my problem!
#A list of months, which are numbers (ie, April is stored as 4).
months = avg_order.month
months = pd.to_numeric(months)
#A dictionary which can be used to convert from month numbers to month names
months_dict = {4:"April",5:"May",6:"June",7:"July",8:"August",9:"September"}
#A new list of months, which are month names (ie, April).
#These can be used to create the plot.
month_names = [months_dict.get(i) for i in months]
#Creating the Bar Chart
ax = plt.subplot()
bar_heights = avg_order.price
bar_errors = std_order.price
plt.bar(range(len(month_names)), bar_heights, yerr = bar_errors, capsize = 5)
#Labels
ax.set_xticks(range(len(month_names)))
ax.set_xticklabels(month_names)
plt.title("Order Price by Month")
plt.ylabel("Price")
plt.show()
To your first question ,the plt.bar function takes an argument telling it where to place each of the bars. If you’re just doing a basic bar chart, a sequence of numbers (one for each bar) will do the trick. That’s what the range(len(bar_heights) does within the plt.bar in this solution.
To your second question, in the codecademy solution the names of the months are entered manually in the ax.set_xticklabels. (I did it a different way in my code).
I find this troublesome too. for the purposes of knowing what the range of months is, how would we code a simple solution from the code on this assignment page?
I tried importing numpy, creating months as an np.array and then listing np.unique(months), but the platform isn’t offering a screen for this return. I can print the entire orders.months to see the list of months (with indexing, as an table) but scrolling this is not an accurate way to determine the range of months in this list.