Here to ask a question about the basic matplotlib 3d plotting tools. Now, let me make some dummy data to explain my issue:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 1000)
y = x
X, Y = np.meshgrid(x, y)
f = np.exp(-(X**2 / 0.5) - Y**2 / 0.5)
I have a bunch of plots I want to stack side by side because they are connected by a parameter, here I'm just going to re-use the same data as it doesn't change much my problem. So my basic plot is appearing like this:
fig, ax = plt.subplots(1, 1, subplot_kw={"projection": "3d"})
for i in range(5):
ax.contourf(x, y, f, zdir="z", offset=i)
ax.view_init(elev=15, azim=45, roll=-90)
ax.set_zlim3d(-0.5, 3.5)
ax.set_box_aspect((3, 3, 7), zoom=1)
Where I see a nice plot on the side:
Now, since this graph is eventually going to be included in a larger figure, I tried to reduce the amount of white space around it. This is a question as old as matplotlib, apparently, unfortunately I wasn't able to find an easy solution, changing the aspect ratio helped covering the space in a nicer way. Now my issue: if I try to zoom, therefore change
ax.set_box_aspect((3, 3, 7), zoom=1)
to ax.set_box_aspect((3, 3, 7), zoom=2)
:
The plots only show up in the centre. Regardless the fact that this is too large of a zoom, aesthetically speaking, I'm confused as of why the plot that should appear at z=3 doesn't show up, and the one at 0 is cut. I can't add a video, but if I try and use the drag-zoom tool I can see that my data will only be shown in the centre of the figure (where the current figure is cut) and any attempt to move it around will end up with a similar result as this: only the centre of the 3d axis shows the data.
Now I'm wonder: why does this happen? Is this because of the implementation of 3d axes in matplotlib? Is there a reason why it's been coded this way? Can I do anything about it?
Thank you.
Try making the figure bigger (maybe (12, 6)
or so) and setting the clipping of every filled contour to False
:
# your code
fig, ax = plt.subplots(1, 1, figsize=(12, 6), subplot_kw={"projection": "3d"})
# your code
for i in range(5):
c = ax.contourf(x, y, f, zdir="z", offset=i, cmap=plt.cm.coolwarm)
c.set_clip_on(False)
# your code
The output for zoom=1
:
The output for zoom=2
: