3

I've googling around for days now to solve a presumably easy problem: I want to generate a 3D plot with matplotlib. This works nicely, and I can rotate the plot in the right position.

fig = plt.figure(figsize=(13,6), facecolor="white")

ax1 = plt.subplot2grid((3,3), (0,0), colspan=2, rowspan=2, projection="3d")



def cc(SpectrumType):
    if SpectrumType == "Normal":
        return "tab:gray"
    if SpectrumType == "Anomaly":
        return mcolors.to_rgba("r", alpha=1)   


# X Axis : equally space from 0 to 10
xs = np.arange(0, 20, 0.4)

verts = []

# 3 D axis: draw 4 plots at points 0, 1, 2, 3
zs = [0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0]

afactor = 1
for z in zs:
    afactor = 1
    if z==10.0:
        afactor = 5
    ys = afactor*np.random.rand(len(xs))
    ys[0], ys[-1] = 0, 0
    verts.append(list(zip(xs, ys)))



poly = PolyCollection(verts)
poly.set_alpha(0.5)

ax1.add_collection3d(poly, zs=zs, zdir='x')

ax1.set_xlabel('X')
ax1.set_xlim3d(-1, 21)
ax1.set_ylabel('Y')
ax1.set_ylim3d(0, 20)
ax1.set_zlabel('Z')
ax1.set_zlim3d(0, 5)
ax1.set_title("Plot 1")

ax1.view_init(8,-82) 
ax1.get_proj = lambda: np.dot(Axes3D.get_proj(ax1), np.diag([0.3, 1, 0.3, 
0.3]))


plt.tight_layout(pad=0)#, w_pad=0.5, h_pad=5.0)
fig.subplots_adjust(top=1, bottom=0, left=0, right=1)
fig = plt.gcf()
plt.legend(loc='upper right')#, bbox_to_anchor=(0.5, 0.5))
plt.show()

Generated 3D plot:

enter image description here

PROBLEM: There is always white space on the left side of the plot. This drives me crazy! I want to add this 3D as a subplot to a grid, but this white space makes the entire figure look really ugly. Please help ! How do I remove this white space on the left?

Thank you very much!

Venkatesh Wadawadagi
  • 2,793
  • 21
  • 34
fabioloso
  • 33
  • 3

0 Answers0