繁体   English   中英

matplotlib text() 未显示在熊猫图中

[英]matplotlib text() not showing in pandas plot

我无法让我的文本显示在我的情节中,axhline() 显示,但不是 text()。 我是 Pandas 和 matplotlib 的新手,显然我不明白什么。

绘图图像

# 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)

问题是数据帧图中的 x 坐标是一个时间。 使用 min() 和 datestr2num() 似乎解决了这个问题。

# 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')

plt.axhline()plt. 是正确的描述。 其他图表可能因缺乏提供的数据而受到影响。 请检查这个。

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)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM