Based on my understanding from this question %matplotlib inline is used so figures can be shown in Jupyter. But figures are shown in Jupyter without using %matplotlib inline perfectly fine. for example the following code:
import numpy as np
import matplotlib.pyplot as plt
plt.plot(np.random.randn(50).cumsum())
plt.show()
So is %matplotlib inline obsolete or am I misunderstanding it's purpose?
The default matplotlib backend in a jupyter notebook is inline. You can inspect this by using print(plt.get_backend()) after loading matplotlib. For example:
import matplotlib.pyplot as plt
print(plt.get_backend())
returns module://ipykernel.pylab.backend_inline
The magic %matplotlib can be used to switch back to inline if you had switched to some other backend. The following cells can illustrate this when run in a notebook.
In [100]:
import matplotlib.pyplot as plt
plt.plot(np.random.randn(50).cumsum())
In [101]:
%matplotlib notebook
plt.plot(np.random.randn(50).cumsum())
In [103]:
%matplotlib inline
plt.plot(np.random.randn(50).cumsum())
Related
import pandas as pd
dataFrame = pd.read_excel("C:/Users/****/desktop/python folder/tensorflow/sheet.xlsx")
import seaborn as sbn
import matplotlib.pyplot as plt
sbn.pairplot(dataFrame)
Output is: PS C:\Users \ -----\Desktop\python folder> & C:/Users/------/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/-----/Desktop/python folder/tensorflow/tensorflow-101.py"
For your problem, try this.
import seaborn as sbn
import matplotlib.pyplot as plt
sbn.pairplot(dataFrame)
plt.show()
Graph will appear in new window, I guess. It's why your code is working in jupyter, and not in script.
ps. vsc is just a editor, so you can't use vsc for executing python. You are executing python script with your own python program.
#%%
from Utils.ConfigProvider import ConfigProvider
import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
config = ConfigProvider.config()
and
#%%
inspected = cv2.imread(config.data.inspected_image_path, 0)
reference = cv2.imread(config.data.reference_image_path, 0)
diff = np.abs(inspected - reference)
plt.figure()
plt.title('inspected')
plt.imshow(inspected)
plt.show()
note config.data.inspected_image_path and config.data.reference_image_path are valid paths.
No errors appear, but no images are shown as well.
Running the same code from a python file does show the image.
I have something missing from the notebook.
This happens both when running using jupyter notebook and directly from PyCharm (pro)
How do I get to see images? all other answers I found just tell me to plt.show() but this obviously does not work.
I don't mind a cv2 solution as well.
You need to set a matplotlib backend.
You can do this with
%matplotlib inline
If you want to be able to interact with the plot, use
%matplotlib notebook
I work with matplotlib. When I add the following lines, the figure is not displayed.
import matplotlib
matplotlib.use('Agg')
here is my code :
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plot
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plot.figure(figsize=(12,9))
def convert_sin_cos(x):
fft_axes = fig.add_subplot(331)
y = np.cos(x)
fft_axes.plot(x,y,'g*')
for i in range(3):
fft_axes = fig.add_subplot(332)
x=np.linspace(0,10,100)
fft_axes.plot(x,i*np.sin(x),'r+')
plot.pause(0.1)
convert_sin_cos(x)
Thanks
That's the idea!
When I run your code, the console says:
matplotlibAgg.py:15: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
How can it be useful? When you're running matplotlib code in a terminal with no windowing system, for example: a cluster (running the same code with different inputs, getting lot of results and without the need to move the data I can plot whatever I need).
I am using jupyter v1.00,Ipython v6.0 and conda v4.3.16 for creating interactive plots. I'm using the following code which is supposed to create one plot and editing it after the change, but it creates multiple plots every time the power variable is changed. why it behaves like this? is it a new thing in Ipython 6.0? I can confirm that it is working in Ipython v5.0
%matplotlib inline
from ipywidgets import interact, IntSlider
import matplotlib.pylab as plt
import numpy as np
power_slider = IntSlider(min=1, max=5)
#interact(power=power_slider)
def plot(power):
plt.figure(figsize=(10, 8))
plt.plot(np.power(range(10), power))
return plt
This works for me:
%matplotlib notebook
from ipywidgets import interact, IntSlider
import matplotlib.pylab as plt
import numpy as np
power_slider = IntSlider(min=1, max=5)
#interact(power=power_slider)
def plot(power):
plt.figure(figsize=(10, 8))
plt.plot(np.power(range(10), power))
return plt
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
Are the above statements equivalent? Which is more readable/better form?
Even though they are equivalent, I think there is a pretty good argument that the second form import matplotlib.pyplot as plt is objectively more readable:
It is generally customary to use import matplotlib.pyplot as plt and suggested in the matplotlib documentation (see http://matplotlib.org/users/pyplot_tutorial.html etc...) so this will be more familiar to most readers.
import matplotlib.pyplot as plt is shorter but no less clear.
import matplotlib.pyplot as plt gives an unfamiliar reader a hint that pyplot is a module, rather than a function which could be incorrectly assumed from the first form.
They both work the same so it is up to you which you prefer, personally I don't like typing so I would prefer the second.
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt1
print(dir(plt) == dir(plt1))
True
Yes, both are the same.
It depends on you what you prefer to import.
Personally, I like to write:
from matplotlib import pyplot as plt
because it looks clearer and cleaner to me.
Just noticed one case that makes the two statements work differently to me
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Qt5Agg')
plt.plot(list(range(10)))
The above code works well.
But if I write the second line in the other way,
import matplotlib
from matplotlib import pyplot as plt
matplotlib.use('Qt5Agg')
plt.plot(list(range(10)))
This above doensn't work and the process stops at "matplotlib.use('Qt5Agg')".
Process finished with exit code -1073741571 (0xC00000FD)
This happens in IDE like Spyder console or Pycharm console. I feel it's related to the backend used though I didn't have a clear clue.