2

I am trying to make a 3D scatter plot that has the following axis: Price, Time and Dates. Essentially I want to plot the price vs the time for a single day and then do that for multiple days and stack them on an axis.

Here is the code I have so far:

 df=pd.read_csv("C:\\Desktop\\dat.csv")
 date=  df['date'].unique()
 dfd = pd.DataFrame(date)
 days= dfd.sort_values(0)

 fig= plt.figure()
 ax= fig.add_subplot(111, projection= '3d')

 xx = np.arange(len(days[0]))
 ys = [i+xx+(i*xx)**2 for i in range(len(days[0]))]
 colors = cm.rainbow(np.linspace(0, 1, len(ys)))

 for d,cc in zip(days[0],colors):
 
     td= df.loc[df['date'] == d]
     st= td[['time','price']]

     x = st['time']         
     y = st['price']

     x=list(x)
     y=list(y)
     ax.scatter(x,y,d,color=cc)

 plt.show()

The main problem is that x is a list with times and that d is a date. when I run the code matplotlib throws an error saying

ValueError: invalid literal for float(): 9:30:01

The dates are in the following format : 2017.04.12, time format : 9:30:01

I am aware that I can set up tick labels for the date, but how can I handle the time?

How can I plot this 3d plot if the axis indexes contain dates and times?

4
  • 1
    matplotlib 3D cannot plot dates. A possible solution is given here: stackoverflow.com/questions/2195983/… Commented Apr 12, 2017 at 22:13
  • 1
    Possible duplicate of matplotlib 3d scatter plot date Commented Apr 12, 2017 at 22:14
  • These question help with the dates, but they do not explain how to convert the list of time strings to a format that matplotlib can use. Commented Apr 12, 2017 at 22:49
  • There is no difference between a date and a time. You may edit your question to show where your specific problem of converting a time to a number is. (Make sure to provide a minimal reproducible example without any external data, such that people can run your code.) Commented Apr 12, 2017 at 22:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.