0

I can't get my text to show in my plot, axhline() shows, but not text(). I'm new to pandas and matplotlib and I'm obviously not understanding something.

plot image

# df is a pandas Dataframe

df_last_24 = df[df['Date']>=(dt.datetime.now()-dt.timedelta(hours=24))]

ax = df_last_24.plot.line(x="Date",title="Air Qualty Index over the last 24 hours")

# Define the date format
years_fmt = mdates.DateFormatter('%I:%M %p')
ax.xaxis.set_major_formatter(years_fmt)


plot.axhline(linewidth=4,y=300, color='#731425', linestyle='-')
plot.text(0, 300, 'Hazardous', fontsize=10, va='center', ha='left', backgroundcolor='w')

plot.axhline(linewidth=4,y=200, color='#8c1a4b', linestyle='-')
plot.text(0, 200, 'Very Unhealthy', fontsize=10, va='center', ha='left', backgroundcolor='w')

plot.axhline(linewidth=4,y=150, color='#951d47', linestyle='-')
plot.text(0, 150, 'Unhealthy', fontsize=10, va='center', ha='left', backgroundcolor='w')

plot.axhline(linewidth=4,y=100, color='#e23127', linestyle='-')
plot.text(0, 100, 'Unhealthy to Sensitive Groups', fontsize=10, va='center', ha='left', backgroundcolor='w')

plot.axhline(linewidth=4,y=50, color='#f29d3a', linestyle='-')
plot.text(0, 50, 'Moderate', fontsize=10, va='center', ha='left', backgroundcolor='w')

plot.show(block=True)

2 Answers 2

1

The issue was the x coordinate in the dataframe graph is a time. Using min() and datestr2num() seems to solve the issue.

# Find Date/Time Range
column = df_last_24['Date/Time'].dt.strftime('%a %b %d %H:%M')
subtitle = column.min() + " - " +column.max()
text_x = mdates.datestr2num(column.min())

...

# Add Level lines and Labels
ax.axhline(linewidth=4,y=300, color='#731425', linestyle='-')
ax.text(text_x, 300, 'Hazardous', fontsize=10, va='center', ha='left', backgroundcolor='w')
Sign up to request clarification or add additional context in comments.

Comments

0

plt.axhline() and plt. are the correct descriptions. The other graphs may have been affected by the lack of data provided. Please check with this.

import matplotlib.pyplot as plt

plt.axhline(linewidth=4,y=300, color='#731425', linestyle='-')
plt.text(0, 300, 'Hazardous', fontsize=10, va='center', ha='left', backgroundcolor='w')

plt.axhline(linewidth=4,y=200, color='#8c1a4b', linestyle='-')
plt.text(0, 200, 'Very Unhealthy', fontsize=10, va='center', ha='left', backgroundcolor='w')

plt.axhline(linewidth=4,y=150, color='#951d47', linestyle='-')
plt.text(0, 150, 'Unhealthy', fontsize=10, va='center', ha='left', backgroundcolor='w')

plt.axhline(linewidth=4,y=100, color='#e23127', linestyle='-')
plt.text(0, 100, 'Unhealthy to Sensitive Groups', fontsize=10, va='center', ha='left', backgroundcolor='w')

plt.axhline(linewidth=4,y=50, color='#f29d3a', linestyle='-')
plt.text(0, 50, 'Moderate', fontsize=10, va='center', ha='left', backgroundcolor='w')

plt.show(block=True)

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.