【发布时间】:2020-12-01 18:23:57
【问题描述】:
我正在尝试使用axvline 绘制一条垂直线,但最终出现以下错误。
更新:问题与发布的参考文献有所不同,因为只使用了谨慎的 x 轴刻度位置(如 Trenton McKinney 所述)。
错误
TypeError: '<' not supported between instances of 'Timestamp' and 'numpy.float64'
代码
import pandas as pd
df = pd.DataFrame({'A': {0: '2020-01-01 06:00:00',
1: '2020-01-01 18:00:00',
2: '2020-01-02 06:00:00',
3: '2020-01-02 18:00:00',
4: '2020-01-03 06:00:00',
5: '2020-01-03 18:00:00',
6: '2020-01-04 06:00:00',
7: '2020-01-04 18:00:00'},
'B': {0: 5, 1: 5, 2: 6, 3:6, 4:7, 5:7, 6:1, 7:1}})
df['A'] = pd.to_datetime(df['A'])
df= df.set_index('A')
ax = df.plot.bar()
ax.axvline(pd.to_datetime('2020-01-02 18:00:00'), color='grey', zorder=1, linestyle='--', marker="v" )
ax.axvline(pd.to_datetime('2020-01-03 00:00:00'), color='grey', zorder=1, linestyle='--', marker="v" )
【问题讨论】:
-
在 matplotlib 中,条形图位于整数 xticks (0, 1, 2...) 中,试试
ax.axvline(3) -
我需要使用时间戳来绘制线条。
-
问题是,基于从 0 开始的整数位置在条形图上绘制条形图。x 轴刻度位置是离散的(即 0、1、2、...、n -1),因此两者之间没有刻度位置。日期只是文本标签。
标签: python pandas matplotlib