[REQUEST] Jupyter Inline Plots
beojan opened this issue ยท 7 comments
When I use the mplcairo backend, my plots do not appear inline in Jupyter (I'm not talking about the inline widget here, just inline PNGs).
Is there anyway to get the inline PNGs, given Cairo supports PNG export anyway?
Looks like adding
import ipykernel.pylab.backend_inline, mplcairo.base
ipykernel.pylab.backend_inline.new_figure_manager = mplcairo.base.new_figure_manager
to your ipython_config.py should work, can you confirm?
I'm trying to follow Brienna's tutorial on plotting emojis with matplotlib. But I got segmentation fault error when running the script from the command line. This is the script I run
import faulthandler; faulthandler.enable()
# Set the backend to use mplcairo
import platform
print(platform.mac_ver())
print(platform.python_version())
import matplotlib, mplcairo
print('Default backend: ' + matplotlib.get_backend())
matplotlib.use("module://mplcairo.macosx")
print('Backend is now ' + matplotlib.get_backend())
print('matplotlib version: ' + matplotlib.__version__)
# IMPORTANT: Import these libraries only AFTER setting the backend
import matplotlib.pyplot as plt, numpy as np
from matplotlib.font_manager import FontProperties
# Load Apple Color Emoji font
prop = FontProperties(fname='/System/Library/Fonts/Apple Color Emoji.ttc')
# Set up plot
freqs = [301, 96, 53, 81, 42]
labels = ['๐', '๐ฑ', '๐', '๐', '๐']
plt.figure(figsize=(12,8))
p1 = plt.bar(np.arange(len(labels)), freqs, 0.8, color="lightblue")
plt.ylim(0, plt.ylim()[1]+30)
# Make labels
for rect1, label in zip(p1, labels):
height = rect1.get_height()
plt.annotate(
label,
(rect1.get_x() + rect1.get_width()/2, height+5),
ha="center",
va="bottom",
fontsize=30,
fontproperties=prop
)
# plt.show()
plt.savefig("e.png")
This is the output I got after enable faulthandler
.
('10.15.1', ('', '', ''), 'x86_64')
3.7.4
Default backend: MacOSX
Backend is now module://mplcairo.macosx
matplotlib version: 3.1.1
Fatal Python error: Segmentation fault
Current thread 0x0000000118db0dc0 (most recent call first):
File "/usr/local/lib/python3.7/site-packages/matplotlib/text.py", line 291 in _get_layout
File "/usr/local/lib/python3.7/site-packages/matplotlib/text.py", line 890 in get_window_extent
File "/usr/local/lib/python3.7/site-packages/matplotlib/axis.py", line 1150 in <listcomp>
File "/usr/local/lib/python3.7/site-packages/matplotlib/axis.py", line 1150 in _get_tick_bboxes
File "/usr/local/lib/python3.7/site-packages/matplotlib/axis.py", line 1205 in draw
File "/usr/local/lib/python3.7/site-packages/matplotlib/artist.py", line 38 in draw_wrapper
File "/usr/local/lib/python3.7/site-packages/matplotlib/image.py", line 135 in _draw_list_compositing_images
File "/usr/local/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 2647 in draw
File "/usr/local/lib/python3.7/site-packages/matplotlib/artist.py", line 38 in draw_wrapper
File "/usr/local/lib/python3.7/site-packages/matplotlib/image.py", line 135 in _draw_list_compositing_images
File "/usr/local/lib/python3.7/site-packages/matplotlib/figure.py", line 1709 in draw
File "/usr/local/lib/python3.7/site-packages/matplotlib/artist.py", line 38 in draw_wrapper
File "/usr/local/lib/python3.7/site-packages/mplcairo/base.py", line 211 in _get_cached_or_new_renderer
File "/usr/local/lib/python3.7/site-packages/mplcairo/base.py", line 219 in get_renderer
File "/usr/local/lib/python3.7/site-packages/mplcairo/base.py", line 319 in _get_fresh_straight_rgba8888
File "/usr/local/lib/python3.7/site-packages/mplcairo/base.py", line 337 in print_png
File "/usr/local/lib/python3.7/site-packages/matplotlib/backend_bases.py", line 2082 in print_figure
File "/usr/local/lib/python3.7/site-packages/mplcairo/base.py", line 236 in print_figure
File "/usr/local/lib/python3.7/site-packages/matplotlib/figure.py", line 2180 in savefig
File "/usr/local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 722 in savefig
File "emojis_plotted_complete_code.py", line 40 in <module>
[1] 13407 segmentation fault python3 emojis_plotted_complete_code.py
I installed mplcairo, pycairo using pip and cairo using brew
Name: mplcairo
Version: 0.2
Name: pycairo
Version: 1.18.2
cairo: stable 1.16.0 (bottled), HEAD
I also noticed you solved her issue a year ago. What other information I can provide to help to debug the issue?
I tested on ubuntu 18.04 and it worked perfect as long as I copy the ttc file to it.
# Set the backend to use mplcairo
import matplotlib, mplcairo
print('Default backend: ' + matplotlib.get_backend())
matplotlib.use("module://mplcairo.base")
print('Backend is now ' + matplotlib.get_backend())
# IMPORTANT: Import these libraries only AFTER setting the backend
import matplotlib.pyplot as plt, numpy as np
from matplotlib.font_manager import FontProperties
# Load Apple Color Emoji font
prop = FontProperties(fname='./Apple Color Emoji.ttc')
# Set up plot
freqs = [301, 96, 53, 81, 42]
labels = ['๐', '๐ฑ', '๐', '๐', '๐']
plt.figure(figsize=(12,8))
p1 = plt.bar(np.arange(len(labels)), freqs, 0.8, color="lightblue")
plt.ylim(0, plt.ylim()[1]+30)
# Make labels
for rect1, label in zip(p1, labels):
height = rect1.get_height()
plt.annotate(
label,
(rect1.get_x() + rect1.get_width()/2, height+5),
ha="center",
va="bottom",
fontsize=30,
fontproperties=prop
)
plt.savefig("e.png")
I think this issue is unrelated to the original one in this thread? If so, please open a separate issue to keep discussions tidy.
OK. Because I tried to set ipython_config.py
as you suggested, but the kernel will restart every time I run the code. I thought it maybe the same error so I decide to get it running on cli first. I'll open a new issue as you suggested.
Closing this for lack of reply on the original poster, and because the second issue is tracked on #18. Feel free to request a reopen if necessary.
I'm using Jupyter Lab and I added these lines in my config file (jupyterlab_config.py):
import ipykernel.pylab.backend_inline, mplcairo.base
ipykernel.pylab.backend_inline.new_figure_manager = mplcairo.base.new_figure_manager
However, the inline plot does not show up. I started Jupyter Lab using the command jupyter lab --config jupyterlab_config.py
. What I'm missing?