marimo-team/marimo

matplotlib: `zlabel` get cut out of the figure

Closed this issue · 2 comments

Describe the bug

I can add zlabel to a 3D plot, but marimo will cut the figure, leaving the zlabel invisible.

Result in marimo (easy to read in the notebook, but some information is missing):
Image

Result of savefig (kind of ugly, but acceptable):
Image


I think the reason is bbox_inches="tight". If can reproduce marimo's result with savefig(…, bbox_inches="tight").

artist.figure.savefig(buf, format="png", bbox_inches="tight") # type: ignore

bbox_inches of matplotlib.figure.Figure.savefig — Matplotlib 3.9.2 documentation:

Bounding box in inches: only the given portion of the figure is saved. If 'tight', try to figure out the tight bbox of the figure.

Could we provide a way to overwrite this bbox_inches="tight"?

Environment

{
  "marimo": "0.9.10",
  "OS": "Windows",
  "OS Version": "10",
  "Processor": "******",
  "Python Version": "3.12.5",
  "Binaries": {
    "Browser": "--",
    "Node": "v20.9.0"
  },
  "Dependencies": {
    "click": "8.1.7",
    "importlib-resources": "missing",
    "jedi": "0.19.1",
    "markdown": "3.7",
    "pygments": "2.18.0",
    "pymdown-extensions": "10.10.2",
    "ruff": "0.6.8",
    "starlette": "0.39.1",
    "tomlkit": "0.13.2",
    "typing-extensions": "missing",
    "uvicorn": "0.30.6",
    "websockets": "12.0"
  },
  "Optional Dependencies": {}
}

Code to reproduce

The above figures are generated from Text annotations in 3D — Matplotlib 3.9.2 documentation + marimo convert. The key line is ax.set_zlabel('Z axis').

Full example on the matplotlib website
import matplotlib.pyplot as plt

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

# Demo 1: zdir
zdirs = (None, 'x', 'y', 'z', (1, 1, 0), (1, 1, 1))
xs = (1, 4, 4, 9, 4, 1)
ys = (2, 5, 8, 10, 1, 2)
zs = (10, 3, 8, 9, 1, 8)

for zdir, x, y, z in zip(zdirs, xs, ys, zs):
    label = '(%d, %d, %d), dir=%s' % (x, y, z, zdir)
    ax.text(x, y, z, label, zdir)

# Demo 2: color
ax.text(9, 0, 0, "red", color='red')

# Demo 3: text2D
# Placement 0, 0 would be the bottom left, 1, 1 would be the top right.
ax.text2D(0.05, 0.95, "2D Text", transform=ax.transAxes)

# Tweaking display region and labels
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_zlim(0, 10)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

plt.show()

hey @YDX-2147483647 , I actually couldn't make this work in Jupyter as well. Were you able to? 🤔
I see some guides online to modify the figure config stack overflow

Thank you for sharing the SO link. 😃

couldn't make this work in Jupyter as well. Were you able to?

I've just tried, and it does not work either.


So it should be a matplotlib issue, and I find matplotlib/matplotlib#28117. I'll close this issue now.