3
$\begingroup$

After running gradient descent I have three arrays theta1 theta2 and J all three of size num_iterationx1. I tried plotting contours using the contour function in Axes3D and 3d surface plot using surf but there is always a dimension error or no figure gets plotted.

Can anyone tell me how to plot this. In python

$\endgroup$
1
$\begingroup$

Try using a trisurf plot! It is very simple to get a nice surface plot.

Have a look at this Matplotlib 3d documentation that includes many examples.

Here is one of the examples, where I additionally print the size of the three arrays used to plot. We see that they are all single vectors of the same length, therefore your data should just work fine using the code below:

enter image description here

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


n_radii = 8
n_angles = 36

# Make radii and angles spaces (radius r=0 omitted to eliminate duplication).
radii = np.linspace(0.125, 1.0, n_radii)
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)

# Repeat all angles for each radius.
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)

# Convert polar (radii, angles) coords to cartesian (x, y) coords.
# (0, 0) is manually added at this stage,  so there will be no duplicate
# points in the (x, y) plane.
x = np.append(0, (radii*np.cos(angles)).flatten())
y = np.append(0, (radii*np.sin(angles)).flatten())

# Compute z to make the pringle surface.
z = np.sin(-x*y)

# Inspect the array shapes
print(x.shape)                    # gives: (289,)
print(y.shape)                    # gives: (289,)
print(z.shape)                    # gives: (289,)

# Create the plot and show it

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)

plt.show()
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.