Please I need an explanation to this error

I am currently stuck on this part of my guided project on “Finding Heavy Traffic Indicators on I-94” and seem not to be able to figure out the error.*

My code:

plt.figure(figsize = (10,5))
time_of_the_day = [daytime, nighttime]
for i, day in zip(range(1,3), time_of_the_day):
    plt.subplot(1,2,i)
    plt.hist(day['traffic_volume'])
    plt.title(day)
    plt.xlabel('Traffic Vol')
    plt.ylabel('Freq')
    plt.xlim([-100,7000])
    plt.ylim([1,8000])
plt.show()


I noticed that when I run it without commenting on the line of .title(), it generates the error which is pointed out in the error message

However, when I do otherwise that is, comment on the aforementioned line, it runs without an error

Please can someone kindly explain this. Thank you in advance.

When you come across an error, one of the first things you should do is go to the documentation for the library that the code is from. In this case, the error is in the code using plt.title() so let’s go to the documentation: matplotlib.pyplot.title — Matplotlib 3.1.2 documentation.

If you read the documentation there, the first parameter that plt.title accepts is:

label : str

The “str” means that the parameter must be a string. Your code reads as follows:

plt.title(day)

My guess is that you wanted to write:

plt.title("day")

but it’s possible that the variable day is supposed to be a string and it isn’t, in which case you’ll want to go back to that variable and ensure nothing is going wrong there.

Best of luck!

1 Like

Thank you very much for your response and clarity on this