Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将datetime设置为zaxis而不出错:Overflower错误:Python int太大,无法转换为C long for 3D绘图_Python_Datetime_Mplot3d - Fatal编程技术网

如何将datetime设置为zaxis而不出错:Overflower错误:Python int太大,无法转换为C long for 3D绘图

如何将datetime设置为zaxis而不出错:Overflower错误:Python int太大,无法转换为C long for 3D绘图,python,datetime,mplot3d,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('

我正在尝试绘制一个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>


发生溢出错误的原因是Matplotlib的DateFormatter无法直接打印
np.datetime64
数据,您的数据也应该如此。您需要显式地将日期转换为
datetime.date
对象

请看一下这个:


发生溢出错误的原因是Matplotlib的DateFormatter无法直接打印
np.datetime64
数据,您的数据也应该如此。您需要显式地将日期转换为
datetime.date
对象

请看一下这个:

编辑: 这可能对你有用

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

输出:

您好,感谢您的快速回复。但是,如果我将对象转换为
df85['Datum']=df85.Datum.astype('O')
。当我运行绘图代码时,我得到了
TypeError:不支持的操作数类型*:“float”和“Timestamp”
你能告诉我你的
df85['Datum']
看起来是什么样子吗?嗨,是的,刚刚有问题。感谢没有适当的上下文和代码,很难识别问题。但是,我附加了一个代码片段,试图生成一个类似的问题。这可能对你有用。嗨,谢谢。我会看一看,如果数据不起作用,我会看看是否可以上传数据。非常感谢。
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()