In the following code the points by the scatter plot become transparent, what makes it…
- User
In the following code the points by the scatter plot become transparent, what makes it so and how to make transparency opposite (so the large x,y values are less transparent)import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D from scipy.interpolate import griddata import seaborn as sns import random
Define the function for the surfacedef surface(x, y):
codereturn np.sin(x * y) + random.random()/100*2 # x2 + y2 ## Creating irregularly spaced datanp.random.seed(42) x = np.random.rand(100)1.5-0.5 y = np.random.rand(100)1.5-0.5 z = surface(x, y)# # Creating a regular grid
xi, yi = np.linspace(x.min(), x.max(), 100), np.linspace(y.min(), y.max(), 100)
xi, yi = np.meshgrid(xi, yi)# Creating a regular grid with fewer grid points
xi, yi = np.linspace(0, 1, 10), np.linspace(0, 1, 10) xi, yi = np.meshgrid(xi, yi)# Interpolating irregular data onto the regular grid zi = griddata((x, y), z, (xi, yi), method='linear')# Creating a 3D plot fig = plt.figure()
sns.set_style("white")# plt.rcParams['grid.axis'] = 'off' # Remove grid
plt.rcParams['axes.facecolor'] = 'white' # Remove gray backgroundax = fig.add_subplot(111, projection='3d')ax.zaxis.set_ticks_position("lower")
ax.grid(False) # Remove grid ax.set_axisbelow(False) # Ensure grid is behind plot ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([]) ax.xaxis.line.set_visible(False) ax.yaxis.line.set_visible(False) ax.zaxis.line.set_visible(False) ax.zaxis.pane.set_facecolor('#87CEEB') ax.yaxis.pane.set_facecolor('#ADD8E6') ax.xaxis.pane.set_facecolor('#F0FFFF')
ax.set_facecolor('white') # Set background color to white
ax.grid(False) # Remove grid# Plotting the 3D surface from irregular data using grid interpolation
ax.plot_surface(xi, yi, zi, cmap='viridis') # , edgecolor='k')
ax.view_init(elev=25, azim=-100)# Plot the surface without gridlines ax.plot_trisurf(xi.ravel(), yi.ravel(), zi.ravel(), cmap='viridis')# Evaluation points tokens = [0.1, 0.2, 0.4, 0.6, 0.8] sizes = [0.2, 0.4, 0.6] eval_points_x = [] eval_points_y = [] for size in sizes: eval_points_x += [size] * len(tokens) eval_points_y += tokenseval_color = "yellow" eval_points_x = np.array(eval_points_x) eval_points_y = np.array(eval_points_y) eval_points_z = surface(eval_points_x, eval_points_y)epsilon_error = 0.03 # shift points so scaling isn't perfect ax.scatter(eval_points_x, eval_points_y, eval_points_z - epsilon_error, marker='o', s=100, label='Evaluation Points', color=eval_color)
Assistant
The transparency of the scatter plot points in your code is caused by their z-coordinate values. In 3D plots, matplotlib automatically applies a depth-dependent alpha (transparency) to points, making those further away (with lower z values) more transparent. To reverse this effect so that points with larger x, y values are less transparent, you can manually set the alpha values for each point based on their x and y coordinates. Here's how you can modify your code to achieve this:
Calculate alpha values based on x and y coordinates. Use these alpha values in the scatter plot.
Here's the modified part of your code: python# Calculate alpha values based on x and y coordinates max_coord = max(eval_points_x.max(), eval_points_y.max()) alpha_values = 1 - (eval_points_x + eval_points_y) / (2 * max_coord)