2

I am having a very similar issue to this post and this post, however I was not able to get the solution from the first post to work, and the second went unanswered. I would also like to avoid having to change the actual MatPlotLib libraries, as I am planning on sharing my code and want to be able to simply import the library unedited (the first post suggests changing the matplotlib library itself). My problem is that when making 3D plots in Matplotlib, I am finding that there is an invisible margin which begins to cut off my actual rendered data. This can be seen in the plot below, where the red data is clearly cut off on the right, and the magenta data is beginning to be cut off on the left):

Matplotlib 3D plot with data being cut off

Is it possible to extend these invisible margins? The actual axes on which the plot sits render correctly, but it is the data itself that begins to be cut off.

I have exhausted just about every StackOverflow post concerning this topic and have not been able to find anything that solves the problem. Here is some simple code which will recreate the problem I am having:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

x_data = range(0,6)
y_Data = [10,10,10,10,10,10]

for i in range(6):
    ax.bar(x_data, y_Data, zs=i, zdir='y', color="r", alpha=0.8)

ax.set_box_aspect((1,6,1))
ax.dist = 8
ax.elev = 23
ax.azim = 31

plt.show()

Image showing recreated problem

Schoppe
  • 33
  • 5

2 Answers2

1

changing ax.dist = 8 to ax.dist = 9 actually works? This what you looking for? I guess it's issue with camera angle.

import matplotlib.pyplot as plt
fig = plt.figure()

ax = fig.add_subplot(projection='3d')

x_data = range(0,6)
y_Data = [10,10,10,10,10,10]

for i in range(6):
    ax.bar(x_data, y_Data, zs=i, zdir='y', color="r", alpha=0.8)

ax.set_box_aspect((1,6,1))
ax.dist = 9
ax.elev = 23
ax.azim = 31

plt.autoscale()
plt.show()

Gives #

enter image description here

  • Hi @Bhargav. I agree this is a solution, however it is not an ideal one. There are certain situations where I need the distance to be a certain value, which then causes the clipping. An ideal solution would change the instances of the invisible margins, making them wider and allowing the plot to be correctly rendered at any distance. Thank you for you help though. – Schoppe Dec 08 '22 at 17:50
  • Yeah agree it's just a quick fix – Bhargav - Retarded Skills Dec 08 '22 at 18:10
1

After months of searching I was finally able to answer this question and it was far easier than I had anticipated. It turns out there is an argument within ax.bar called "clip_on", which if set to False will stop this problem. See updated code fragment below and resultant plot:

import matplotlib.pyplot as plt

fig = plt.figure() 
ax = fig.add_subplot(projection='3d')

x_data = range(0,6) 
y_Data = [10,10,10,10,10,10]

for i in range(6):
    ax.bar(x_data, y_Data, zs=i, zdir='y', color="r", alpha=0.8, clip_on=False)

ax.set_box_aspect((1,6,1)) 
ax.dist = 8 
ax.elev = 23 
ax.azim = 31

plt.show()

enter image description here

Schoppe
  • 33
  • 5