Matplotlib: display plot on a remote machine

后端 未结 8 1483
天涯浪人
天涯浪人 2020-11-28 19:45

I have a python code doing some calculation on a remote machine, named A. I connect on A via ssh from a machine named B. Is there a way to display the figure on

相关标签:
8条回答
  • 2020-11-28 20:17

    if that doesn't work you could also try:

    import matplotlib.pyplot as plt
    plt.switch_backend('agg')
    

    or

    import matplotlib.pyplot as plt
    plt.switch_backend('TkAgg')
    

    this seemed to work for me

    Yet, if you are trying to get a GUI working I suggest you look at this link: http://fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/

    0 讨论(0)
  • 2020-11-28 20:23

    GTK seems impossible to get working on Ubuntu with Python3. Instead, I used tkagg (from this answer):

    import matplotlib
    matplotlib.use('tkagg')
    import matplotlib.pyplot as plt
    

    Test that it's working with this:

    import matplotlib
    matplotlib.use('tkagg')
    import matplotlib.pyplot as plt
    plt.plot([1, 2, 3])
    plt.show()
    
    0 讨论(0)
  • 2020-11-28 20:31

    I have used IPython to solve the related problem. The steps are as follows:

    Step 1: Install IPython and Jupyter in the remote machine (A) locally (assuming no root privilege) using the following commands:

    pip install --user ipython
    pip install --user jupyter 
    

    Update matplotlib:

    pip install --user -U matplotlib
    

    Step 2:

    Run Jupyter with no browser from the code directory in the remote machine (A):

    cd PATH/TO/THE/CODE
    jupyter notebook --no-browser --port=8080
    

    After this command, a URL will be given something similar to below:

    http://localhost:8080/?token=5528ab1eeb3f621b90b63420f8bbfc510edf71f21437c4e2

    Step 3:

    Now open another terminal in the local machine (B) and connect to the remote machine (A) using ssh:

    ssh -N -L 8080:localhost:8080 user_id@remote.host
    

    The port number has to be same in step 2 and step 3. In this example, the port number is 8080.

    Step 4:

    Copy and paste the URL in the step 3 to a browser in your local machine (B).

    Now, the notebook in the remote machine can be used through the browser and plot can be generated using the data in the remote machine.

    0 讨论(0)
  • 2020-11-28 20:32

    export MPLBACKEND="agg" this worked for me. obviously you can set it via code as well.

    0 讨论(0)
  • 2020-11-28 20:36

    Just wanted to add - if you're on Windows as the local machine, make sure you've set up Xming (an X Windows server) and Putty so you can see the remote Linux graphical applications.

    I followed the instructions from here: http://laptops.eng.uci.edu/software-installation/using-linux/how-to-configure-xming-putty to do this. It also sets your display environment and variable so you don't get an error when using tkagg as the backend.

    0 讨论(0)
  • 2020-11-28 20:40

    Sure, you can enable X11 forwarding. Usually this is done by passing the -X or -Y option to ssh when you connect to the remote computer

    ssh -X computerA
    

    Note that the SSH daemon on computer A will also have to be configured to enable X11 forwarding. This is done by putting

    X11Forwarding yes
    

    in computer A's sshd_config configuration file.

    If computer A's SSH daemon does not have X11 forwarding enabled, you can always have Python write the result of the calculation to a text file, download it to computer B, and use Matplotlib locally.

    0 讨论(0)
提交回复
热议问题