The matplotlib figure suddenly doesn't display in vscode's jupyter notebook environment, even though I used:
%matplotlib inline
When I plot a figure, e.g.:
data = np.random.randn(1000, 2)
fig, ax = plt.subplots()
ax.scatter(data[:, 0], data[:, 1])
plt.show()
only return followings:
<Figure size 432x288 with 1 Axes>
Here is the versions:
matploltlib: 3.5.1
numpy: 1.22.3
ipython : 8.1.1
vscode: 1.73.1
vscode jupyter extension: 2022.9.1303220346
I tried to re-install my vscode and it works, but several days later it happens again.
So if any one knows how to solve it? Thanks!
The answer to Matplotlib plots not showing in VS Code? works. I just needed to change the display mode of jupyter renderer.
Related
when I am plotting a graph in jupyter with the code below, I can press "download plot" and get the expected png. However, when I use "pan" or "zoom" before downloading the plot, i get some distorted or even blank images like in the image below.
I am using Firefox 84.0, Jupyter 6.03 and Python 3.8.3.
Does anyone know how to fix that?
Edit: I reinstalled Anaconda which updated Jupyter to 6.1.4. Now everything works as it should.
import matplotlib.pyplot as plt
%matplotlib notebook
x = [1,2,3,4,5,6]
y = [4,2,7,15,17,11]
fig, ax = plt.subplots()
ax.plot(x,y)
The Distorted Image:
I reinstalled Anaconda which updated Jupyter to 6.1.4. Now everything works as it should.
I'm trying to generate some matplotlib figures, but I'm running into issues visualizing the x/y tick labels.
import numpy as np
import matplotlib.pyplot as plt
plt.plot(np.linspace(0, 10), np.linspace(0, 10))
This problem only occurs within a jupyter notebook - I'm able to get this working in the ipython console so I'm wondering if there is a jupyter misconfiguration).
matplotlib : 3.2.2
numpy : 1.18.1
jupyter core : 4.6.3
jupyter-notebook : 6.0.3
qtconsole : 4.7.3
ipython : 7.13.0
ipykernel : 5.2.1
jupyter client : 6.1.3
jupyter lab : not installed
nbconvert : 5.6.1
ipywidgets : 7.5.1
nbformat : 5.0.6
traitlets : 4.3.3
EDIT: If I can get a workaround by using seaborn to load another style via seaborn.set()
It's curious how this got overloaded in the first place.
Jupyter simply returns the Matplotlib figure, so I doubt that it would be a Jupyter misconfig.
However, what you could try doing is plt.xticks and plt.yticks which manually add the ticks in. You pass one parameter, which is an array of the ticks that you want to display. In this case you would pass it like this:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(np.linspace(0, 10), np.linspace(0, 10))
plt.xticks(np.arange(0, 10, 1)) # Here np.arange() generates a sequence of number starting from 0 and ending at 10, going up in increments of 1.
plt.yticks(np.arange(0, 10, 1)) # Same for the y axis
plt.show()
Two points of note here. Firstly, the %matplotlib inline in line 3 is only necessary for when you are in a Jupyter environment, as this is a special signal to the Jupyter kernel to display any plots inside the notebook.
Also, it is probably good practice to have plt.show() at the end, because if, in some case, you are not working in a Jupyter environment, the code will not return the plot unless you call this function. Jupyter is nice and extracts the plot, but just as good practice, use this line of code at the end.
Hi everyone. I am running jupyter notebook through Chrome and Windows
10. the versions are : Windows 10 Chrome Version 84.0.4147.105 (Official Build) (64-bit) I am using the magic %matplotlib notebook.
To be able to zoom in and out in the plot. The code works in other
machines with same windows and version. However, when I run it in my
personal laptop, the plot is not possible to see it:
I was trying to follow the following info found: help
I have run out of ideas, I restarted the laptop, reinstalled jupyter,
and nothing seems to work. Did anyone have a similar issue? Here is the code:
%matplotlib notebook
import matplotlib.pyplot as plt
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
import numpy as np
import pandas as pd
def g(qi,d):
fig = plt.figure()
x_1 = df['dprod_year']
y_1 = qi*np.exp(-x_1*d)
plt.scatter(x_1,y_1,marker='+')
plt.scatter(df['dprod_year'],df['qoil_1000b'],marker='o',color='grey')
plt.ylim(0,4000)
plt.xlim(0,60)
plt.grid(True,axis='both')
"""
#interact(g,qi=(0.0000,5000,10),d=(0.0000,0.02000,0.0010))
"""
interact(g,qi=widgets.FloatSlider(value=3900,min=0,max=4000,step=10,description='qi:',readout_format='.1f'),
d=widgets.FloatSlider(value=0.0061,min=0.0001,max=0.01,step=0.001,description='d:',readout_format='.5f'))
plt.show()
Thank you very much for your help.
You should try to change from %matplotlib notebook to %matplotlib inline.
If that doesn't work then try to restart the kernel.
It seems that in some cases it helps to repeat the setting of the notebook backend, i.e. try calling it twice like
%matplotlib notebook
%matplotlib notebook
An analysis for why that is can be found in this comment
I plotted 2 figures, here is the screenshot
figsize
the one (2D) on top is rendered as expected.
the one (3D) on bottom is not, the figure is so small!
setting the value of figsize does not work.
figsize=(3,3) and figsize=(13,13) give the same result!
%matplotlib inline rendered same way.
I tried different browsers, clear the cache, dose not work either.
I guess some rcParams control this, because this is reproducible only on my mac, but I don't have a clue to find it!
any clue will be appreciated.
whole coding:
%pylab inline
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(3,3))
ax = fig.gca(projection='3d')
ax.scatter(0.5, 0.5, 0.5, marker = '^')
As of notebook version 5.7.4, on MacOS 10.14.2 and Chrome 71.0.3578.98, this problem persists but can be fixed with %matplotlib inline setting by using the magic command
%config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
as given in the documentation here.
Correctly funcitoning example.
It appears to be a bug in Jupyter.
As a quick fix, you can use the notebook- instead of the inline-backend for matplotlib.
To do so, replace %matplotlib inline with %matplotlib notebook.
Upgrading matplotlib from version 3.0.0 to 3.0.2 solved the problem. If you use pip type:
pip install --upgrade matplotlib
I verified the case with Jupyter version 4.4.0.
I've found various short files that produce animations using matplotlib. In general, they work fine when run from the command line, but in PyCharm I only get a still frame.
I'm asking the same question as Matplotlib does not update plot when used in an IDE (PyCharm). There's an answer posted there, which seems to work for the original asker. When I run that code from the command line, it works fine. From PyCharm, it pauses for a long time (presumably running the animation) and then shows a still frame (that looks like either the beginning or end of the animation).
I'm running Python 3.6.2 (Anaconda) through PyCharm 2017.3.2 (Professional) on a Mac (OS 10.11.6). I created a Python project in PyCharm, pasted that code into a .py file, installed the appropriate libraries (matplotlib 2.0.2, numpy 1.13.1), and ran the program.
The only difference I can see between this and what I did on the command line is that python --version there gives:
Python 3.6.0 :: Anaconda custom (x86_64)
What else could be the problem?
According to this answer and this ticket, you can disable Show plots in tool window(File->Settings->Tools->Python Scientific) in Pycharm, and I will give an example for this solution.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro')
def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return ln,
def update(frame):
xdata.append(frame)
ydata.append(np.sin(frame))
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
init_func=init, blit=True)
plt.show()
The above (or in the link attached) did not work for me, however, i found this works well (running 2.7 with anaconda and ipython console)-
Instead of executing the script normally (using run / Shift+f10), i would first set:
%matplotlib qt5
then execute the script from pycharm console, using
runfile('/path/to/script.py')
resulting in a similar result as if i would do the same from the stand alone ipython console:
(Note - the figure is animated)