jupyter notebook inline plots as svg - svg

By default jupyter notebook inline plots are displayed as png, e.g.:
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot()
How can you configure jupyter notebooks to display matplotlib inline plots as svg?

%config InlineBackend.figure_formats = ['svg'] does the trick. A minimal example is:
%config InlineBackend.figure_formats = ['svg']
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot()

Use set_matplotlib_formats('svg').
import matplotlib.pyplot as plt
from IPython.display import set_matplotlib_formats
%matplotlib inline
set_matplotlib_formats('svg')
plt.plot()
This is also documented in a document of %matplotlib magic.
Note: InlineBackend.figure_format is deprecated.

set_matplotlib_formats() is deprecated since May 2021.
DeprecationWarning: set_matplotlib_formats is deprecated since IPython 7.23, directly use matplotlib_inline.backend_inline.set_matplotlib_formats()
set_matplotlib_formats('svg')
Current Working method:
%matplotlib inline
import matplotlib_inline
matplotlib_inline.backend_inline.set_matplotlib_formats('svg')

Related

Counting ipywidgets Intslider steps in Jupyter notebook

I am using the IntSlider() method from the ipywidgets() class to visualize and record the count for each step of a parameter in a seaborn Kdeplot(). Any quick fix is deeply appreciated. Thanks
import ipywidgets as widgets
from ipywidgets import Play
from IPython.display import display
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
cars = sns.load_dataset('mpg')
#widgets.interact(bw=(.1,5,.1))
def bandwidth(bw=0):
sns.kdeplot(cars.horsepower,lw=3,fill=True,bw_adjust=bw)
plt.xlim(-20,200)
plt.ylim(0, 0.04);
The widgets work well, but I need a way to record/capture the count (Good and Bad) of each step move in the slider; hopefully get the data in a list or table.

"Import as" isn't recognized, normal import is

I'm playing around with matplotlib to understand its structure better and I'm confused by the following piece of code:
import matplotlib as mpl
from mpl import pyplot as plt # ModuleNotFoundError : No module named 'mpl'
mpl.pyplot # AttributeError: module 'matplotlib' has no attribute 'pyplot'
If on the other hand I abstain from importing matplotlib as a different name and execute instead
import matplotlib
from matplotlib import pyplot as plt #works!
the things work.
Even more crazy, if I "combine" these two
import matplotlib as mpl
from matplotlib import pyplot as plt #works!
mpl.pyplot.get_backend() # works
I can curiously access attributes from pyplot even if I reference it as mpl.pyplot.
What is going on here, why does
from mpl import pyplot as plt throws a ModuleNotFoundError?
import mpl.pyplot not work? Since the error message indcates that mpl was correctly resolved to matplotlib, yet still pyplot couldn't be found...
referencing pyplot as mpl.pyplot not cause an error in my last example?
(Note that I do know of course that the preferred way is to import pyplot as import matplotlib.pyplot as plt but the point of my investigation is precisely to understand what fails and why when I ventured outside the welltrodden streets of code.)
The as part in an import statement is just syntactic sugar for assigning the imported module to a variable with the given name. The import documentation describes it as such:
If the module name is followed by as, then the name following as is bound directly to the imported module.
"Bound" in this context means doing an assignment to the given name. The following statement
import someModule as someName
Is equivalent to this code:
import someModule
someName = someModule
So, when you do import matplotlib as mpl, all you do is create a variable called mpl. This has no effect on any further import statements, as import statements do not care about your local variables, but search for python packages and modules - and an import as statement cannot change the package or module name of the imported element. Which is a good thing, as you do not want your import statements to fail just because another import 5 lines earlier used a conflicting name in an as clause.
Now, why you're getting weird results with the import mpl.pyplot statement - no idea, it shouldn't work. You can see the expected behaviour if you try the following:
import os as asdf
import asdf.path #ModuleNotFoundError: no module named 'asdf'
Tested with python 3.10.2 on Archlinux. If your example is reproducible, then you might have found a weird bug or undefined behaviour in your specific python version, or have some other issue (e.g. a mpl module in your path... although that on its own wouldn't explain why you get an error message about matplotlib instead of mpl).
In conclusion, all as does is assigning a name to the imported object, so a name assigned with as cannot be used as a source in another import statement.
On package imports and why matplotlib.pyplot gives an error:
Importing a package only imports the package itself, not any of its subpackages or modules. A package can explicitly import submodules in its __init__.py, so if the matplotlib init file would contain a statement like from . import pyplot line then accessing matplotlib.pyplot would work. There are however many reasons why a package might choose not to import any submodules, such as time and resources required to import and initialize them.
Everything in python is a object, but when you import matplotlib you are importing all library but you cannot change your functions names, the library will looking for plt
from matplotlib import pyplot as plt in this case.
You can use plt directly but you cannot use mpl.plt.
When you tried it :
import matplotlib as mpl
from mpl import pyplot as plt # ModuleNotFoundError : No module named 'mpl'
mpl.pyplot
You should import correctly:
import matplotlib as mpl
from mpl import pyplot
mpl.pyplot
'As' change the module name what you going to use in your project, but not from another import. But if you use plt directly it is going works.
Try this code to understand :
import matplotlib as mpl
import matplotlib
print(type(matplotlib))
print(type(mpl))
from matplotlib import pyplot as plt# ModuleNotFoundError : No module named 'mpl'
from matplotlib import pyplot
print(type(plt))
print(type(mpl.pyplot))
plt
mpl.pyplot
When you do import module as mdl, it doesn't change the module name. It only affects other statements when you actually use it. But the name doesn't change in the actual python lib or the external library. So what you can do is this:
import matplotlib
from matplotlib import pyplot as plt
or just from matplotlib import pyplot as plt
This is my answer...

matplotlib.plot doesn't show any image or plot without any error

I'm trying to show some images on VScode in Ubuntu, but matplotlib doesn't work. For example, even the most simple code such as:
import matplotlib.pyplot as plt
s_in = plt.imread("/media/aro/New Volume/Super resolution dataset/set5/Test/BSDS100/3096.png")
plt.imshow(s_in)
plt.show()
is not working, and it doesn't show any errors or warnings. What should I do?
In case anybody has got the same problem as mine. The error was because of the backend. So I had to change it. the only backend that worked for me was WebAgg.
Just add the following code at the beginning of your code.
import matplotlib.pyplot as plt
plt.switch_backend('WebAgg')

Customized matplotlib style with matplotlibrc doesn't load

I customized my own matplotlib style with matplotibrc by using the commands:
plt.style.reload_library()
plt.style.available
I was able to retreive my cumstomized style. However, when trying to load it with the command:
import matplotlib
#matplotlib.use('TkAgg')
matplotlib.rcParams["backend"] = "TkAgg"
import matplotlib.pyplot as plt
plt.style.use('MyCustomedStyle')
It's not being loaded. As you see, I have also tried to change the backend to use TkAgg for an interactive session, since I want to have it printed out on the screen.
I'm not getting any error messages. Still, however, it is not changing to my customized style.
I have also tried the command:
plt.rcParams.update(plt.rcParamsDefault)
%matplotlib inline
What am I doing wrong?

plt.show() not working in pycharm

I am using pycharm after upgrading my python to python 3.5.
I re-run a standard code that i had in place and had a problem with plt.show()
example:
import matplotlib
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show()
The suggestion by DavidG made things worked fine. But this time when I do
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show()
i get an error saying
/apps/qtrinst/install/python/anaconda/envs/sx_anaconda/lib/python3.5/site-packages/matplotlib/__init__.py:1401: UserWarning: This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.
It didnt get this error before-not sure what happened there.
I think the problem is with your "backend". The documentation has a section entitled "What is a backend?" which will be helpful.
I'm not familiar with WebAgg but I don't think you want to be using it. A more conventional one might be TkAgg which requires Tkinger or Qt4Agg which requires PyQt4. You can switch backends using
import matplotlib
matplotlib.use("TkAgg") # Do this before importing pyplot!
import matplotlib.pyplot as plt
Try using a different Backend. It worked for me when I used QtAgg
PyQt
you will need to install some version of PyQt. At the moment:
pip install PyQt6
Specify the GUI backend
import matplotlib
matplotlib.use("QtAgg")
Try plt.show()
from matplotlib import pyplot as plt
# some code here
plt.show()
This worked flawlessly for me. Hope It worked for you too.

Resources