I am trying to plot a simple 3D surface (just for testing). It appears to me that the surface does not correspond with the data matrix (defined as the sum of two input vectors using the Numpy add.outer()
function).
Here’s the Python code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import cm
import tkinter as tk
# Create root window
root = tk.Tk()
root.title("Test 3D")
root.geometry("1400x900")
X = [1, 2, 3, 4, 5]
f_X = [50, 30, 10, -5, -20]
Y = [1, 2, 3, 4, 5]
f_Y = [-15, -5, 15, 30, 40]
Z = np.add.outer(f_X, f_Y)
print(Z)
max_Z = np.max(Z)
# Find the index of the maximum Z value
max_index = np.unravel_index(np.argmax(Z, axis=None), Z.shape)
print("max Z=", max_Z)
print("Indices:", max_index)
# Plot the surface
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(X, Y, Z, cmap=cm.turbo, linewidth=0, antialiased=True)
ax.yaxis.set_major_locator(plt.MaxNLocator(10)) # Limits the number of ticks to 5
ax.set_xlabel("X-axis", linespacing=3.2)
ax.set_ylabel("Y-axis", linespacing=3.1)
max_canvas = FigureCanvasTkAgg(fig, master=root)
max_canvas.draw() # Render the graph
max_canvas.get_tk_widget().pack(fill='both', expand=True)
# Start the tkinter loop
root.mainloop()
The maximum value of Z equals 90, with indices X=0 (where X=50) and Y=4 (where Y=40). In the graph, the surface’s highest point is at X=4 and Y=4.
Peter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.