I am trying to plot my data in a 3d graph with X, Y and Z coordinates with matlopyplot in python as I thought it would be fun and cool and didn’t realize how challenging it would be (but I am not giving up!).
I found many tutorials for plotting functions in 3d but not as much plotting of actual data. I keep receiving the error shape mismatch: objects cannot be broadcast to a single shape
even though I have the correct number of x y and z data (192, 6, 32). After reading on some Q and A websites I learned that I need to change my data to be 2d arrays I believe in order that the library functions can know how the x y and z data correspond together and I tried creating a meshgrid of the x and y functions as suggested. I tried multiple ways but I am still receiving the same error as before.
Does anyone have anymore insights or suggestions and perhaps a better understanding of how this works? Thanks so much for any help you can provide!
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
df = pd.read_csv('testcsv.csv')
df2 = [df.loc[:,'0':'8']] #how to append all as 1d list?
#x= df2.values.tolist()
x=np.array(np.linspace(0, 1, 32*6)) #using this as placeholder for x data
y=np.array([0,0.5, 1,2, 4, 8])
z= np.array(df.loc[:,'Elapsed'].values.tolist()) #32 values here
fig = plt.figure()
# syntax for 3-D projection
ax = plt.axes(projection ='3d')
z=np.tile(z, (len(z), 1))
x, y = np.meshgrid(x, y)
ax.scatter(x, y, z)
#commented out code includes other methods I tried
# including reshaping z based on the dimensions of x
# create matrix for z values
#dim = int(np.sqrt(len(x_data)))
#z = z_data.reshape((dim, dim))
# create matrix for the x and y points
#x, y = np.arange(0, dim, 1), np.arange(0, dim, 1)
# plotting
ax.plot3D(x, y, z, 'green')
ax.set_title('title')
plt.show()
I don't have a solution, but a suggestion that you try the plotly package. It makes sense to make 3D plots that are interactive, as projecting on a static 2D page can give misleading perspective. Start by repeating the example here and then substitute your own data.