Why do we need to convert to a string value

fig = plt.figure(figsize=(10,6))
colors = [‘red’, ‘blue’, ‘green’, ‘orange’, ‘black’]
for i in range(5):
start_index = i*12
end_index = (i+1)*12
year = i + 1948
subset = unrate[start_index:end_index]
plt.plot(subset[‘MONTH’], subset[‘VALUE’], c=colors[i], label=year)
plt.legend(loc=‘upper left’)

#This is an exericse from course"exploratory data visualization"-multiple plots- exercise 10/13 Here is my codes, it passed as expected. But when I checked the answers, it converts the label to a string value, as it shows: label = str(1948 + i).
#I don’t why we need to convert to a string value.

Because label requires it to be of string.

You are right that there is no need to convert to string.

You can look under “Other parameters” to find the description for label: https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.artist.Artist.set_label.html#matplotlib.artist.Artist.set_label

It will automatically call str() on the object you pass into label.
Always keep in view that even answers may not be perfect, try to do better!

1 Like

@hanqi Good morning. Good to see you. Thank you for your reply.

I just moved from Slack to this forum. Still trying to figure out things here. It is kind of quiet here except @alvinctk being really helpful.

Just some feedback - lesson 9 to 10 seems too big a jump in learning without explanation.

The task says “modify the code from the last screen” - screen 9 - wheres this screen, 10, is dealing with a loop using a range instead of a dataset.

What’s a loop got to do with the previous screen?

What is a ‘range’ in first line of the loop?

Am I being blind or are there no notes that detail all this?

Again, it seems too big a jump without explanation.