Jupyter reload custom mplstyles - python

I want to develop custom styles for plots in matplotlib. For this reason I have a custom stylesheet (my_style.mpstyle) located in the default directory (~/.config/matplotlib/styles). I can do this with Python scripts without a problem but recently I find myself using Jupyter more. Although the stylesheets generally work, I have the problem, that changes in the stylesheet are not considered unless I restart the Kernel - is there a way of reloading the stylesheet when I execute the cell such that I can develop the style with Jupyter?
Thanks.

I got around this problem by defining a PYTHONSTARTUP file. This is a file that contains a set of python commands (usually import statements) that are automatically executed every time you launch a python interpreter, even when launching a jupyter application or restarting a kernel within one.
Here's a portion of my startup file that imports pyplot and sets a style sheet.
try:
import matplotlib.pyplot as plt
print('import matplotlib.pyplot as plt')
except:
print('Could not import matplotlib.pyplot')
try:
plt.style.use('mystylesheet')
except:
print('No mpl style sheet set; could not find "mystylesheet"')
To make python execute the startup file, you need to define the environmental variable PYTHONSTARTUP=full/path/to/startupfile. In Linux, it's quite easy; just modify your .bashrc to include
export PYTHONSTARTUP="full/path/to/startupfile"
For windows it is a little more involved, and of course the process varies from one version to another. Here is one website that appears to summarize the process for several versions.

Related

How to load Python script into Jupyter Notebook without revealing code and making it available to all cells

I'm trying to import a Python script as a module into one of my Notebooks, but the import or import importlib commands do not work in Jupyter Notebook like they do in a standard Python file and terminal.
I've come across the %load command but as far as I understand and have seen in my own Notebook, is that it only loads the contents of the script I'm trying to import into the current cell and is invisible to the other cells, and it also outputs the code in the script being imported for everyone to see.
I want to be able to load the script and make it available to all cells, as well as hiding the code for the purpose of encapsulation and also keeping the Notebook neat and tidy - only focusing on the code relevant to the topic of the Notebook. Is this possible?

PyCharm and IPython Notebook: Include files from project

Within my project XYZ, I have a file superSource.py, which contains some functions.
Now, I've used the new cool pyCharm feature of creating an IPython notebook, which I calltest test.ipynb, and saved it in the projects main directory (next to superSource.py).
However, when I run import superSource; foo = superSource.parameters() nothing happens, I don't even get a warning. pyCharm will underline superSource within the code though, warning me that there is no module called superSource.
How can I include other files from the same directory using the IPython notebook and/or pyCharm?
I've had the same problem and have a partial solution.
To include your file, add the following to a cell:
execfile("superSource.py").
This should load and execute it and make its contents available for reuse so that you can access variable and call functions defined or imported by it in other cells.
Unfortunately, PyCharm does not know about it, so that as you type, there is no statement completion and if you have "Show import popup" enabled in PyCharm, it will suggest adding an import but highlight it as an error afterwards. It should still work, however.

Synchronize .ipynb and .py to use ipython notebook and eclipse at the same time

I started programming some scripts with ipython notebook but now the project is becoming to big for a notebook. Nevertheless I love to execute my stuff in an ipython notebook (load de data only once, online figures...).
What I would want is to program everything with eclipse but executing it in ipython. I know I can save the notebooks as .py by adding the --script option at the beginning. But now I want to automatically make the process the other way around. I mean, I want my ipython notebook to reload de code I modify with Eclipse.
Is it possible?
Should I make a program that makes it using the converter?
Thanks!!
I found the solution for manually updating the functions without rerunning the whole .ipynb file. However, I do not know how to make it automated. Here is the solution:
Synchronizing code between jupyter/iPython notebook script and class methods
To cut it short you need to put reload function from importlib module in the cell of interest:
import babs_visualizations
from importlib import reload
reload(babs_visualizations)
Just a little addition: make sure that you are addressing the function in the form of moldule.function. If you previously imported function by from module import function and then reloaded the module the function will not be reloaded. You can put the function inside the notebook cell and rerun it to see, how the changes in the module affected the function output in the notebook.
I hope this was helpful for someone.

How do I load and run specific packages when I launch 'matplotlib'?

I see how to change certain settings for matplotlib in such a way that the are used to configure it each time I launch, including when I launch interactively with
ipython --pylab
but I'm not sure how run arbitrary code each time I launch in this way, or how to ensure that certain packages have been imported. For example I'd like to do the following whenever I launch as above:
from mpltools import style
style.use('ggplot')
How do I load and run specific packages when I launch 'matplotlib'?
From the ipython website it seems you can place any .py file in the startup folder of your profile and it will be run when ipython is initiated. For help finding the profile where your startup folder is [see here].
To get the effect your looking for you need to check that matplotlib has been imported, the only way I can think of doing that is to add the following
import sys
if "matplotlib" in sys.modules:
# Do something
print
print "This seems to be working"
I have left it with the test so you can quickly determine if it is working.
Two things to note: I am using ipython 2.0.0-dev (in case this has any bearing), and secondly I suspect that calling sys.modules is not recommended, or at least that was the impression I had off the SO post I borrowed the idea from, perhaps someone better than I can enlighten us.

Pyplot/Matplotlib: How to access figures opened by another interpreter?

I am using matplotlib.pyplot (with Eclipse on Windows). Every time I run my code it opens several pyplot figure windows.
The problem is that if I don't close those windows manually they accumulate. I would like to use pyplot to find those windows (opened by another process of python.exe) and re-use them. In other words, I do not want to have multiple windows for the same figure, even across interpreter processes.
Is there a simple way to do it?
There is no simple way to reuse plot windows if you must use eclipse to run it. When I am working interactively with matplotlib, I use either spyder or ipython. Edit class, reload class, and run code again. If you just want to get rid of all the open plot windows, hit the stacked stop icons to kill all your runing python instances.

Categories

Resources