Coder Social home page Coder Social logo

remote_plot's Introduction

Remote Plot

PyPI version

Developing python code on a remote machine can get frustrating when you want to visualize your results. Instead of relying on a remote screen / X11 forwarding / VNC, Remote Plot opens your plots in a local server. It's a super simple library that replicates the API of matplotlib with a web renderer. All you need to do is forward the port to your machine. If your using VS Code, it will take care of it for you.

Getting started

Installation

pip install remote_plot

Port forwarding

If you are using VSCode, once you run your first plot, it will automatically forward the port and pop a dialog box that will let you open the plot in your web browser.

If you are using SSH, you can forward the port using he following flag while connecting to the remote machine:

ssh YOUR_USER_NAME@YOUR_MACHINE_IP -L 8000:localhost:8000

Then, you can just open localhost:8000 in a web browser once you run your first plot.

Your first plot

from remote_plot import plt

plt.plot([1, 2, 3], [4, 5, 6])

A more advanced example

Here's a more advnaced example taken from the official matplotlib documentation

from remote_plot import plt
import numpy as np

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)

# the histogram of the data
n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75)


plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()

plot

Check the examples directory for more examples.

API

Remote plot is intended to act as a drop-in replacement to matplotlib. Because of this, it replicates the matplotlib API, which you can find here.

By default, every call plot will automatically render the result (equivalent to calling plt.show() on matplotlib). This can make things a bit slow, so if you prefer to turn it off, set plt.auto_show = False, and use plt.show() as usual.

It also supports native image displaying that can display numpy array or pillow images.

from PIL import Image
img = Image.open("PATH TO YOUR IMAGE")
plt.imshow_native(img)

Changing the web server port

from remote_plot import plt

plt.port = 8001

License

MIT License. See LICENSE for further details.

remote_plot's People

Contributors

itaicaspi avatar itaicaspi-mirrori avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

remote_plot's Issues

TypeError: 'NoneType' object does not support item assignment

In [1]: import numpy as np
   ...: # import matplotlib.pyplot as plt
   ...: from remote_plot import plt
   ...: import networkx as nx
   ...:
   ...: plt.rcParams["font.size"] = 10
   ...:
   ...: plt.figure(figsize=(8,3))
   ...:
   ...: ax = plt.subplot(121)
   ...: x = np.arange(0,10,0.001)
   ...: ax.plot(x, np.sin(np.sinc(x)), 'r', lw=2)
   ...: ax.set_title('Nice wiggle')
   ...:
   ...: ax = plt.subplot(122)
   ...: plt.tick_params(axis='both', left='off', top='off', right='off', bottom='off', labelleft='off',
   ...:  labeltop='off', labelright='off', labelbottom='off')
   ...: G = nx.random_geometric_graph(200, 0.125)
   ...: pos=nx.spring_layout(G)
   ...: nx.draw_networkx_edges(G, pos, alpha=0.2)
   ...: nx.draw_networkx_nodes(G, pos, node_color='r', node_size=12)
   ...: ax.set_title('Random graph')
   ...:
   ...: plt.show()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[1], line 6
      3 from remote_plot import plt
      4 import networkx as nx
----> 6 plt.rcParams["font.size"] = 10
      8 plt.figure(figsize=(8,3))
     10 ax = plt.subplot(121)

TypeError: 'NoneType' object does not support item assignment

If I comment the line which throws error, then I get the error below:

In [3]: import numpy as np
   ...: # import matplotlib.pyplot as plt
   ...: from remote_plot import plt
   ...: import networkx as nx
   ...:
   ...: # plt.rcParams["font.size"] = 10
   ...:
   ...: plt.figure(figsize=(8,3))
   ...:
   ...: ax = plt.subplot(121)
   ...: x = np.arange(0,10,0.001)
   ...: ax.plot(x, np.sin(np.sinc(x)), 'r', lw=2)
   ...: ax.set_title('Nice wiggle')
   ...:
   ...: ax = plt.subplot(122)
   ...: plt.tick_params(axis='both', left='off', top='off', right='off', bottom='off', labelleft='off',
   ...:  labeltop='off', labelright='off', labelbottom='off')
   ...: G = nx.random_geometric_graph(200, 0.125)
   ...: pos=nx.spring_layout(G)
   ...: nx.draw_networkx_edges(G, pos, alpha=0.2)
   ...: nx.draw_networkx_nodes(G, pos, node_color='r', node_size=12)
   ...: ax.set_title('Random graph')
   ...:
   ...: plt.show()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[3], line 12
     10 ax = plt.subplot(121)
     11 x = np.arange(0,10,0.001)
---> 12 ax.plot(x, np.sin(np.sinc(x)), 'r', lw=2)
     13 ax.set_title('Nice wiggle')
     15 ax = plt.subplot(122)

AttributeError: 'NoneType' object has no attribute 'plot'

AttributeError: 'ForkAwareLocal' object has no attribute 'connection'

Hello!

This seems like a super-useful repo, X11 forwarding of matplotlib sucks!

However, there seems to be an issue when "end-of-file" has been reached

ubuntu@ip-123-45-6-7:~/my_dir$ python3 graph_book.py 
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
----------------------------------------
Exception occurred during processing of request from ('127.0.0.1', 60430)
Traceback (most recent call last):
  File "/usr/lib/python3.10/multiprocessing/managers.py", line 810, in _callmethod
    conn = self._tls.connection
AttributeError: 'ForkAwareLocal' object has no attribute 'connection'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/socketserver.py", line 683, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python3.10/socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python3.10/socketserver.py", line 747, in __init__
    self.handle()
  File "/usr/lib/python3.10/http/server.py", line 432, in handle
    self.handle_one_request()
  File "/usr/lib/python3.10/http/server.py", line 420, in handle_one_request
    method()
  File "/home/ubuntu/.local/lib/python3.10/site-packages/remote_plot/server.py", line 96, in do_GET
    token = self.server.SHARED['token']
  File "<string>", line 2, in __getitem__
  File "/usr/lib/python3.10/multiprocessing/managers.py", line 814, in _callmethod
    self._connect()
  File "/usr/lib/python3.10/multiprocessing/managers.py", line 801, in _connect
    conn = self._Client(self._token.address, authkey=self._authkey)
  File "/usr/lib/python3.10/multiprocessing/connection.py", line 502, in Client
    c = SocketClient(address)
  File "/usr/lib/python3.10/multiprocessing/connection.py", line 630, in SocketClient
    s.connect(address)
FileNotFoundError: [Errno 2] No such file or directory
----------------------------------------
----------------------------------------
Exception occurred during processing of request from ('127.0.0.1', 60432)
Traceback (most recent call last):
  File "/usr/lib/python3.10/multiprocessing/managers.py", line 810, in _callmethod
    conn = self._tls.connection
AttributeError: 'ForkAwareLocal' object has no attribute 'connection'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/socketserver.py", line 683, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python3.10/socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python3.10/socketserver.py", line 747, in __init__
    self.handle()
  File "/usr/lib/python3.10/http/server.py", line 432, in handle
    self.handle_one_request()
  File "/usr/lib/python3.10/http/server.py", line 420, in handle_one_request
    method()
  File "/home/ubuntu/.local/lib/python3.10/site-packages/remote_plot/server.py", line 96, in do_GET
    token = self.server.SHARED['token']
  File "<string>", line 2, in __getitem__
  File "/usr/lib/python3.10/multiprocessing/managers.py", line 814, in _callmethod
    self._connect()
  File "/usr/lib/python3.10/multiprocessing/managers.py", line 801, in _connect
    conn = self._Client(self._token.address, authkey=self._authkey)
  File "/usr/lib/python3.10/multiprocessing/connection.py", line 502, in Client
    c = SocketClient(address)
  File "/usr/lib/python3.10/multiprocessing/connection.py", line 630, in SocketClient
    s.connect(address)
FileNotFoundError: [Errno 2] No such file or directory
----------------------------------------

The same issues happen if I run plt.plot() in the python shell, and then hit "Cmd+D" for the EOF character. It's not a big deal, just CTRL+C exits, but just thought I'd post the error here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.