Skip to content

Commit

Permalink
add option to disable automatic figure resizing
Browse files Browse the repository at this point in the history
A user requested that there be an option to disable
automatic resizing to support certain workflows.

As accessing the actual backend instance is a bit awkward,
we expose this setting as an environment variable called
`MPLBACKEND_KITTY_SIZING`, named analogously to MPL's
`MPLBACKEND` variable used to select backends.

closes #3
  • Loading branch information
jktr committed Nov 21, 2021
1 parent 088dcee commit a6ad968
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ To use it, add the directory containing this module to your
`sys.path` or the `$PYTHONPATH`, or clone this repo into
your python's `site-packages` directory.

Then, initialize matplotlib like this:
Then, initialize matplotlib either as follows or using the
the `MPLBACKEND` environment variable:

```python
import matplotlib
Expand Down Expand Up @@ -38,6 +39,10 @@ you can construct your figures as usual, and then call
`plt.show()` to render them to your terminal. This
works from both a repl and when running scripts.

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
Expand Down
26 changes: 15 additions & 11 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,25 @@ def f(*args, output=True, **kwargs):
return f

def show(self):
tput = __class__._run('tput')

icat = __class__._run('kitty', '+kitten', 'icat')

# gather terminal dimensions
rows = int(tput('lines'))
px = icat('--print-window-size')
px = list(map(int, px.split('x')))
if os.environ.get('MPLBACKEND_KITTY_SIZING', 'automatic') != 'manual':

tput = __class__._run('tput')

# gather terminal dimensions
rows = int(tput('lines'))
px = icat('--print-window-size')
px = list(map(int, px.split('x')))

# account for post-display prompt scrolling
# 3 line shift for [\n, <matplotlib.axes…, >>>] after the figure
px[1] -= int(3*(px[1]/rows))
# account for post-display prompt scrolling
# 3 line shift for [\n, <matplotlib.axes…, >>>] after the figure
px[1] -= int(3*(px[1]/rows))

# resize figure to terminal size & aspect ratio
dpi = self.canvas.figure.dpi
self.canvas.figure.set_size_inches((px[0] / dpi, px[1] / dpi))
# resize figure to terminal size & aspect ratio
dpi = self.canvas.figure.dpi
self.canvas.figure.set_size_inches((px[0] / dpi, px[1] / dpi))

with BytesIO() as buf:
self.canvas.figure.savefig(buf, format='png', facecolor='#888888')
Expand Down

0 comments on commit a6ad968

Please sign in to comment.