You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pandas/Matplotlib绘制次坐标轴折线时出现异常内存错误

绘图时添加次坐标轴引发内存错误的复现问题

在Jupyter Notebook中执行代码给次坐标轴添加折线时,触发如下内存错误:

MemoryError: Unable to allocate 48.2 GiB for an array with shape (1726364447,) and data type [('val', '<i8'), ('maj', '?'), ('min', '?'), ('fmt', 'S20')]

执行的代码如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

np.random.seed(0)

t = pd.Timestamp('2024-09-15 07:02:04')
times = []
for i in range(101):
    times.append(t)
    t = t + pd.Timedelta(f'{np.random.randint(7, 10)}s')

data1 = pd.Series(
    np.random.normal(size=101), 
    index=pd.DatetimeIndex(times),
    name='Data 1'
)

data2 = pd.Series(
    [5.67, 5.85, 5.78], 
    index=pd.DatetimeIndex(["2024-09-15 07:03:39", "2024-09-15 07:08:43", "2024-09-15 07:13:47"])
)

fig, ax = plt.subplots(figsize=(7, 2.5))
data1.plot(ax=ax, style='.-')
data2.plot(ax=ax, style='.-', secondary_y=True)
ax.grid()
plt.show()

仅对数据做微小修改(比如把data2最后一个点的时间从"2024-09-15 07:13:47"改成"2024-09-15 07:12:47"),绘图就能正常完成。注意两个数据集的时间索引均为非均匀间隔。不确定该问题是否与已知的类似问题相关,想问问其他用户能否复现这个错误?

使用版本信息:

  • Python 3.10.12
  • pandas 2.1.2
  • matplotlib 3.8.2
  • numpy 1.26.1

内容的提问来源于stack exchange,提问作者Bill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 15:17:41