gpt4 book ai didi

python - matplotlib 3d 散点图日期

转载 作者:行者123 更新时间:2023-11-28 20:37:33 25 4
gpt4 key购买 nike

我有一个格式为 15/10/2017

的日期列表

我试过以下方法

from matplotlib import pyplot
import pandas as pd

dates = ['15/10/2016', '16/10/2016', "17/10/2015", "15/10/2014"]
dates_formatted = [pd.to_datetime(d) for d in dates ]
x = [1,2,3,4]
z = [5,6,7,8]

pyplot.scatter(x, dates_formatted, z)
pyplot.show()

它抛出错误 TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

它显示它是否是二维的。例如pyplot.scatter(x, dates_formatted)

我也尝试过以下

ax = Axes3D(fig)
ax = fig.add_subplot(111,projection='3d')
ax.scatter(x, dates_formatted, y)
pyplot.show()

它抛出错误Float() argument must be a string or number

最佳答案

告诉 matplotlib 如何将字符串转换为坐标系并不总是那么简单。为什么不简单地为轴设置自定义刻度标签?

import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure('scatter dates')
ax = fig.add_subplot(111, projection='3d')
dates = ['15/10/2016', '16/10/2016', "17/10/2015", "15/10/2014"]
dates_formatted = [pd.to_datetime(d) for d in dates ]
x = [1,2,3,4]
y = [9,10,11,12]
z = [5,6,7,8]

ax.scatter(x, y, z)
ax.xaxis.set_ticks(x)
ax.xaxis.set_ticklabels(dates_formatted)
plt.show()

enter image description here

关于python - matplotlib 3d 散点图日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42677160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com