我正在尝试绘制 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 代码,我会得到这个图像:
提前致谢!!!
在“数据”底部:
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()
关于python - 如何将日期时间设置为zaxis而不出现错误: OverflowError: Python int too large to convert to C long for 3D plot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60929013/