Skip to content

Does codebraid work with Python Anaconda environments? Trouble plotting with python from jupyter notebook #54

@00krishna

Description

@00krishna

Hello. I use Anaconda to create virtual python environments. I have jupyter notebooks and jupyterlab installed in my load virtual environment, as well as codebraid. But for some reason, I can't seem to generate plots with the jupyter kernel, though everything else for python seems to work. So the key question is, do I need to add some instruction to point to the Anaconda environment?

I just ran this codeblock in the attached file, and the error message says that Matplotlib is not found.

Plots

```{.python .cb-nb jupyter_kernel=python3 session=random example=true}
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 1001)
x_tick_values = np.linspace(0, 2*np.pi, 5)
x_tick_labels = ['0', r'$\pi/2$', r'$\pi$', r'$3\pi/2$', r'$2\pi$']
plt.plot(x, np.cos(x), label=r'$\cos(x)$')
plt.plot(x, np.sin(x), label=r'$\sin(x)$')
plt.xticks(x_tick_values, x_tick_labels)
plt.legend(prop={'size': 12})
plt.grid()

I know that matplotlib is installed, because I can open and ipython terminal session, and then import matplotlib.pyplot as plt and that works.
julia_output_python.pdf
julia-braid-test-python.md

The full error message is below.

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 get_ipython().run_line_magic('matplotlib', 'inline')
      2 import matplotlib.pyplot as plt
      3 import numpy as np

File /media/hayagriva/miniforge3/lib/python3.9/site-packages/IPython/core/interactiveshell.py:2294, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2292     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2293 with self.builtin_trap:
-> 2294     result = fn(*args, **kwargs)
   2295 return result

File /media/hayagriva/miniforge3/lib/python3.9/site-packages/IPython/core/magics/pylab.py:99, in PylabMagics.matplotlib(self, line)
     97     print("Available matplotlib backends: %s" % backends_list)
     98 else:
---> 99     gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)
    100     self._show_matplotlib_backend(args.gui, backend)

File /media/hayagriva/miniforge3/lib/python3.9/site-packages/IPython/core/interactiveshell.py:3444, in InteractiveShell.enable_matplotlib(self, gui)
   3423 def enable_matplotlib(self, gui=None):
   3424     """Enable interactive matplotlib and inline figure support.
   3425 
   3426     This takes the following steps:
   (...)
   3442         display figures inline.
   3443     """
-> 3444     from matplotlib_inline.backend_inline import configure_inline_support
   3446     from IPython.core import pylabtools as pt
   3447     gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)

File /media/hayagriva/miniforge3/lib/python3.9/site-packages/matplotlib_inline/backend_inline.py:6, in <module>
      1 """A matplotlib backend for publishing figures via display_data"""
      3 # Copyright (c) IPython Development Team.
      4 # Distributed under the terms of the BSD 3-Clause License.
----> 6 import matplotlib
      7 from matplotlib.backends.backend_agg import (  # noqa
      8     new_figure_manager,
      9     FigureCanvasAgg,
     10     new_figure_manager_given_figure,
     11 )
     12 from matplotlib import colors

ModuleNotFoundError: No module named 'matplotlib'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions