Skip to content

Instantly share code, notes, and snippets.

@llllvvuu
Last active September 12, 2024 05:24
Show Gist options
  • Save llllvvuu/b96c5ab2b7d9d97c0c79eda0c6f65d48 to your computer and use it in GitHub Desktop.
Save llllvvuu/b96c5ab2b7d9d97c0c79eda0c6f65d48 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Set up the figure and 3D axis
fig = plt.figure(figsize=(16, 14))
ax = fig.add_subplot(111, projection='3d')
# Create the grid of x and y values
x = np.linspace(0.01, 1, 200) # Increased resolution
y = np.linspace(0.01, 1, 200)
X, Y = np.meshgrid(x, y)
# Create the two static surfaces
Z1 = X * Y # f(x, y) = xy
Z2 = X # f(x, y) = x
# Plot the static surfaces with subtle grid lines
surf1 = ax.plot_surface(X, Y, Z1, alpha=0.6, color='skyblue', edgecolor='royalblue',
linewidth=0.05, label='f(x,y) = xy')
surf2 = ax.plot_surface(X, Y, Z2, alpha=0.3, color='lightgreen', edgecolor='green',
linewidth=0.05, label='f(x,y) = x')
# Create and plot the hyperbolas
k_values = np.arange(0.1, 1.1, 0.1)
colors = plt.cm.rainbow(np.linspace(0, 1, len(k_values)))
for k, color in zip(k_values, colors):
x_hyp = np.linspace(k, 1, 1000) # x values from k to 1
y_hyp = k / x_hyp
z_hyp_k = np.full_like(x_hyp, k)
z_hyp_x = x_hyp
# Plot (x, k/x, k) hyperbola
ax.plot(x_hyp, y_hyp, z_hyp_k, color=color, linewidth=3, label=f'k = {k:.1f}')
# Plot (x, k/x, x) curve
ax.plot(x_hyp, y_hyp, z_hyp_x, color=color, linewidth=2)
# Add dotted mapping lines
for i in range(0, len(x_hyp), 50): # Adjust the step to control the number of lines
ax.plot([x_hyp[i], x_hyp[i]], [y_hyp[i], y_hyp[i]], [z_hyp_k[i], z_hyp_x[i]],
color=color, linestyle=':', alpha=0.7, linewidth=1)
# Set labels and title
ax.set_xlabel('X', fontsize=12, labelpad=10)
ax.set_ylabel('Y', fontsize=12, labelpad=10)
ax.set_zlabel('Z', fontsize=12, labelpad=10)
ax.set_title('exponentiation by U(0, 1) applied to f(x, y) = xy contourwise yields f(x, y) = x', fontsize=16)
# Set axis limits
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
# Adjust the view angle
ax.view_init(elev=20, azim=250)
# Add a legend
ax.legend(loc='upper left', bbox_to_anchor=(1.1, 1), fontsize=10)
# Adjust the layout to make room for the legend
plt.tight_layout()
# Show the plot
# plt.show()
# Uncomment the following line to save the figure as a png
plt.savefig('3d_graph_with_hyperbolas_and_subtle_grids.png', dpi=300, bbox_inches='tight')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment