Python Matplotlib在鼠标悬停时未显示完整日期 [英] Python matplotlib doesn't show full date on mouse hover

查看:70
本文介绍了Python Matplotlib在鼠标悬停时未显示完整日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有日期索引温度值的数据框:

I have a dataframe with date index and temperature values:

Date     Temperature
2015-10-21     9.118
2015-10-22     9.099
2015-10-23     8.945
2015-10-26     8.848
2015-10-27     8.848
2015-10-28     8.829
2015-10-30     8.935
2015-11-02     9.302
2015-11-03     9.012
2015-11-04     9.109
...
2017-10-16    10.160
2017-10-17    10.180
2017-10-18    10.140
2017-10-19    10.370
2017-10-20    10.360

使用交互式%matplotlib笔记本进行绘制,在右下方显示温度,年/月,但省略了一天.

Plotting it using interactive %matplotlib notebook shows the temperature, year/month on the bottom right, but omits the day.

#%matplotlib notebook
import pandas as pd
import matplotlib.pyplot as plt

x_axis = df.index.tolist()
y_axis = df['temperature'].tolist()

plt.plot(x_axis, y_axis)
plt.show()

如何显示日期?任何格式都可以,但最好使用"2017年1月1日".

How can I get it to show the day? Any format is fine, but preferably '01-Jan-2017'.

推荐答案

您可以使用轴的 format_coord 方法来指定状态栏或左下角显示的文本格式.笔记本图.

You can use format_coord method of the axes to specify the format of the text shown in the status bar or lower left corner of the notebook figure.

如果要作为日期时间对象,则使用 matplotlib.dates.DateFormatter 是合理的.然后,格式说明符将为%d-%b-%Y" ,例如"01-Jan-2017".

In case this is to be a datetime object, the use of a matplotlib.dates.DateFormatter is reasonable. The format specifier would then be "%d-%b-%Y" for something like '01-Jan-2017'.

import matplotlib.dates
datefmt = matplotlib.dates.DateFormatter("%d-%b-%Y")
fmt = lambda x,y : "{}, {:.5g}".format(datefmt(x), y)
plt.gca().format_coord = fmt

这篇关于Python Matplotlib在鼠标悬停时未显示完整日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆