Explaination to this code please

Please i need explanation to this code, like how did you come up with if sp ==0: and elif sp == 5: What was the idea behind the statement.
if sp == 0:
ax.text(2005, 87, ‘Men’)
ax.text(2002, 8, ‘Women’)
elif sp == 5:
ax.text(2005, 62, ‘Men’)
ax.text(2001, 35, ‘Women’)
plt.show()

What other way can this be solved without using ‘if statement’. I tried this:
ax.text(sp, 2005, 87, “Men”, horizontalalignment= “left”)
ax.text(sp, 2002, 8, “Women”, horizontalalignment = “left”)

ax.text(sp, 2005, 62, “Men”, horizontalalignment= “right”)
ax.text(sp, 2001, 35, “Women”, horizontalalignment = “right”)

But it didn’t work.

I also noticed that the annotation is on Engineering and Maths/Statistics. What if i want all my chats to have the annotations

Hi @oriji.sandra
Can you share the screen link to have a little bit more of context and being able to help you?
Thanks!

Sorry for the incomplete information. Here is the link:
https://app.dataquest.io/m/148/color%2C-layout%2C-and-annotations/7/annotating-in-matplotlib

I also attached a screenshots too

I marked the place i need explanation.
In addition, what other way can this be solved without using ‘if statement’. I tried this:
ax.text(sp, 2005, 87, “Men”, horizontalalignment= “left”)
ax.text(sp, 2002, 8, “Women”, horizontalalignment = “left”)

ax.text(sp, 2005, 62, “Men”, horizontalalignment= “right”)
ax.text(sp, 2001, 35, “Women”, horizontalalignment = “right”)

But it didn’t work.

I also noticed that the annotation is on Engineering and Maths/Statistics. What if i want all my chats to have the annotations

1 Like

As @alegiraldo666 said, a link to the page you were working on would really help with context and being able to help you specifically.

That said, I can tell from your code that you’re working on some sort of data visualization and building a plot using subplots. I’m going to guess that sp stands for “subplot” and that you need labels for the subplots with indices of 0 and 5 but not the others…that’s why you are using sp ==0 and elif sp == 5.

UPDATE:
It looks like we were typing at the same time! Thanks for the link and context for your question.

Unfortunately the ax.text() method does not support these types of arguments. Since you only want to use labels on a couple of strategically placed plots (not every plot needs a legend because it gets too busy) the best way to handle this (at this stage of our learning) is to use a conditional statement:

if this_is_the_right_sub_plot:
plot the legend

The annotations appear on particular plots based on their location and how much it adds to the readability of the plot overall, it is not related to the subjects being studied.

If you wanted to put a legend/label on each plot, you could do it inside the for loop but be warned, each would be placed in the same location on the plot, which might not work for every plot because the lines are in different locations. So, in order to put a legend on each plot, I would build a conditional statement for each one (ie sp==0, sp==1, sp==2, …) so that I could control exactly where it is placed on the plot, relative to the lines. (ie I could change the values for the arguments on ax.text(x, y, s) to customize each sub plot) Definitely check out the documentation for more help on this function.