Code Monkey home page Code Monkey logo

matplotlib-backend-kitty's Introduction

matplotlib-backend-kitty

This python module allows you to use your kitty terminal to show the plots generated by python's matplotlib.

For other terminals, the similar notcurses backend may also be of interest.

To install this one, do one of the following

  • $ pip install --user matplotlib-backend-kitty
  • clone this repo into your python's site-packages directory
  • clone this repo and add the parent directory to sys.path or $PYTHONPATH

Then, configure matplotlib to use the module by either setting the environment variable MPLBACKEND to module://matplotlib-backend-kitty or by initializing matplotlib as follows.

import matplotlib
matplotlib.use('module://matplotlib-backend-kitty')
import matplotlib.pyplot as plt

If you've installed this module correctly, you can now use the following sample code to draw a plot in your terminal.

$ export MPLBACKEND='module://matplotlib-backend-kitty'
$ python
>>> n = 10000
>>> df = pd.DataFrame({'x': np.random.randn(n),
                       'y': np.random.randn(n)})
>>> df.plot.hexbin(x='x', y='y', gridsize=20)
<plot is shown>

If you set your matplotlib to interactive mode via matplotlib.pyplot.ion() or by running an interactive python shell like python or ipython, non-empty figures are drawn on construction where possible. This allows you to use pandas' plot() calls directly, without calling plt.show(), and still enables you to manually construct and plt.show().

If your matplotlib is in non-interactive mode, you can construct your figures as usual, and then call plt.show() to render them to your terminal. This works both from scripts and from a repl.

Figures are resized to the size of your terminal by default. If you'd rather control the sizing of figures manually, set the MPLBACKEND_KITTY_SIZING environment variable to manual.

Internally, this backend is somewhat based on matplotlib's IPython support: it's a hybrid of image and GUI backend types. It works by using matplotlib's Agg backend to render the plot, and then calls kitty's icat to place the rendered image on your terminal. This means that plotting works as expected, but the image drawn to your terminal isn't interactive and animations aren't supported.

matplotlib-backend-kitty's People

Contributors

ayorgo avatar jktr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

matplotlib-backend-kitty's Issues

Make the backend work inside a Docker container

Background

Due to complex nature of my Python projects that are often deployed in the cloud and involve external non-Python dependencies I usually prefer to isolate my environments using Docker rather than through Python's native mechanism of virtual environments.

Issue

The backend doesn't really work inside a Docker container. I tried to add kitty as a dependency (which I'd try to avoid regardless) but it didn't really help.

Solution

Use kitty's native protocol instead of kitty +kitten icat to make it work both inside and outside Docker containers.

Only shows figure background on matplotlib 3.3.0

I cloned the repository to my current directory, opened python, ran the following -

import matplotlib
matplotlib.use('module://matplotlib-backend-kitty')
import matplotlib.pyplot as plt
import pandas; pandas.Series(range(10)).plot()

It shows only a grey picture. Solid grey.
then I tried

plt.show()

Nothing.



Any ideas?

Make the interactive environment detection heuristic work for ipython

Background

When playing around with Python, especially when plotting graphs is involved, I use ipython most of the time.

Issue

The backend doesn't recognise ipython as an interactive environment.

Solution

Pick a different heuristic that recognises ipython, python -i and simple python in an interactive shell as interactive.

Working with Conda

Since I'm using conda I don't have a set PYTHONPATH, can you help me out? I tried using conda develop and pointing to the repo, but despite it being added to the path.
Output of python -c "import sys; print(sys.path)"

['', '/home/joaogui/miniconda3/lib/python37.zip', '/home/joaogui/miniconda3/lib/python3.7', '/home/joaogui/miniconda3/lib/python3.7/lib-dynload', '/home/joaogui/miniconda3/lib/python3.7/site-packages', '/home/joaogui/Downloads/matplotlib-backend-kitty']

Appears as a non-GUI backend with `matplotlib==3.6.0`

Firstly, thanks for the module, it has been hugely helpful for me on daily basis!

Secondly, calling plt.show() with matplotlib==3.6.0 (specifically matplotlib-3.6.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl) release results in

UserWarning: Matplotlib is currently using module://matplotlib-backend-kitty, which is a non-GUI backend, so cannot show the figure.
  _Backend.show(*args, **kwargs)

(same goes for matplotlib-backend-notcurses, btw).

I am, unfortunately, at a loss, if I am simply missing something. Please let me know if I can help with more information.

Error: is a non-GUI backend, so cannot show the figure.

I'm using python 3.11.1 with ipython 8.8.0 and got this error on trying to plot:

[ins] In [6]: plt.show()
/home/fbence/Codes/matplotlib-backend-kitty/__init__.py:83: UserWarning: Matplotlib is currently using module://matplotlib-backend-kitty, which is a non-GUI backend, so cannot show the figure.
  _Backend.show(*args, **kwargs)

Allow setting custom facecolors for figures

Hello. I'm having an issue where Matplotlib's facecolor options for the outside background is not working :

Situation 1 - should show white background if facecolor is not specified:
image

Situation 2 - should show the color specified on facecolor, but shows grey:
image

Situation 3 - Works normally without using MPL/Kitty backend:
image

I found on the code a reference to this:

self.canvas.figure.savefig(buf, format='png', facecolor='#888888')

Removing the , facecolor='#888888' option makes it work just like normal matplotlib does:
No facecolor setting shows as white:
image

Specified color shows correctly:
image

I'm creating a PR for this change (see below).

My info:

fedora in ๐ŸŒ server in ~
โฏ kitty -v
kitty 0.27.1 created by Kovid Goyal
fedora in ๐ŸŒ server in ~
โฏ pip list | grep matplot
matplotlib               3.7.1
matplotlib-backend-kitty 2.1.2
fedora in ๐ŸŒ server in ~
โฏ cat /etc/os-release
NAME="Fedora Linux"
VERSION="37 (Cloud Edition)"
ID=fedora
VERSION_ID=37
VERSION_CODENAME=""
PLATFORM_ID="platform:f37"
PRETTY_NAME="Fedora Linux 37 (Cloud Edition)"
...
fedora in ๐ŸŒ server in ~
โฏ python -VVV
Python 3.11.2 (main, Feb  8 2023, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)]
fedora in ๐ŸŒ server in ~
โฏ

upstream regression in kitty 0.24.4 (ValueError: invalid literal for int() with base 10: '')

Hi,
I'm getting the following error trying to use the package: ValueError: invalid literal for int() with base 10: ''
Here is the full stack trace

  File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.8/site-packages/pandas/plotting/_core.py", line 1758, in hexbin
    return self(kind="hexbin", x=x, y=y, C=C, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.8/site-packages/pandas/plotting/_core.py", line 917, in __call__
    return plot_backend.plot(data, x=x, y=y, kind=kind, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.8/site-packages/pandas/plotting/_matplotlib/__init__.py", line 72, in plot
    plot_obj.draw()
  File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py", line 323, in draw
    self.plt.draw_if_interactive()
  File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.8/site-packages/matplotlib/pyplot.py", line 319, in draw_if_interactive
    return _backend_mod.draw_if_interactive(*args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.8/site-packages/matplotlib-backend-kitty/__init__.py", line 79, in draw_if_interactive
    cls.show()
  File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.8/site-packages/matplotlib-backend-kitty/__init__.py", line 83, in show
    _Backend.show(*args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 3524, in show
    manager.show()  # Emits a warning for non-interactive backend.
  File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.8/site-packages/matplotlib-backend-kitty/__init__.py", line 44, in show
    px = list(map(int, px.split('x')))
ValueError: invalid literal for int() with base 10: '' 

I am using Kitty 0.24.4 under MacOS 12.3 with Python 3.8.13 installed with Conda and Matplotlib 3.5.1

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.