我有格式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''
显示它是否为2D。例如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
答案 0 :(得分:5)
告诉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()
答案 1 :(得分:2)
Scatter期待一个数字。因此,您可以将日期转换为数字,如下所示:
y = [ (d-min(dates_formatted)).days for d in dates_formatted]
现在您可以将数据绘制为
pyplot.scatter(x, y)
对于3D情节,你可以试试这样的......
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
plt.ion()
x = [1,2,3,4]
z = [5,6,7,8]
dates = ['15/10/2016', '16/10/2016', "17/10/2015", "15/10/2014"]
dates_formatted = [pd.to_datetime(d) for d in dates]
y = [ (d-min(dates_formatted)).days for d in dates_formatted]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.scatter(x, y, z)
现在的y轴是天。您可以通过查找日期字符串并将其更改回来进行更改...
dt = [ pd.Timedelta(d) + min(dates_formatted) for d in ax.get_yticks()]
将这些转换为字符串......
dtStr = [d.isoformat() for d in dt]
然后把它们放回去
ax.set_yticklabels(dtStr)