FAQ: Project: Board Slides for FoodWheel - Orders Over Time

This community-built FAQ covers the “Orders Over Time” exercise from the lesson “Project: Board Slides for FoodWheel”.

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

Data Science

FAQs on the exercise Orders Over Time

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 (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 (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!

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.

I am struggling to plot the bar chart. This is what I am putting together on the following elements.

plt.bar(bar_heights, yerr=std_order, capsize = 5)

Any help?

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. :slight_smile:

1 Like

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()`
1 Like

There are a few modules for working with dates and such which may give you an easy way to do it and matplotlib has it’s own methods. Have you searched around at all? A lot of queries already have neat solutions. These matplotlib recipes seem to go along the right lines-
https://matplotlib.org/3.1.1/gallery/text_labels_and_annotations/date.html
https://matplotlib.org/3.1.0/gallery/ticks_and_spines/date_concise_formatter.html

Or for a more general solution there were a number of results, one example-

2 Likes

The solution to the exercise is:

bar_heights = avg_order.price

bar_errors = std_order.price

plt.bar(range(len(bar_heights)),

        bar_heights,

        yerr=bar_errors,

       capsize=5)

ax.set_xticks(range(len(bar_heights)))

ax.set_xticklabels(['April', 'May', 'June', 'July', 'August', 'September'])

plt.ylabel('Average Order Amount')

plt.title('Order Amount over Time')

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?

Thank you!

1 Like

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()
2 Likes

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

https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.bar.html

3 Likes

At first I made a dictionary by myself too. Your post is very useful to find a better solution. Thank you!

import calendar

month_names = [calendar.month_name[int(month)] for month in avg_order.month]
1 Like

A couple of points of Question 4 were a bit misleading/confusing to me. Just in case anyone else found this to be the same…

  • Create an axes object called ax using plt.subplot .

This was already completed in in Question 1.

  • Make sure that you label each bar with the name of the month (i.e., 4 = April).

4 does not represent April, but 0 does.

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.