【问题标题】:Python matplotlib doesn't show full date on mouse hoverPython matplotlib 在鼠标悬停时不显示完整日期
【发布时间】:2018-04-01 20:53:49
【问题描述】:

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

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 笔记本绘制它会在右下角显示温度、年/月,但省略了日期

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

我怎样才能让它显示当天?任何格式都可以,但最好是 '01-Jan-2017'。

【问题讨论】:

    标签: pandas datetime matplotlib dataframe


    【解决方案1】:

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

    如果这是一个日期时间对象,使用matplotlib.dates.DateFormatter 是合理的。然后,格式说明符将是 "%d-%b-%Y",表示类似“2017 年 1 月 1 日”。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 2012-09-21
      • 1970-01-01
      相关资源
      最近更新 更多