Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib 3d scatter plot date

I have a list of dates in format 15/10/2017

I have tried the following

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

It throws an error 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''

It shows if it is 2D. e.g. pyplot.scatter(x, dates_formatted)

I have also tried the following

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

It throws an error Float() argument must be a string or number

like image 243
Gavin Avatar asked Oct 27 '25 08:10

Gavin


1 Answers

It's not always trivial to tell matplotlib how to translate strings into a coordinate system. Why not simply set custom tick labels for the axes?

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

like image 154
Vlas Sokolov Avatar answered Oct 28 '25 20:10

Vlas Sokolov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!