Giter Club home page Giter Club logo

matplotlib-inline's Introduction

image

image

image

image

image

image

IPython: Productive Interactive Computing

Overview

Welcome to IPython. Our full documentation is available on ipython.readthedocs.io and contains information on how to install, use, and contribute to the project. IPython (Interactive Python) is a command shell for interactive computing in multiple programming languages, originally developed for the Python programming language, that offers introspection, rich media, shell syntax, tab completion, and history.

IPython versions and Python Support

Starting after IPython 8.16, we will progressively transition to Spec-0000.

Starting with IPython 7.10, IPython follows NEP 29

IPython 7.17+ requires Python version 3.7 and above.

IPython 7.10+ requires Python version 3.6 and above.

IPython 7.0 requires Python version 3.5 and above.

IPython 6.x requires Python version 3.3 and above.

IPython 5.x LTS is the compatible release for Python 2.7. If you require Python 2 support, you must use IPython 5.x LTS. Please update your project configurations and requirements as necessary.

The Notebook, Qt console and a number of other pieces are now parts of Jupyter. See the Jupyter installation docs if you want to use these.

Main features of IPython

Comprehensive object introspection.

Input history, persistent across sessions.

Caching of output results during a session with automatically generated references.

Extensible tab completion, with support by default for completion of python variables and keywords, filenames and function keywords.

Extensible system of ‘magic’ commands for controlling the environment and performing many tasks related to IPython or the operating system.

A rich configuration system with easy switching between different setups (simpler than changing $PYTHONSTARTUP environment variables every time).

Session logging and reloading.

Extensible syntax processing for special purpose situations.

Access to the system shell with user-extensible alias system.

Easily embeddable in other Python programs and GUIs.

Integrated access to the pdb debugger and the Python profiler.

Development and Instant running

You can find the latest version of the development documentation on readthedocs.

You can run IPython from this directory without even installing it system-wide by typing at the terminal:

$ python -m IPython

Or see the development installation docs for the latest revision on read the docs.

Documentation and installation instructions for older version of IPython can be found on the IPython website

IPython requires Python version 3 or above

Starting with version 6.0, IPython does not support Python 2.7, 3.0, 3.1, or 3.2.

For a version compatible with Python 2.7, please install the 5.x LTS Long Term Support version.

If you are encountering this error message you are likely trying to install or use IPython from source. You need to checkout the remote 5.x branch. If you are using git the following should work:

$ git fetch origin
$ git checkout 5.x

If you encounter this error message with a regular install of IPython, then you likely need to update your package manager, for example if you are using pip check the version of pip with:

$ pip --version

You will need to update pip to the version 9.0.1 or greater. If you are not using pip, please inquiry with the maintainers of the package for your package manager.

For more information see one of our blog posts:

https://blog.jupyter.org/release-of-ipython-5-0-8ce60b8d2e8e

As well as the following Pull-Request for discussion:

#9900

This error does also occur if you are invoking setup.py directly – which you should not – or are using easy_install If this is the case, use pip install . instead of setup.py install , and pip install -e . instead of setup.py develop If you are depending on IPython as a dependency you may also want to have a conditional dependency on IPython depending on the Python version:

install_req = ['ipython']
if sys.version_info[0] < 3 and 'bdist_wheel' not in sys.argv:
    install_req.remove('ipython')
    install_req.append('ipython<6')

setup(
    ...
    install_requires=install_req
)

Alternatives to IPython

IPython may not be to your taste; if that's the case there might be similar project that you might want to use:

Ignoring commits with git blame.ignoreRevsFile

As of git 2.23, it is possible to make formatting changes without breaking git blame. See the git documentation for more details.

To use this feature you must:

  • Install git >= 2.23
  • Configure your local git repo by running:
    • POSIX: tools\configure-git-blame-ignore-revs.sh
    • Windows: tools\configure-git-blame-ignore-revs.bat

matplotlib-inline's People

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

matplotlib-inline's Issues

Importing `backend_inline` triggers selection of `QtAgg` backend and tie-in of GUI event loop in IPython

I've been working on the GUI event loop hooks in IPython, and while working on this issue, I realized that importing backend_inline will trigger matplotlib's "auto backend selection". In many systems, this results in selecting QtAgg and thus in IPython, this effectively calls %gui qt.

The issue is that 'qt' as far as GUI event loop hooks go implies "latest version of Qt", and once a version of Qt is imported in a session, you can't change it. So on a system with e.g. pyside6 and PyQt5 installed, starting IPython with --matplotlib=qt5 will result in an effective %gui qt call, which will import pyside6, followed by a %gui qt5 call, which will refuse to run because it cannot import PyQt5.

The culprit is this:

I believe that should instead call _get_backend_or_none(), i.e. you want to see if a backend is loaded, you don't want to cause the default to be loaded.

I prevented IPython from importing backend_inline where the problem starts (see here) and it seems to cause no difference in Jupyter notebook - so I'm not entirely sure what matplotlib-inline is supposed to be doing. It's quite likely I don't understand what I'm even testing here.

Question about pypi/matplotlib-inline pointing to a fork of this project.

Hello,

I was taking a look at https://pypi.org/project/matplotlib-inline/ .

Is there any reason why as a source/homepage it points to a fork of @martinRenou instead of this repo? Isn't this the "official" repo? As of my understanding, Martin currently works for your organization so it is possible there was a mistake when publishing to Pypi?

To me it feels strange to have the official package source to be here (ipython/matplotlib-inline) but deployed/owned by Martin on Pypi (via martinRenou/matplotlib-inline, which clearly is shown by GitHub as a fork) .

Thanks for the clarification,

Cristian

AttributeError: module 'matplotlib' has no attribute 'pyplot'

While attempting to create a piano roll with librosa and matplotlib, like this:

def plot_piano_roll(pm, start_pitch, end_pitch, fs=100):
    # Use librosa's specshow function for displaying the piano roll
    librosa.display.specshow(pm.get_piano_roll(fs)[start_pitch:end_pitch],
                             hop_length=1, sr=fs, x_axis='time', y_axis='cqt_note',
                             fmin=pretty_midi.note_number_to_hz(start_pitch))

plt.figure(figsize=(8, 4))
plot_piano_roll(pm, 56, 70)

I encounter this error:

AttributeError                            Traceback (most recent call last)
File ~/anaconda3/lib/python3.9/site-packages/matplotlib_inline/backend_inline.py:100, in show(close, block)
     97 # only call close('all') if any to close
     98 # close triggers gc.collect, which can be slow
     99 if close and Gcf.get_all_fig_managers():
--> 100     matplotlib.pyplot.close('all')

File ~/anaconda3/lib/python3.9/site-packages/matplotlib/_api/__init__.py:226, in caching_module_getattr.<locals>.__getattr__(name)
    224 if name in props:
    225     return props[name].__get__(instance)
--> 226 raise AttributeError(
    227     f"module {cls.__module__!r} has no attribute {name!r}")

AttributeError: module 'matplotlib' has no attribute 'pyplot'

The error seems to be fixed if I explicitly declare import matplotlib.pyplot as plt at the start of the file and then use that to call it like this: plt.close('all'). If that alone is satisfactory, I would request to do a PR.

figure formatters not in this package

This package doesn't contain the figure formatters it uses, which are still all defined in IPython.core.pylabtools. Should they be here so that upgrading the matplotlib-inline package upgrades the inline-figure formatters?

Figures not automatically rendered in some cases

I found that in a fresh Jupyter kernel, the following snippet prevents further figures to be rendered:

# cell 1
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter

with plt.style.context('seaborn'):
    fig, ax = plt.subplots(figsize=(5, 5))
    ax.xaxis.set_major_formatter(DateFormatter("%H:%M"))

This figure ("cell 1") is rendered, but new figures are not rendered any more in a notebook:

# cell 2
fig, ax = plt.subplots(figsize=(5, 5))  # not rendered

The issue does not appear in any of the following case:

  • "cell 2" is run before "cell 1": all following figures are then properly rendered
  • The %matplotlib inline instruction is explicitly added, before or after "cell 1"
  • The context manager is not used (but it fails with any stylesheet)
  • The DateFormatter is not applied

difference in plt.gca() behaviour between inline and notebook backends

I have one notebook cell that creates a 3D axes, which I want to plot into in the next cell.

With the inline backend

Screen Shot 2022-01-26 at 3 18 55 pm

we see that the "current plot" has been forgotten, and plt.gca() creates a new 2D plot.

With the notebook backend

The behaviour is different

Screen Shot 2022-01-26 at 3 23 57 pm

and plt.gca() remembers the 3D plot across the cell boundaries.

Expected behaviour

The same in both cases, ideally the remembering the current plot across the cell boundary. I'm not sure which behaviour is "correct" but the difference is a problem when I'm running code in an ipython kernel from a client.

Commit schedule for matplotlib-inline

The last release of matplotlib-inline is 0.1.3 from 9 months ago (Sept. 2021).
There have been several code changes since.
Specifically I am interested in commit a433a89 from May 12/13th which includes dropping some Matplotlib rcParams overrides that were specific to ipython notebooks.
I would like to know if any schedule is currently in place for this commit to be published.
Thanks.

docs for API calling

IPYthon docs lead me here -
https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#IPython.display.set_matplotlib_formats

they suggest:

"deprecated since version 7.23: use matplotlib_inline.backend_inline.set_matplotlib_formats()"

It's not entirely clear how to call this API.

This is what I"m doing

from IPython import get_ipython
get_ipython().run_line_magic("matplotlib", "inline")
import matplotlib_inline.backend_inline
matplotlib_inline.backend_inline.set_matplotlib_formats()

Is this the correct convention for using this API via python?

Cheers, thanks for the great library.

`plt.ioff()` not preventing automatic figure generation in jupyter

Using plt.interactive(False) or plt.ioff() in Jupyter (Lab and classic notebook) does not seem to prevent automatic figure generation, unless the magic %matplotlib inline in run first, even though the inline backend is the default.

Example

import matplotlib.pyplot as plt

plt.ioff()

fig, ax = plt.subplots()
a = 0

The figure still appears automatically under the cell.
Screenshot at 2022-08-29 14-25-08

Note that

Explicitly running %matplotlib inline blocks figure creation (even though inline is the default activated backend in Jupyter):
Screenshot at 2022-08-29 14-26-48

Originally posted in matplotlib/matplotlib#23766

Switch from PNG to SVG output by default?

The removal of the Matplotlib RC overrides in #14 by @fperez has given me hope to one day get good defaults for inline plots.

What about setting the default output format to SVG (instead of the current PNG)?

This would provide the correct plot size in notebooks and I guess it would also improve the perceived plot quality in most cases.

I've used SVG for years via local settings, but it would be great to have it as the default.

A possible objection would be that SVG plots can become quite large if there is a lot of data to plot, but I think in this case rasterization should be recommended.

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

Bug summary

plt.scatter gives UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte error with notebook running python3 ipykernel, matplotlib-3.4.3, backend: module://matplotlib_inline.backend_inline.

Works fine when kernel is switched to python2, matplotlib-2.2.3, backend: module://ipykernel.pylab.backend_inline .

Code for reproduction

import matplotlib.pyplot as plt

X = [590,540,740,130,810,300,320,230,470,620,770,250]
Y = [32,36,39,52,61,72,77,75,68,57,48,48]
print(X)
print(Y)

plt.scatter(X,Y)

Actual outcome

[590, 540, 740, 130, 810, 300, 320, 230, 470, 620, 770, 250]
[32, 36, 39, 52, 61, 72, 77, 75, 68, 57, 48, 48]
<matplotlib.collections.PathCollection at 0x7fd16a4d8390>
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
~/.local/lib/python3.7/site-packages/matplotlib_inline/backend_inline.py in show(close, block)
     41             display(
     42                 figure_manager.canvas.figure,
---> 43                 metadata=_fetch_figure_metadata(figure_manager.canvas.figure)
     44             )
     45     finally:

~/.local/lib/python3.7/site-packages/IPython/core/display.py in display(include, exclude, metadata, transient, display_id, *objs, **kwargs)
    325                 # kwarg-specified metadata gets precedence
    326                 _merge(md_dict, metadata)
--> 327             publish_display_data(data=format_dict, metadata=md_dict, **kwargs)
    328     if display_id:
    329         return DisplayHandle(display_id)

~/.local/lib/python3.7/site-packages/IPython/core/display.py in publish_display_data(data, metadata, source, transient, **kwargs)
    120         data=data,
    121         metadata=metadata,
--> 122         **kwargs
    123     )
    124 

~/.local/lib/python3.7/site-packages/ipykernel/zmqshell.py in publish(self, data, metadata, transient, update)
    137 
    138         self.session.send(
--> 139             self.pub_socket, msg, ident=self.topic,
    140         )
    141 

~/.local/lib/python3.7/site-packages/jupyter_client/session.py in send(self, stream, msg_or_type, content, parent, ident, buffers, track, header, metadata)
    828         if self.adapt_version:
    829             msg = adapt(msg, self.adapt_version)
--> 830         to_send = self.serialize(msg, ident)
    831         to_send.extend(buffers)
    832         longest = max([len(s) for s in to_send])

~/.local/lib/python3.7/site-packages/jupyter_client/session.py in serialize(self, msg, ident)
    702             content = self.none
    703         elif isinstance(content, dict):
--> 704             content = self.pack(content)
    705         elif isinstance(content, bytes):
    706             # content is already packed, as in a relayed message

~/.local/lib/python3.7/site-packages/jupyter_client/session.py in json_packer(obj)
     97         default=json_default,
     98         ensure_ascii=False,
---> 99         allow_nan=False,
    100     )
    101 

/usr/lib/python3/dist-packages/zmq/utils/jsonapi.py in dumps(o, **kwargs)
     38         kwargs['separators'] = (',', ':')
     39 
---> 40     s = jsonmod.dumps(o, **kwargs)
     41 
     42     if isinstance(s, unicode):

/usr/lib/python3/dist-packages/simplejson/__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, encoding, default, use_decimal, namedtuple_as_object, tuple_as_array, bigint_as_string, sort_keys, item_sort_key, for_json, ignore_nan, int_as_string_bitcount, iterable_as_array, **kw)
    397         ignore_nan=ignore_nan,
    398         int_as_string_bitcount=int_as_string_bitcount,
--> 399         **kw).encode(obj)
    400 
    401 

/usr/lib/python3/dist-packages/simplejson/encoder.py in encode(self, o)
    294         # exceptions aren't as detailed.  The list call should be roughly
    295         # equivalent to the PySequence_Fast that ''.join() would do.
--> 296         chunks = self.iterencode(o, _one_shot=True)
    297         if not isinstance(chunks, (list, tuple)):
    298             chunks = list(chunks)

/usr/lib/python3/dist-packages/simplejson/encoder.py in iterencode(self, o, _one_shot)
    376                 self.iterable_as_array, Decimal=decimal.Decimal)
    377         try:
--> 378             return _iterencode(o, 0)
    379         finally:
    380             key_memo.clear()

/usr/lib/python3/dist-packages/simplejson/encoder.py in encode_basestring(s, _PY3, _q)
     42     if _PY3:
     43         if isinstance(s, bytes):
---> 44             s = str(s, 'utf-8')
     45         elif type(s) is not str:
     46             # convert an str subclass instance to exact str

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

Expected outcome

plt.show() right after this should give an image of the plot.

Operating system

Debian 10 (Skolelinux)

Matplotlib Version

3.4.3

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.7.3

Jupyter version

5.7.8

Other libraries

No response

Installation

pip

Conda channel

No response

Retina+jpeg option?

I often work with complex matplotlib figures with millions of points, which I've been displaying in my notebooks using the ipython magic %config InlineBackend.figure_format = 'retina'. Unfortunately, these inline images are quite large in terms of file size.

I'm looking for general suggestions on how to reduce the size of my ipynb files, which can grow into the 10s or even hundreds of MB in size and become a bit unwieldy.

One thought I had was to switch to generating jpegs, but while it looks like I can meaningfully reduce the file size by switching the backend to jpeg and increasing the dpi to 2x:

from matplotlib import rcParams
%config InlineBackend.figure_format = 'jpeg'
rcParams['figure.dpi'] = 200

From this repo, it looks like I can dial in jpeg compression values, which should help reduce file size. However, I can't figure out how to size the jpegs appropriately for a retina display (they're twice as large as they should be, and slightly blurry). Is there a way to display jpegs in jupyter notebooks at similar resolution and size to the defaults provided by 'retina' mode?

See also:
Matplotlib issue
Stackoverflow question

[Bug]: Figure manager is gone - AttributeError: 'NoneType' object has no attribute 'canvas'

Inside my Jupyter Notebooks, I want to create several plots with matplotlib. Since the release of v0.1.5, my CI for my docs and unittests is failing now.

The error message goes like this:

 File ~/work/conflowgen/conflowgen/conflowgen/analyses/inbound_and_outbound_vehicle_capacity_analysis_report.py:77, in InboundAndOutboundVehicleCapacityAnalysisReport.get_report_as_graph(self, **kwargs)
     71 df = pd.DataFrame({
     72     "inbound volume (in TEU)": inbound_capacities,
     73     "outbound volume (in TEU)": outbound_actual_capacities,
     74     "outbound maximum capacity": outbound_maximum_capacities
     75 })
     76 df.index = [str(i).replace("_", " ") for i in df.index]
---> 77 ax = df.plot.barh()
     78 ax.set_xlabel("Capacity (in TEU)")
     79 ax.set_title("Inbound and outbound vehicle capacity analysis")

File ~/.local/lib/python3.8/site-packages/pandas/plotting/_core.py:1217, in PlotAccessor.barh(self, x, y, **kwargs)
   1133 @Appender(
   1134     """
   1135     See Also
   (...)
   1206 @Appender(_bar_or_line_doc)
   1207 def barh(self, x=None, y=None, **kwargs):
   1208     """
   1209     Make a horizontal bar plot.
   1210 
   (...)
   1215     other axis represents a measured value.
   1216     """
-> 1217     return self(kind="barh", x=x, y=y, **kwargs)

File ~/.local/lib/python3.8/site-packages/pandas/plotting/_core.py:972, in PlotAccessor.__call__(self, *args, **kwargs)
    969             label_name = label_kw or data.columns
    970             data.columns = label_name
--> 972 return plot_backend.plot(data, kind=kind, **kwargs)

File ~/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/__init__.py:71, in plot(data, kind, **kwargs)
     69         kwargs["ax"] = getattr(ax, "left_ax", ax)
     70 plot_obj = PLOT_CLASSES[kind](data, **kwargs)
---> 71 plot_obj.generate()
     72 plot_obj.draw()
     73 return plot_obj.result

File ~/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py:328, in MPLPlot.generate(self)
    326 self._args_adjust()
    327 self._compute_plot_data()
--> 328 self._setup_subplots()
    329 self._make_plot()
    330 self._add_table()

File ~/.local/lib/python3.8/site-packages/pandas/plotting/_matplotlib/core.py:386, in MPLPlot._setup_subplots(self)
    384 else:
    385     if self.ax is None:
--> 386         fig = self.plt.figure(figsize=self.figsize)
    387         axes = fig.add_subplot(111)
    388     else:

File ~/.local/lib/python3.8/site-packages/matplotlib/pyplot.py:810, in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, clear, **kwargs)
    798     _api.warn_external(
    799         f"More than {max_open_warning} figures have been opened. "
    800         f"Figures created through the pyplot interface "
   (...)
    803         f"warning, see the rcParam `figure.max_open_warning`).",
    804         RuntimeWarning)
    806 manager = new_figure_manager(
    807     num, figsize=figsize, dpi=dpi,
    808     facecolor=facecolor, edgecolor=edgecolor, frameon=frameon,
    809     FigureClass=FigureClass, **kwargs)
--> 810 fig = manager.canvas.figure
    811 if fig_label:
    812     fig.set_label(fig_label)

AttributeError: 'NoneType' object has no attribute 'canvas'
AttributeError: 'NoneType' object has no attribute 'canvas'

I have checked the changes with git blame and saw that there is this line where the figure manager can turn None:
https://github.com/ipython/matplotlib-inline/blob/master/matplotlib_inline/backend_inline.py#L55 which has changed with commit 6380cd9.
Is it possible that this causes my issue? In my unittests, I use nbformat/nbconvert and for the docs I use nbsphinx. I am no expert in these tools myself but in each case just image files are generated and included in the output. They are in that sense not interactive. However, I still need the figure manager not to be None! A bit of discussion regarding this can be found at matplotlib/matplotlib#23648. There, it was mentioned that when the figure manager is None, something fishy is going on.

I am no expert in either tool. My first guess work, however, is that in 6380cd9 and later commits possibly some edge cases were not covered? I would love if you could have a look! Thank you!

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.