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

如何在Ubuntu环境下的Docker容器中查看Matplotlib生成的绘图?

Solutions to View Matplotlib Plots & CNN Outputs in Docker for Your Kaggle Segmentation Task

Hey there! I’ve been in your exact situation before—trying to debug matplotlib plots inside a Docker container can be tricky when you’re just starting out. Let’s break down a few solid solutions to get those CNN filters and segmentation results visible:

1. Save Plots to Files (Simplest Approach)

Since Docker containers typically don’t have a graphical interface, plt.show() won’t display anything. Instead, save your plots directly to files and access them from your host machine:

  • Modify your code: Replace any plt.show() calls with plt.savefig() to save images to a specific directory. For example, to save CNN filter visualizations:
    # Example: Save a single filter plot
    plt.figure(figsize=(8, 8))
    plt.imshow(filter_data, cmap='gray')
    plt.title('CNN Filter Layer 1')
    plt.savefig('/container_plots/filter_layer_1.png')  # Use a path inside the container
    
    # For multiple filters, loop through them and save each with a unique name
    for i, filter in enumerate(filters):
        plt.figure(figsize=(4,4))
        plt.imshow(filter, cmap='gray')
        plt.savefig(f'/container_plots/filter_{i}.png')
    
  • Mount a host directory to Docker: When starting your container, use the -v flag to link a folder on your Ubuntu host to the directory you’re saving plots to in the container. This way, saved files will show up directly on your host:
    docker run -v /home/your_username/kaggle_plots:/container_plots your_docker_image_name
    
    Now you can open the /home/your_username/kaggle_plots folder on your Ubuntu machine to view all saved plots.

2. Forward X11 to Display Plots on Your Host Screen

If you want to see plots in real-time (like a normal plt.show() window), use X11 forwarding to send the container’s graphical output to your host:

  • Allow X11 access on your host: Run this command in your Ubuntu terminal first (it temporarily grants local root access to your X server; you can run xhost -local:root later to revoke it):
    xhost +local:root
    
  • Start the container with X11 settings: Launch your container with environment variables and volume mounts to connect to your host’s X server:
    docker run -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix your_docker_image_name
    
  • Install required dependencies (if missing): If your container doesn’t have the necessary graphical libraries, install them inside the container:
    apt-get update && apt-get install -y libgtk2.0-0
    
    Now when your code runs plt.show(), the plot window will pop up on your Ubuntu host’s screen.

3. Use TensorBoard for Advanced Model Visualization

For a more robust way to inspect CNN filters, segmentation outputs, and training metrics, use TensorBoard (works with TensorFlow, PyTorch, and other frameworks):

  • Add TensorBoard logging to your code: For example, in PyTorch, use SummaryWriter to log images:
    from torch.utils.tensorboard import SummaryWriter
    
    # Initialize writer
    writer = SummaryWriter('/tensorboard_logs')
    
    # Log a CNN filter
    writer.add_image('Layer 1 Filter 0', filter_data, global_step=0)
    
    # Log segmentation output
    writer.add_image('Segmentation Result', segmentation_output, global_step=epoch)
    
  • Map TensorBoard port when starting the container: Forward the container’s port 6006 (default TensorBoard port) to your host:
    docker run -p 6006:6006 your_docker_image_name
    
  • Access TensorBoard from your host: After running your code, open a browser on your Ubuntu machine and go to http://localhost:6006 to view all your visualizations.

Quick Tip to Prevent Instant Exit

If your code exits immediately after running, add a pause at the end to keep the container running (so you can check logs or files):

# Add this at the end of your script
input("Press Enter to exit the container...")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 20:17:49