jktr / matplotlib-backend-kitty

show matplotlib plots directly in your kitty terminal
Creative Commons Zero v1.0 Universal
137 stars 16 forks source link

Only shows figure background on matplotlib 3.3.0 #1

Closed subnut closed 4 years ago

subnut commented 4 years ago

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?

jktr commented 4 years ago

Thanks for reporting your problem.

Do you mind sharing your OS + kitty version and a screenshot?

Can you display any other jpeg images in your terminal with $ kitty +kitten icat --align=left ./image.jpg?

It shows only a grey picture. Solid grey. I've attached the intended result (kitty 0.18.1 + NixOS). I'm guessing that your grey is probably the same as the plot's background, right? That would mean that the plot plot itself doesn't get drawn for some reason.

BTW: Both plot() and show() clear the list of figures after drawing, so your show() call doesn't have any effect.

jktr commented 4 years ago

It seems that github doesn't handle email attachments :/ Here's that screenshot of the intended result again. screenshot-2020-07-22T14:56:41+02:00

subnut commented 4 years ago
❯ kitty --version
kitty 0.18.1 created by Kovid Goyal

gifcast_200722232025

You can see that i open python and run ...

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

And then, to verify that icat is indeed working for JPEG files, I open the ONLY jpg i had in my PC :wink:

kitty +kitten icat --align=left ./Pictures/IMG_20181222_213723061.jpg

These are from neofetch:

OS: EndeavourOS Linux x86_64
Kernel: 5.7.9-arch1-1

It is basically stock Arch Linux with modified theme and GUI installer

subnut commented 4 years ago

@jktr Any more info needed?

subnut commented 4 years ago

Another quick example ... gifcast_200722233616

The source is here

subnut commented 4 years ago

The same WITHOUT running

matplotlib.use('module://matplotlib-backend-kitty')

gifcast_200723005054

mrzv commented 4 years ago

I'm having the same problem across platforms (both Linux and Mac). I believe the culprit is matplotlib 3.3.0. The problem started when I upgraded to it (and everything still works fine with matplotlib 3.2.1).

Perhaps, another clue is the following error in a trivial example:

import numpy as np                                                                                                                                                          
import matplotlib.pyplot as plt                                                                                                                                             
a = np.random.random((10,10))                                                                                                                                               
plt.matshow(a)                                                                                                                                                              

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-2e2cc1f4284c> in <module>
----> 1 plt.matshow(a)

/usr/local/lib/python3.8/site-packages/matplotlib/pyplot.py in matshow(A, fignum, **kwargs)
   2283         ax = fig.add_axes([0.15, 0.09, 0.775, 0.775])
   2284     im = ax.matshow(A, **kwargs)
-> 2285     sci(im)
   2286     return im
   2287 

/usr/local/lib/python3.8/site-packages/matplotlib/pyplot.py in sci(im)
   3083 @_copy_docstring_and_deprecators(Axes._sci)
   3084 def sci(im):
-> 3085     return gca()._sci(im)
   3086 
   3087 

/usr/local/lib/python3.8/site-packages/matplotlib/axes/_base.py in _sci(self, im)
   1859                 raise ValueError("ContourSet must be in current Axes")
   1860         elif im not in self.images and im not in self.collections:
-> 1861             raise ValueError("Argument must be an image, collection, or "
   1862                              "ContourSet in this Axes")
   1863         self._current_image = im

ValueError: Argument must be an image, collection, or ContourSet in this Axes
jktr commented 4 years ago

@subnut: Thanks for the test cases. They should suffice for now.

On Fri 2020-07-24T15:51:54-0700, Dmitriy wrote:

I believe the culprit is matplotlib 3.3.0.

Yep. This seems to be it. Arch has 3.3.0, while NixOS unstable was still on 3.2.1. I can reproduce this with matplotlib 3.3.0 on both Arch and NixOS.

I suspect that this is an issue with subplots/axes and our Gcf.destroy_all() getting rid of parts of the plot too early. That would explain why the container of the Series.plot() example gets drawn, but not the plot itself, and why your matshow example fails halfway through showing.

I'm not yet sure what in 3.3.0 causes this, but I'll do some digging over the weekend.

subnut commented 4 years ago

@jktr I found this, might help you :+1:

jktr commented 4 years ago

I've tried implementing a fix in 4a72250526753cb150220f6e0deb94b0b04fe845. @subnut @mrzv: Would you mind testing that version? It seems to work on both 3.3.0 and 3.2.1 on my end.

For interactive use (= skipping some plt.show() calls), you'll need to set matplotlib to interactive mode via python -i or plt.ion(). I couldn't get plt.matshow() to work without plt.show() in interactive mode, as the only draw_if_interactive call on that codepath seems to happen when the figure isn't fully populated yet.

mrzv commented 4 years ago

Works great. The interactive use is a non-issue for me, since I didn't even know about it.

Thanks for fixing it.

subnut commented 4 years ago

Great! Thanks for fixing it! @jktr Should I close this issue now?

EDIT: Mistakenly closed it while commenting :sweat_smile:

jktr commented 4 years ago

I've made the fix available on master. Thanks for your help.

luciodaou commented 1 year ago

Hello. I'm having the same issue as @subnut :

image

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)"
ANSI_COLOR="0;38;2;60;110;180"
LOGO=fedora-logo-icon
CPE_NAME="cpe:/o:fedoraproject:fedora:37"
HOME_URL="https://fedoraproject.org/"
DOCUMENTATION_URL="https://docs.fedoraproject.org/en-US/fedora/f37/system-administrators-guide/"
SUPPORT_URL="https://ask.fedoraproject.org/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Fedora"
REDHAT_BUGZILLA_PRODUCT_VERSION=37
REDHAT_SUPPORT_PRODUCT="Fedora"
REDHAT_SUPPORT_PRODUCT_VERSION=37
SUPPORT_END=2023-11-14
VARIANT="Cloud Edition"
VARIANT_ID=cloud
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 ~
❯
jktr commented 1 year ago

@luciodaou: You seem to be running matplotlib 3.7.1 (instead of the 3.3.0 discussed here), so please open a new issue specific to that version. However, your screenshot appears to demonstrate that the module is working? If that isn't the case, please describe what you think is broken in that new issue.