You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Docker容器的IPython环境中渲染并显示Matplotlib图表?

Fixing Matplotlib Plot Display in Docker Container via VSCode Attach Shell

Got it, let's break down why your matplotlib plots aren't showing up and walk through the fixes that work for your setup:

Root Cause

The core issue is that your Docker container doesn't have access to your host machine's graphical display system. The default agg backend is a non-interactive, headless renderer—it can generate plot data but can't pop up a window. Even %matplotlib inline won't work in a raw shell session because it relies on a frontend (like Jupyter's browser interface or an IDE's rendering panel) that isn't present in a basic bash shell attached to the container.

Solutions

1. Allow Container to Access Host's X11 Display (Linux/macOS)

This lets the container use your host's graphical system to pop up matplotlib windows directly.

For Linux:

  • First, on your host machine, grant X11 access to Docker containers temporarily:
    xhost +
    
    (Run xhost - afterward to lock down X11 access again for security.)
  • Modify your Docker run command to mount the X11 socket and set the DISPLAY environment variable:
    docker run --rm -it -p 8888:8888 \
    --mount type=bind,source=/project,target=/work \
    --mount type=bind,source=/tmp/.X11-unix,target=/tmp/.X11-unix \
    -e DISPLAY=$DISPLAY \
    python-3.9.1-jupyterlab
    
  • Attach the shell again, then switch to an interactive backend in IPython:
    %matplotlib tk
    plt.plot([1.6, 2.7])
    plt.show()
    
    You should see a plot window pop up on your host desktop.

For macOS:

  • First, install XQuartz (required for X11 support on macOS).
  • Open XQuartz, then run this in a host terminal to grant local access:
    xhost +localhost
    
  • Modify your Docker run command to point to the host's X11 display:
    docker run --rm -it -p 8888:8888 \
    --mount type=bind,source=/project,target=/work \
    -e DISPLAY=host.docker.internal:0 \
    python-3.9.1-jupyterlab
    
  • Follow the same IPython steps above to switch to tk backend and show the plot.

2. Use VSCode's Jupyter Extension (Best for IDE Workflow)

Since you're already using VSCode, skip the shell attach entirely and connect directly to the container's Jupyter server—this lets plots render natively in VSCode's interface.

  • Start your container as usual, then copy the Jupyter access URL with token from the container's output (looks like http://127.0.0.1:8888/?token=xxxxxx).
  • In VSCode, open the Jupyter extension via the sidebar icon.
  • Click Connect to Jupyter Server → select Existing → paste the URL you copied.
  • Create or open an .ipynb file in VSCode (you can access your /project files via the mounted /work directory).
  • Run plot code—%matplotlib inline will work natively, and plots will display directly below the code cell in VSCode.

3. Save Plots to File (Quick Workaround)

If you just need to view plots without graphical access, save them to the mounted directory and open them on your host:

In IPython:

plt.plot([1.6, 2.7])
plt.savefig('/work/plot.png')

The file will appear in your host's /project directory—open it with any image viewer.

内容的提问来源于stack exchange,提问作者Jeff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 08:47:32