1

I have the following code:

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

      ax.scatter(t,p,d,color=cc)

t and p are lists (time and price) and d is an integer (day). When I run the code the result I get is below :

enter image description here

The issue is that the axis are wrong. p and d need to be swapped but when I try to do:

ax.scatter(t,d,p)

I get an error saying "Arguments xs and ys must be of same size". Is there any way I can just get the axis to be switched since intuitively the plot does not make sense in this configuration.

The reason that the days are iterated over is so that I can have a separate color for each day on the plot.

I tried the solution of iterating through the t and p lists for each day and just plotting individual corresponding t,d,p points, However that is much slower and afterwards the matplotlib plot is unresponsive if you try to move it.

1 Answer 1

1

I'm not sure why you are getting an error message, but can you give a sample of your data? The following code works fine, and produces the type of plot you are asking for.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Generate some dummy data
time = np.random.rand(100)
price = 120+10*np.random.rand(100)
day = np.random.randint(0,10,100)


# Plot data
fig = plt.figure(figsize=(12,4))

ax = fig.add_subplot(121, projection='3d')
ax.scatter(time, price, day)
ax.set_xlabel('time')
ax.set_ylabel('price')
ax.set_zlabel('day')

ax = fig.add_subplot(122, projection='3d')
ax.scatter(time, day, price)
ax.set_xlabel('time')
ax.set_ylabel('day')
ax.set_zlabel('price')

fig.show()

enter image description here

Edit:

You can set the colour of the points in a scatter plot by passing a list/array. If we plot the second scatter plot using:

ax.scatter(time, day, price, c=day)

We get: enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

Hello, thank you for the help, I forgot to include an important detail, but I edited the post to add it. basically in your example day is a list, however in my case since I would like to have different colors set for each day, so i iterate over the list doing it day by day. @Robbie
You can colour the points in a scatter plot by passing a list as the colour argument, see my edit. Is this what you want? Also, is your day variable a list too?
There is still an issue since in your example day is a 100 element list , so it matches the size of the price and time. my day list is just a list that goes from 0-12 .
Basically I have a list with times that has length n and a list for price which is the same length as time (since the price corresponds to time) and a list with days which is only has 12 elements.
days are essentially independent of each other
|

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.