Issue
I have the following code which produces two plots in matplotlib.
However, each I'm unable to remove this border in the second subplot despite feeding all these arguments.
How can I remove the black border around the second subplot (see attached image)
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
beta, gamma = np.linspace(-np.pi / 2, np.pi / 2, 500), np.linspace(-np.pi / 2, np.pi / 2, 500)
B, G = np.meshgrid(beta, gamma)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
# 2D Contour plot
ax1.imshow(obj_vals.T, origin='lower', cmap='hot', extent=(-np.pi/2, np.pi/2, -np.pi/2, np.pi/2))
ax1.set_xlabel(r'$\gamma$')
ax1.set_ylabel(r'$\beta$')
ax1.set_xticks([])
ax1.set_yticks([])
ax2 = fig.add_subplot(122, projection='3d')
# Make panes transparent
ax2.xaxis.pane.fill = False # Left pane
ax2.yaxis.pane.fill = False # Right pane
ax2.zaxis.pane.fill = False # Right pane
# Remove grid lines
ax2.grid(False)
# Remove tick labels
ax2.set_xticklabels([])
ax2.set_yticklabels([])
ax2.set_zticklabels([])
# Transparent spines
ax2.xaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
ax2.yaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
ax2.zaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
ax2.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax2.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# No ticks
ax2.set_xticks([])
ax2.set_yticks([])
ax2.set_zticks([])
# Surface plot
surf = ax2.plot_surface(B, G, obj_vals.T, cmap='hot')
plt.axis('off')
plt.tight_layout()
plt.show()
Solution
I think the issue here is that you are creating a subplot with two 2D axes and then adding a 3D axis over the right 2D one. You have removed the splines for the 3D plot, but the original 2D plot that you originally made is still there. I think a better way to do this would be to create the figure and then the subplots individually, like what is shown on this documentation page.
Then, you can also remove all the 3D axes components using ax.set_axis_off()
, as per this answer.
import matplotlib.pyplot as plt
import numpy as np
plt.close("all")
x = np.linspace(-2*np.pi, 2*np.pi, 100)
y = np.linspace(-2*np.pi, 2*np.pi, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X)*np.cos(Y)
fig = plt.figure(figsize=(12,6))
ax1 = fig.add_subplot(1, 2, 1)
ax1.contourf(X, Y, Z, levels=100, cmap="hot")
ax1.set_xlabel(r'$\gamma$')
ax1.set_ylabel(r'$\beta$')
ax1.set_xticks([])
ax1.set_yticks([])
ax2 = fig.add_subplot(1, 2, 2, projection="3d")
ax2.plot_surface(X, Y, Z, cmap="hot", rstride=1, cstride=1)
ax2.set_axis_off()
plt.tight_layout()
plt.show()
Answered By - jared
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.