matplotlib 使用MinuteLocator为Pandas设置每分钟的分笔成交点.DataFrame.plot给出“溢出错误:int太大,无法转换”

nuypyhwy  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(197)

为1秒采样数据设置分钟次要刻度会引发:OverflowError: int too big to convert
考虑以下 Dataframe ,采样间隔为1秒,跨度约为30分钟:

  1. import matplotlib.pyplot as plt
  2. from matplotlib.dates import MinuteLocator
  3. import pandas as pd
  4. ndex = pd.date_range('2021-08-01 07:07:07', '2021-08-01 07:41:12', freq='1S', name='Time')
  5. df = pd.DataFrame(data=np.random.randint(1, 100, len(ndex)), index=ndex, columns=['A'])

现在我们绘制它:

  1. fig, ax = plt.subplots()
  2. df.plot(color='red', marker='x', lw=0, ms=0.2, ax=ax)

它创造了一个情节没有任何抱怨:

现在我想每分钟都有小滴答声。
我试过了

  1. ax.xaxis.set_minor_locator(MinuteLocator())

但是OverflowError: int too big to convert的情况就不行了

whitzsjs

whitzsjs1#

  • pandas.DataFrame.plot使用matplotlib作为默认的绘图后端,但它将日期刻度编码为unix时间戳,这将导致OverflowError: int too big to convert
  • 此处的默认值为kind='line',但在OP中使用marker='x', lw=0, ms=0.2来绘制杂乱的散点图。
  • pandas.DataFrame.plot.scatter将正常工作。
  • 使用matplotlib.pyplot.scatter将按预期工作。
  • matplotlib: Date tick labels
  • Matplotlib日期绘制是通过将日期示例转换为自某个纪元以来的天数来完成的(默认为1970-01- 01 T00:00:00)
  • seaborn.scatterplot也可以:
  • sns.scatterplot(x=df.index, y=df.A, color='red', marker='x', ax=ax)
    *python 3.8.11pandas 1.3.2matplotlib 3.4.3seaborn 0.11.2中进行测试

matplotlib.pyplot.scatter

  • 额外的格式设置会删除刻度标签中位于时间之前的月份('01')(例如'%m %H:%M')。
  1. import matplotlib.dates as mdates
  2. import matplotlib.pyplot as plt
  3. fig, ax = plt.subplots(figsize=(25, 6))
  4. ax.scatter(x=df.index, y=df.A, color='red', marker='x')
  5. hourlocator = mdates.HourLocator(interval=1) # adds some extra formatting, but not required
  6. majorFmt = mdates.DateFormatter('%H:%M') # adds some extra formatting, but not required
  7. ax.xaxis.set_major_locator(mdates.MinuteLocator())
  8. ax.xaxis.set_major_formatter(majorFmt) # adds some extra formatting, but not required
  9. _ = plt.xticks(rotation=90)

pandas.DataFrame.plot.scatter的第一个字符

  • 也可将pandas.DataFrame.plotkind='scatter'组合使用
  • x1米19英寸1x
  1. # reset the index so Time will be a column to assign to x
  2. ax = df.reset_index().plot.scatter(x='Time', y='A', color='red', marker='x', figsize=(25, 6), rot=90)
  3. ax.xaxis.set_major_locator(mdates.MinuteLocator())

  • 请注意这两种方法生成的xtick的差异

第一个月第一个月

  1. ax = df.plot(color='red', marker='x', lw=0, ms=0.2, figsize=(25, 6))
  2. # extract the xticks to see the format
  3. ticks = ax.get_xticks()
  4. print(ticks)
  5. [out]:
  6. array([1627801627, 1627803672], dtype=int64)
  7. # convert the column to unix format to compare
  8. (df.index - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')
  9. [out]:
  10. Int64Index([1627801627, 1627801628, 1627801629, 1627801630, 1627801631,
  11. 1627801632, 1627801633, 1627801634, 1627801635, 1627801636,
  12. ...
  13. 1627803663, 1627803664, 1627803665, 1627803666, 1627803667,
  14. 1627803668, 1627803669, 1627803670, 1627803671, 1627803672],
  15. dtype='int64', name='Time', length=2046)

第一个月第一个月第一个月

  1. fig, ax = plt.subplots(figsize=(25, 6))
  2. ax.scatter(x=df.index, y=df.A, color='red', marker='x')
  3. ticks2 = ax.get_xticks()
  4. print(ticks2)
  5. [out]:
  6. array([18840.29861111, 18840.30208333, 18840.30555556, 18840.30902778,
  7. 18840.3125 , 18840.31597222, 18840.31944444])
展开查看全部

相关问题