Placing Text using matplotlib

Screen Link:Learn data science with Python and R projects

I am trying to build a graph using matplotlib, and I am having trouble placing descriptive text on the graph itself.

My y values range from .9 to 1.65, and x ranges from 2001 to 2021

Here are the basics of what I am working with:

fig, ax = plt.subplots(figsize=(10,7))

I know that I have to use ax.text() to place any text, but whenever I try to enter basically any values for the x and y coordinates of the text, the entire graph disappears when I re-run the cell. I have plotted the following line, but if I use the same coordinates in ax.text(), I get the output I just described. Why might this be happening?

plt.axhline(y=1.19, xmin=.032, xmax=.96)

1 Like

I will also add that when I literally copy and paste from the solution notebok, I still get an error.

1 Like

Can you provide the code you are using as well as the error it produces?

Based on what I’m seeing in the solution notebook compared to plt.axhline(y=1.19, xmin=.032, xmax=.96), it appears there is a massive difference in the scale on your x-axis. Have you tried adding text using larger values for your x coordinates as is done in the solution notebook?

Seeing the actual code you are using and thus being able to reproduce the error could make it easier to figure out what’s going wrong here.

Hi, @Willyjgolden, this is more likely related to the answer I posted a while ago, let me know if it helps you fix the problem:

1 Like

Hi! So, initially, this worked, but now this doesn’t work for future graphs. I use get_xticks(), and literally copy and paste the x coordinate, and I STILL get the same error. Any ideas?

I use get_xticks(), and literally copy and paste the x coordinate, and I STILL get the same error.

Can you provide a specific example? Also, what’s the error name?

Hey! I fixed it! It was my own silly error. Thank you for your help!

1 Like