Why does Jupyter Notebook need figure and axes objects in the same cell to show a figure
I am trying to create a simple plot using matplotlib to draw a figure in Jupyter Lab. Below is a snapshot of the code.
I create two figure objects (fig_1 and fig_2) and then add axes to them. The axes show up in the first case when I have both the fig_1 and ax_1 objects in the same cell.
But in the second case, the image is not rendered, if the objects (fig_2 and ax_2 are in different cells).
The scope of these objects should extend beyond the cells.
Why does the image not appear in the second case?
Forgive me, if this sounds too trivial. But I am new to Jupyter Notebooks, Python and matplotlib. I am using Anaconda 4.5 with Python 3.6.5. I did try to look around but could not find an answer.
1 answer
See also questions close to this topic

How to merge different timeseries plot into a 2D in Python
I have three different timeseries data of the following format where the first column is timestamp and the second column is the value.
0.086206438,10 0.086425551,12 0.089227066,20 0.089262508,24 0.089744425,30 0.090036815,40 0.090054172,28 0.090377569,28 0.090514071,28 0.090762872,28 0.090912691,27
For reproduciability, I have shared the three timeseries data I am using here.
From column 2, I wanted to read current row and compare it with the value of the previous row. If it is greater, I keep comparing. If the current value is smaller than the previous row's value continue, I take the difference. Let me make it clear. For example in the above sample record I provided, the seventh row (28) is smaller than the value in the sixth row (40)  so it will be (4028 =12).
Here is my sample code.
import numpy as np import pandas as pd import csv import numpy as np import scipy.stats import matplotlib.pyplot as plt import seaborn as sns from scipy.stats import norm from statsmodels.graphics.tsaplots import plot_acf, acf protocols = {} types = {"data1": "data1.csv", "data2": "data2.csv", "data3": "data3.csv"} for protname, fname in types.items(): arr = [] arr1 = [] with open(fname, mode='r', encoding='utf8sig') as f: reader = csv.reader(f, delimiter=",") for i in reader: arr.append(int(i[1])) arr1.append(float(i[0])) arr, arr1 = np.array(arr), np.array(arr1) diffs = np.diff(arr) diffs1 = np.diff(arr1) diffs1 = diffs1[diffs > 0] diffs = diffs[diffs > 0] # To keep only the increased values protocols[protname] = { "rtime": np.array(arr1), "rdata": np.array(arr), "data": diffs, "timediff": diffs, } ## change in time for protname, values in protocols.items(): d = values["rdata"] t = values["rtime"] d = np.diff(d, 1) #/ d[:1] t = np.diff(t, 1) plt.plot(t, d, ".", label=protname, alpha=0.5) plt.xlabel("Changes in time") plt.ylabel("differences") plt.legend() plt.show()
This gives me the following plots
How can we plot the differences versus the change in time (column onw) in a twodimensitional (2D) graph of the three data I included separately?

Matplotlib  Fill_Between doesn't seem work
I'm new to python and learned a ton over the past 2 weeks. I have just started learning and experimenting with matplotlib.pylot. In my code, I have 3 seperate simple graphs on the same plot, in the third graph there are 2 lines. I am trying to have a green fill_between when y2 > y3, and red fill_between when y3 > y2. I have taken a look at other code, and they look identical to mine, but for some reason it doesn't work. Any help?
There are a few commented lines, they are just experimentation.
import matplotlib.pyplot as plt import random from matplotlib import style style.use('fivethirtyeight') def create_points(nPoints): xs = [] ys = [] for i in range(nPoints): rand = random.randrange(0,3*nPoints) xs.append(i) ys.append(rand) return xs, ys x,y = create_points(200) x1,y1 = create_points(200) x2, y2 = create_points(200) x3, y3 = create_points(200) fig = plt.figure() ax1 = plt.subplot2grid((6,1), (0,0), rowspan = 1, colspan = 1) plt.title('Subplot2grid Method') plt.ylabel('Plot 1') ax2 = plt.subplot2grid((6,1), (1,0), rowspan = 4, colspan = 1, sharex = ax1) plt.ylabel('Plot 2') ax2_xtwin = ax2.twinx() ax3 = plt.subplot2grid((6,1), (5,0), rowspan = 1, colspan = 1, sharex = ax1) plt.xlabel('x') plt.ylabel('Plot 3') ax2_xtwin.fill_between(x,0,y, facecolor = '#0079a3', alpha = 0.4) #ax2v.axes.yaxis.set_ticklables([]) ax2_xtwin.grid(False) ax2_xtwin.set_ylim(0, 1.5*max(y)) ax3.plot(x2, y2, x2, y3, linewidth = 1, label = 'x2y2 plot', color = 'k') #ax3.plot(x3, y3, linewidth = 1, label = 'x3y3 plot', color = 'firebrick') ax3.fill_between(x2, y2, y3, where = (y2 >= y3), facecolor = 'darkgreen', edgecolor = 'g', alpha = 0.5, interpolate = True) ax3.fill_between(x2, y2, y3, where = (y2 <= y3), facecolor = 'firebrick', edgecolor = 'r', alpha = 0.5, interpolate = True) #Print Points ax1.plot(x, y, linewidth = 1, label = 'xy plot', color = 'gold') ax2.plot(x1, y1, linewidth = 1, label = 'x1y1 plot', color = 'sandybrown') #ax3.plot(x2, y2, linewidth = 1, label = 'x2y2 plot', color = 'darkgreen') #ax3.plot(x3, y3, linewidth = 1, label = 'x3y3 plot', color = 'firebrick') plt.subplots_adjust(left = 0.15, bottom = 0.1, right = 0.9, top = 0.9, wspace = 0.2, hspace = 0) plt.show()

How to turn a set of data to a matrix and vectors
Suppose I have this huge set of data in a .txt file that has the following structure: first and the second column represents a discrete bidimensional domain, and the third column represents the values calculated on each point of the discrete X and Y axis. Example given below
x y z 1 1 100 1 0 50 1 1 100 0 1 50 0 0 0 0 1 50 1 1 100 0 1 50 1 1 100
It seems stupid, but I've been struggling to turn this data into vectors and matrices like X = [1, 0, 1], Y = [1, 0, 1] and Z = [[100, 50, 100], [50, 0, 50], [100, 50, 100]]. I went through many technics and methods using numpy, but coudn't manage to make it!
As a bonus: would turning this data into vectors and matrices, like I described, be a good way to plot it in a 3dscatter ou 3dcountour type using matplotlib?

Jupyter notebook not opening from Centos 7
I have recently installed jupyter notebook on Centos 7. I have been trying to get it launched on my browser from past 2 days and have tried every answer on stack overflow. It stopped showing " No web browser found " when i type it on the command terminal. But when i copy and paste the link onto the google chrome browser it's showing, This site can't be reached. How will i resolve this issue?

setting up turtle environment, getting AttributeError on Jupyter notebooks
I have tried to run turtle code in Jupyter, and I get the error:
AttributeError: '_Screen' object has no attribute 'forward'
I am able to get a turtle window to run with the same code on PyCharm and Spyder. Can anyone give me the steps on how to set up Jupyter to get turtle module to run? Is there a way to easily configure environment for turtle and Jupyter from Anaconda Navigator?
note: PyCharm interpreter is python, and Spyder was installed through Anaconda and interpreter is set to: "default ie same as Spyder's".
Thanks in advance.
import turtle wn = turtle.Screen() jack = turtle.Screen() jack.forward(150)

How do I print greek letters in jupyter
If I say
plt.title(r'$\tau$')
or
plt.xlabel(r'$\tau$')
In
jupyter
notebook, I get the greek letter tau.How do I do the same for just text?
This doesn't work:
print(r'$\tau$', tau)

jupyterlab: bokeh plot displays in wrong browser
I have a VM running jupyterlab and have opened it up for external access. When jupyterlab starts, a local browser opens up, which is fine.
Externally, I access jupyterlab and things appear to work. However, when I plot, the output doesn't show up in the browser but instead appears as a new tab in the local browser which started up with jupyterlab.
The same thing happens if I have
%matplotlib inline
.The following is a little better in that now things display inline. However, a new tab still opens up in the other browser.
%matplotlib inline from bokeh.plotting import figure, show, output_notebook from bokeh.sampledata.iris import flowers output_notebook() colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'} colors = [colormap[x] for x in flowers['species']] p = figure(title = "Iris Morphology") p.xaxis.axis_label = 'Petal Length' p.yaxis.axis_label = 'Petal Width' p.circle(flowers["petal_length"], flowers["petal_width"], color=colors, fill_alpha=0.2, size=10) # output_file("iris.html", title="iris.py example") show(p)

SageMaker jupyterlab matplotlib widgets
I can't seem to get
%matplotlib widget
working in a SageMaker jupyterlab environment.%matplotlib widget import matplotlib.pyplot as plt plt.figure() x = [1,2,3] y = [4,5,6] plt.plot(x,y) plt.show()
Just prints out:
FigureCanvasNbAgg()
As per these instructions I opened a terminal and ran:
pip install ipympl jupyter labextension install @jupyterwidgets/jupyterlabmanager jupyter labextension install jupytermatplotlib
Then restarted my jupyterlab kernel, but still have the same output.

How to download a .py file from Jupyter lab notebook?
In jupyter notebook, there is a provision to download a notebook as .py file. In jupyter lab, there is an option called export as which does not contain an option to download a .py file. Is there any workaround for this, instead of copying the script in the notebook and pasting it in a python file.