Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REQUEST] Jupyter Inline Plots #17

Closed
beojan opened this issue Oct 29, 2019 · 7 comments
Closed

[REQUEST] Jupyter Inline Plots #17

beojan opened this issue Oct 29, 2019 · 7 comments

Comments

@beojan
Copy link

beojan commented Oct 29, 2019

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?

@anntzer
Copy link
Collaborator

anntzer commented Oct 29, 2019

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?

@nickyfoto
Copy link

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?

@nickyfoto
Copy link

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")

@anntzer
Copy link
Collaborator

anntzer commented Nov 4, 2019

I think this issue is unrelated to the original one in this thread? If so, please open a separate issue to keep discussions tidy.

@nickyfoto
Copy link

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.

@anntzer
Copy link
Collaborator

anntzer commented Dec 11, 2019

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.

@WinDerek
Copy link

WinDerek commented Feb 26, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants