Mustard Tiger
Mustard Tiger

Reputation: 3671

Python, changing axis of 3D plot

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.

Upvotes: 1

Views: 4583

Answers (1)

Robbie
Robbie

Reputation: 4882

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

Upvotes: 1

Related Questions