python - 如何将日期时间设置为zaxis而不出现错误: OverflowError: Python int too large to convert to C long for 3D plot

标签 python datetime mplot3d

我正在尝试绘制 3D 图像,其中 z 是时间。 当我尝试将 zaxis 标签设置为“年、月”时,我收到错误。

为此:

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.scatter3D(df85['Messwert'], df85['average_cux'], df85['Datum'],c=df85['code'], cmap="jet_r")
ax.zaxis.set_major_formatter(dates.DateFormatter('%Y-%M'))

我收到此错误:


OverflowError: Python int too large to convert to C long

<Figure size 432x288 with 1 Axes>

如果没有设置 zaxis 代码,我会得到这个图像:

enter image description here

提前致谢!!!

enter image description here

在“数据”底部:

Name: Datum, Length: 81, dtype: object

最佳答案

发生溢出错误是因为Matplotlib的DateFormatter无法直接绘制np.datetime64数据,这应该是您的数据的情况。您需要将日期显式转换为 datetime.date 对象。

请看一下这个: https://matplotlib.org/3.1.1/gallery/recipes/common_date_problems.html

发生溢出错误是因为Matplotlib的DateFormatter无法直接绘制np.datetime64数据,这应该是您的数据的情况。您需要将日期显式转换为 datetime.date 对象。

请看一下这个: https://matplotlib.org/3.1.1/gallery/recipes/common_date_problems.html

编辑: 这可能对您有用。

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates import date2num, DateFormatter
import numpy as np
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
from datetime import datetime, timedelta

# a test dataframe
df = pd.DataFrame({
    'date': np.array([str(datetime(2020,3,30).date()+timedelta(x+1))+' 00:00:00' for x in range(200)], dtype='object'),
    'sales': np.random.randint(low=1, high=200, size=200),
    '%sales in US' : 30 * np.random.random_sample(size=200) + 20
})

# appropriate type conversions
df['date']= pd.to_datetime(df['date'])
df['date'] = df['date'].apply(date2num)

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.scatter3D(df['sales'], df['%sales in US'], df['date'], c=df['date'], cmap="jet_r")
ax.zaxis.set_major_formatter(matplotlib.dates.DateFormatter('%Y-%M'))
plt.xlabel('sales', fontsize=20)
plt.ylabel('%sales in US', fontsize=16)
ax.set_zlabel('date', fontsize=16) 
plt.show()

输出:https://imgur.com/a/WXM07it.jpg

关于python - 如何将日期时间设置为zaxis而不出现错误: OverflowError: Python int too large to convert to C long for 3D plot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60929013/

相关文章:

python - 如何使用 Python 展平这样的数组?

php - 如何在HUMhub开源社交网站中创建自定义模块?

python - 无法从 patsy 导入 dmatrices

datetime - Kendo UI Scheduler 错误地转换时间、向后添加和减去小时

python - Scrapy设置可以使用custom_settings,但在settings.py中不起作用

php - SQL 查询返回错误的时间戳

datetime - Coldfusion 版本返回不同的查询结果

python - Spyder 终端内的 3D matplotlib 图

python - 使用 mplot3d 绘制二维数组

python - Pylab 或 mplot3d : index as marker