I am using Jupiter notebook with jupyter themes (one dork). However, I still want my figure to have a white face color and not adapt to the theme.
This can be accomplished using...
%matplotlib inline
import matplotlib.pyplot as plt
fig, ax = plt.subplots(facecolor='w')
ax.plot(np.arange(40), np.arange(40))
HOWEVER, when using %matplotlib notebook, this does not work ...
%matplotlib notebook
import matplotlib.pyplot as plt
fig, ax = plt.subplots(facecolor='w')
ax.plot(np.arange(40), np.arange(40))
Any thought on this behavior??
Related
I am just looking for some kind of working example which will allow me to have a slider and a sine wave plot whereby I can vary the frequency in a Jupyter Notebook. All the online demos that I have Googled don't work for me. The issue seems to be that the old matplotlib graph is not erased before the new one is created.
Here is my minimal example
import ipywidgets as widgets
from IPython.display import display, clear_output
import matplotlib.pyplot as plt
import numpy as np
def f(a):
clear_output(wait=True)
fig, ax = plt.subplots()
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(a*x)
ax.grid()
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.plot(x,y)
ax.set_title("y = sin(x)")
%matplotlib inline
from time import sleep
for i in range(1,10):
clear_output(wait=True)
f(i)
sleep(1)
I have also tried
out = widgets.Output(layout={'border': '1px solid black'})
with out:
widgets.interact(f, a=1)
out.clear_output()
display(out)
However nothing I try will erase the previous matplotlib graph. They just all pile up on top of each other. I admit I am floundering as I don't really understand the API that well and the example in the documentation don't work for me.
I don't expect people to fix my code as it is probably rubbish. I am just looking for a working example which will allow me to interactively control the frequency and redraw the sine wave on a Jupyter notebook.
Here is a minimal example that works for me:
from ipywidgets import interact
import ipywidgets as widgets
import matplotlib.pyplot as plt
import numpy as np
def plot(freq):
x = np.linspace(0, 2*np.pi)
y = np.sin(x * freq)
plt.plot(x, y)
interact(plot, freq = widgets.FloatSlider(value=2, min=0.1, max=5, step=0.1))
default plot:
Screenshot of notebook after moving the slider to the right:
Embed IPython in a script and run:
from IPython import embed
# code ...
embed()
%matplotlib
#^ With or without; same result
fig = plt.figure()
Can't do anything with fig at this point.
It's already shown and the window is displayed,
even though I never called show.
plt.show() # does absolutely nothing
I normally import matplotlib in IPython this way:
%matplotlib inline
import matplotlib.pyplot as plt
ax, fig = plt.subplots()
plt.plot([[1,1], [2,2]])
plt.show()
Does this help?
When using %matplotlib notebook I'm getting two plots instead of one from a pandas Series.
Code in cell is:
%matplotlib notebook
import matplotlib.pyplot as plt
fig=plt.figure()
ax1=fig.add_subplot(1,1,1)
cTitle='H-alpha plot '+galaxy[10:17]
cXAxisTitle='Galactocentric radius/pixels'
cYAxisTitle='Data counts'
ax1.set_title(cTitle)
ax1.set_xlabel(cXAxisTitle)
ax1.set_ylabel(cYAxisTitle)
ax1.grid()
ds.plot()
I'm getting Fig 1 and Fig 2:
Title, axis labels and grid lines in Fig 1 are what I want (expect) and plot in Fig 2 is also what I expect. But why am I getting two plots anyway?
Start from:
%matplotlib inline
import matplotlib.pyplot as plt
once, at the beginning of your notebook.
My suggestion is to use inline instead of notebook.
Then run:
cTitle = 'H-alpha plot '+ galaxy[10:17]
cXAxisTitle = 'Galactocentric radius/pixels'
cYAxisTitle = 'Data counts'
ax1 = ds.plot(title=cTitle, grid=True)
ax1.set(xlabel=cXAxisTitle, ylabel=cYAxisTitle);
Note the semicolon at the end of the last instruction.
Otherwise you will have additional "messages" superimposed
on your drawing.
in IPython if I use the following code, I can see the plot inline:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,2,3],[4,5,6])
but if I use this code, the graph does not appear inline or in separate window:
%matplotlib inline
import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot([1,2,3],[4,5,6])
I am running Python 3.5.2 |Anaconda 4.1.1 (64-bit)| on Windows 7
matplotlib version is 1.5.1
I need to open a bar charts in Matplotlib in a browser-Like Firefox- but I shouldn't use Bokeh in my Project. Any suggestions?
Use the WebAgg backend, which opens a browser window with the plot and is fully interactive like the Qt or GTK window.
import matplotlib as mpl
mpl.use('WebAgg')
import matplotlib.pyplot as plt
# do your plotting
plt.show()
For example:
>>> import numpy as np
>>> a=np.random.random(100)
>>> b=np.random.random(100)
>>> plt.plot(a,b)
Opens http://127.0.0.1:8988/ showing:
IPython with %matplotlib inline as demonstrated here