简体   繁体   中英

Plotting a 3D graph in Python

I have 3 vectors "x1", "x2" and "targets" of shape (1000,1) each and I want to plot this on a 3D graph.

While using the code

targets = targets.reshape(observations,)
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.plot(x1,x2,targets)
ax.set_xlabel("x1")
ax.set_ylabel("x2")
ax.set_zlabel("targets")
ax.view_init(azim=200)
plt.show()

I get the error: operands could not be broadcast together with remapped shapes [original->remapped]: (1000,) and requested shape (1000,1)

If I remove the code targets = targets.reshape(observations,) to remap the shape as (1000,1), I get the error: Line3D' object has no attribute '_verts3d'

To my understanding, plot requires 3 vectors with one dimension each. If you just extract one column of data from you arrays you be fine I think. See the example below:

import numpy as np
import matplotlib.pyplot as plt

x1 = np.random.random((1000,1))
x2 = np.random.random((1000,1))
targets = np.random.random((1000,1))

x1 = x1[:,0]
x2 = x2[:,0]
targets = targets[:,0]

# targets = targets.reshape(targets,)
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.plot(x1,x2,targets)
ax.set_xlabel("x1")
ax.set_ylabel("x2")
ax.set_zlabel("targets")
ax.view_init(azim=200)
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM