Set the vertical space between the lines of code in jupyter notebook - python

I am working in jupyter notebook, with python code.
I want to increase the vertical space between the lines of code. For example, I have this code:
a=2
b=3
I want to change it to:
a=2
b=3
Do you know any trick to set this space? Thanks
I am trying to use the setting from edit part. But it can not work.

Related

Plot multiple lines in one chart using function

I am working on a project and I need to plot multiple lines in one chart using function, I tried several times but I still don't know what is missing.
I did it without a function and it was successful but using the function is mandatory.
What you see is an artifact of using Matplotlib's "inline" backend in a notebook, the same code tested in a command line interpreter works as you intended, three lines and a single legend in a single Axes.
To remedy the problem is, fortunately, very easy: just dedent plt.show, so that it's executed once, when all the three lines have been placed in a single Axes.
def Plot(*cols):
for c in cols: plt.plot(...)
plt.legend() ; plt.show()
(note that also plt.legend can/should be outside of the loop).
Post Scriptum
If your function is meant to be executed in a "cell" of a Jupyter notebook, you can just omit completely the plt.show, because Jupyter will do that for you when you execute the cell.
Post Post Scriptum
The OP laments continuing problems, but I cannot reproduce their issues. To this aim, I have defined two functions, one with show outside the loop and another with show inside the loop, and at the bottom you can see what I get using the Jupyter notebook.
I whish good luck to the OP, I'm not going to help them further.

Finding output cells causing large file size in jupyter notebook

I have a jupyter notebook which has ~400 cells. The total file size is 8MB so I'd like to suppress the output cells that have a large size so as to reduce the overall file size.
There are quite a few possible output cells that could be causing this (mainly matplotlib and seaborn plots) so to avoid spending time on trial and error, is there a way of finding the size of each output cell? I'd like to keep as many output plots as possible as I'll be pushing the work to github for others to see.
My idea with nbformat spelled out for running in a cell in a Jupyter notebook cell to get the code cell numbers listed largest to smallest (it will fetch a notebook example first to have something to try it on):
############### Get test notebook ########################################
import os
notebook_example = "matplotlib3d-scatter-plots.ipynb"
if not os.path.isfile(notebook_example):
!curl -OL https://raw.githubusercontent.com/fomightez/3Dscatter_plot-binder/master/matplotlib3d-scatter-plots.ipynb
### Use nbformat to get estimate of output size from code cells. #########
import nbformat as nbf
ntbk = nbf.read(notebook_example, nbf.NO_CONVERT)
size_estimate_dict = {}
for cell in ntbk.cells:
if cell.cell_type == 'code':
size_estimate_dict[cell.execution_count] = len(str(cell.outputs))
out_size_info = [k for k, v in sorted(size_estimate_dict.items(), key=lambda item: item[1],reverse=True)]
out_size_info
(To have a place to easily run that code go here and click on the launch binder button. When the session spins up, open a new notebook and paste in the code and run it. Static form of the notebook is here.)
Example I tried didn't include Plotly, but it seemed to do similar using a notebook with all Plotly plots. I don't know how it will handle a mix though. It may not sort perfectly if different kinds.
Hopefully, this gives you an idea though how to do what you wondered. The code example could be further expanded to use the retrieved size estimates to have nbformat make a copy of the input notebook without the output showing for, say, the top ten largest code cells.

How to insert python variable into Latex matrix in Jupyter notebook markdown cell

So, I am trying to make a Jupyter notebook that is slightly interactive in which I can change the number value of a variable, and then use that variable in a Markdown cell to display a Latex matrix like so:
And that cell displays this:
I don't know why that spanID thing is showing or how to get rid of it. I already have NBextensions installed and in that Markdown cell if I just type {{a_0}} then when I run that cell it just displays 1 like it should. But the moment I put it within the latex matrix, then I get the error. Any help with this is greatly appreciated.
Thanks!
The solution I applied for the problem is to create Latex objects in the code cells and displaying them in the markdown cells with Python Markdown extension of the Jupyter notebook extensions. For example, a code cell can have:
from IPython.display import Latex as lt
# Room_length [m]
l_rl = 30
l_rl_lt = lt(rf"$l_\mathrm{{rl}}={l_rl}\ \mathsf{{m}}$")
and the markdown cell can have:
The room has length size {{l_rl_lt}}.
Since variables in markdown cells are still not natively supported and it cannot be done without specific extensions (this is especially a problem in JupyterLab).
It is worth mentioning that you can also print the matrix directly from the code cell.
As an example, I will use function from my gist.
It basically looks like this:

Jupyter notebook make input cell scrollable

I have input code cell in the notebook that contains a lot of lines of code. I can't split it into different cells so its a long line of code block.
I would like to make it scrollable so that its easier to navigate within the notebook. Is it possible to do in jupyter?
Again, I need the vertical scrolling for code block (INPUT cell) not output.

plt.show() not showing data instead holding it for next plot (spyder)

I have been using the same setup for quite some time now but suddenly I am no longer allowed to plot more than one graph in a program.
Usually I can plot multiple plots after each other and let the program run through it. It executes the next lines of code after closing the first window. However, recently the first plot is not shown but instead the data is added to the last plot.
I have included a sample code which used to give me two plots but now only one.
import matplotlib.pyplot as plt
import numpy as np
random_num = np.random.randint(0,5,10)
random_num_2 = np.random.randint(0,100,10)
plt.plot(random_num, 'ko')
plt.show()
plt.plot(random_num_2, 'g*')
plt.show()
The first image shows the output from my program. But I would like to have them separated into two plots like Figure 2 and 3 show.
Maybe I should add that I am using Python 3.6 with Spyder 3.2.4. The graphics option is set to display it in Qt5 even though I tried all settings and only 'Inline' shows me the results the way I want it.
Sorry if this is a very simple question. I have tried googling but I only come up with questions about my topic where the way mine works would be the solution not the problem.
#TheresaOtt. I would suggest you create a new figure instance (plt.figure()) for each plot and use only once at the end the plt.show() command.

Categories

Resources