Matplotlib tick labels are not being set properly in jupyter - python

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.

Related

Maplotlib figure not show in vscode jupyter notebook

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.

Not possible to see the plot created in Jupyter notebooks when using magic: %matplotlib notebook. Windows 10 and Chrome

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

figsize does not work for matplotlib 3d plot

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.

Issue with interactive plot on matplotlib within jupyter notebook

I have this issue after selecting the zoom button on the interactive toolbar beneath the plot. When I click to draw a rectangle (for zooming in to), black lines appears. This only happens in the bottom and right-most side of the plot.
I am using Python 3.6.4 and the following packages:
matplotlib (2.1.2)
jupyter (1.0.0)
Code to reproduce issue (within jupyter notebook):
import numpy as np
from numpy.random import random
import matplotlib.pyplot as plt
%matplotlib notebook
plt.plot(random(100), random(100), '.')

Jupyter Notebook - Matplotlib keep running

I just started to use Jupiter Notebook to learn Python. while I am trying out matplotlib with this basic code:
import numpy as np
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
The kernel just keep running and nothing happen. How to resolve this? Is there an dependency issue? My newly installed matplotlib is 1.5.1, python is 3.5.2, numpy is 1.11. Running on MacBook Pro 10.11(El Capitan).
To Visualize the plots created by the matplotlib in Jupiter Notebook or ipython notebook you have add one extra line at the beginning.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
If your matplotlib version is above 1.4, and you are using IPython 3.x you have to use the below code.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
It likely showed you a Matplotlib popup, with the IP(y) logo. To show it inline you have to use some IPython magic. Run this in a cell somewhere:
%matplotlib inline
After you close the popup it will finish the statement in your kernel
It sometimes takes time until the kernel starts.
Check that the code is color-highlighted. If it is, it means that the kernel is running. Evaluate the cell again. You will notice a * beside that cell, meaning it's running.
And one more thing: Sometimes the plot is displayed but its window hides behind the notebook... Access it from the task bar

Categories