Bug 469939 - IPython command raises error in jupyter notebooks
Summary: IPython command raises error in jupyter notebooks
Status: RESOLVED FIXED
Alias: None
Product: LabPlot2
Classification: Applications
Component: general (show other bugs)
Version: 2.10.0
Platform: Neon Linux
: NOR minor
Target Milestone: ---
Assignee: Alexander Semke
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-05-18 10:53 UTC by YOYOWORKOUT
Modified: 2023-09-18 06:54 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments
picture of the SyntaxError (in virtualenvironment) (420.52 KB, image/png)
2023-05-18 10:53 UTC, YOYOWORKOUT
Details

Note You need to log in before you can comment on or make changes to this bug.
Description YOYOWORKOUT 2023-05-18 10:53:15 UTC
Created attachment 159068 [details]
picture of the SyntaxError (in virtualenvironment)

SUMMARY
***
When using matplotlib and ipython installed both in  virtual environment by disto repos, the command %matplotlib inline raises  "SyntaxError: invalid syntax "
***


STEPS TO REPRODUCE
1. install dependencies : apt install -y python3-{matplotlib,numpy,notebook,ipykernel} 
2. create a LabPlot project, add new notebook in it
3. import numpy, matplotlib : is ok
4. in a command cell   :  insert '%matplotlib inline'

OBSERVED RESULT
  File "/home/yoyo/Downloads/PyDataScience.lml", line 1
    %matplotlib inline
    ^
SyntaxError: invalid syntax


EXPECTED RESULT
nothing , being abble to see matplotlib.pyplot.plot( x, y) in the notebook 
note : using plot.show()without trying to inline the matplotlib figure  indeed  does show the outside window with plot


SOFTWARE/OS VERSIONS
Linux/KDE Plasma: KDE Neon 5.27  
(available in About System)
KDE Plasma Version: 5.27.5 
KDE Frameworks Version: 5.106.0
Qt Version: 5.15.9


ADDITIONAL INFORMATION
In options , for CAS Python backend , I left the defaults : matplotlib as drawing lib , both plot integration and variable management enabled.
Deed I miss something not being in the help book ?
Comment 1 Alexander Semke 2023-05-18 14:57:53 UTC
(In reply to YOYOWORKOUT from comment #0)
> Created attachment 159068 [details]
> picture of the SyntaxError (in virtualenvironment)
> 
> SUMMARY
> ***
> When using matplotlib and ipython installed both in  virtual environment by
> disto repos, the command %matplotlib inline raises  "SyntaxError: invalid
> syntax "
> ***
> 
> 
> STEPS TO REPRODUCE
> 1. install dependencies : apt install -y
> python3-{matplotlib,numpy,notebook,ipykernel} 
> 2. create a LabPlot project, add new notebook in it
> 3. import numpy, matplotlib : is ok
> 4. in a command cell   :  insert '%matplotlib inline'
> 
> OBSERVED RESULT
>   File "/home/yoyo/Downloads/PyDataScience.lml", line 1
>     %matplotlib inline
>     ^
> SyntaxError: invalid syntax
This 'inline' command is from ipython. In Cantor you don't need to use it, we rely on python only. With the option "Plot Integration" you can control where the produce plot result is opened in the external viewer used by matplotlib, etc. or embedded into Cantor's notebook. The following example should work:

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0,2*np.pi, 0.1)
x = 16*np.sin(t)**3
y = 13*np.cos(t)-5*np.cos(2*t)-2*np.cos(3*t)-np.cos(4*t)

plt.plot(x,y)
plt.show()

I just tried it and saw the produce plot is not properly embedded if the plot integration option is activated :-( But this must be a different problem. Please give it a try without any ipython specific keywords.
Comment 2 Alexander Semke 2023-09-18 06:54:44 UTC
The problem with not showing plot results in Python was fixed recently in Cantor that is being used in LabPlot for the notebook interface. As discussed in the Matrix chat, you don't need to use that "inline" command. Just specify in the application settings whether you want the plot results to be integrated/embedded into the notebook or to be shown in an external window.

A minimal example for a working matplotlib session:
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()

Just try it out with and without integrating the plots option in the settings to see how it works in LabPlot. You'll need a newer version of Cantor  (current nightly build or 23.12 to be released later).