The following code display the image and audio in the top-bottom style:
Here is the test code:
import librosa
import matplotlib.pyplot as plt
import IPython.display as ipd
def plot_it(name, audio, sample_rate):
plt.figure(figsize=(8, 1))
plt.plot(audio)
plt.gca().set_title(name)
plt.show()
ipd.display(ipd.Audio(data=audio, rate=sample_rate))
Is it possible for changing the "top-bottom" style to "left-right" style for displaying the audio at the right side of the plt figure?
You can use a GridspecLayout which is similar to matplotlib's GridSpec. In order to direct to output into the needed grid cells, you can capture it using the Output widget:
import librosa
import matplotlib.pyplot as plt
import IPython.display as ipd
from ipywidgets import Output, GridspecLayout
def plot_it(name, audio, sample_rate):
grid = GridspecLayout(1, 2, align_items='center')
out = Output()
with out:
fig, ax = plt.subplots(figsize=(8, 1))
ax.plot(audio)
ax.set_title(name)
plt.close(fig)
ipd.display(ax.figure)
grid[0, 0] = out
out = Output()
with out:
ipd.display(ipd.Audio(data=audio, rate=sample_rate))
grid[0, 1] = out
ipd.display(grid)
name = 'nutcracker'
filename = librosa.example(name)
y, sr = librosa.load(filename)
plot_it(name, y, sr)
(It is essential to close the figure, otherwise you'll have double output of the figure. This is easier to do this using the OOP than the pyplot interface, that's why I changed your matplotlib code a bit)
Related
I have a problem using Seaborn relplot when trying to make an animation. I have recreated the issue I have using one of the datasets that comes with Seaborn, below.
I suspect it is something to do with the plt.figure() not being the same as the sns.relplot. Any ideas on how to make this work would be greatly received. Thanks.
%matplotlib inline
import seaborn as sns
import pandas as pd
from matplotlib import pyplot as plt
from celluloid import Camera
from IPython.display import HTML
import ffmpeg
df = sns.load_dataset('car_crashes')
f = plt.figure(figsize=(3,3))
camera = Camera(f)
# This might seem a little bit unnecessary, but its emulating the way I am plotting my other data source:
for i in range(0, len(df), 10):
g = sns.relplot(x='total', y='abbrev', hue='abbrev', data=df.iloc[i:i+10 , [0,7]])
plt.axis('off')
plt.title(f'THIS IS THE TITLE OF {i}')
plt.gca().set_aspect('equal')
camera.snap()
animation = camera.animate()
HTML(animation.to_html5_video())
I have a function that creates a figure and for some reason it is shown in Jupyter notebook twice, even though I didn't run show at all. I pass the fig and ax as an output of this function, and plan to show it only later.
I get confused between plt, fig and ax functionaries and guess that the answer is hidden somewhere there.
Here is an anonymised version of my code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
%matplotlib inline
def plot_curve(dummydata):
# builds a chart
fig,ax = plt.subplots(1) # get subplots
fig.set_figheight(7)
fig.set_figwidth(12) #set shape
plt.plot(dummydata.x1, dummydata.y1,label = 'l1') #curve 1
plt.plot(dummydata.x2, dummydata.y2,label = 'l2') #curve2
plt.xlabel('xlabel') #labels
plt.ylabel('xlabel')
plt.yscale('linear') #scale and bounds
plt.ylim(0,100)
ymin,ymax= ax.get_ylim()
ax.axhline(1, color='k', linestyle=':', label = 'lab1') #guideline - horizontal
ax.axvline(2, color='r',linestyle='--', label = 'lab2') #guideline - vertical
ax.axvline(3, color='g',linestyle='--', label = 'lab3') #guideline - vertical
ax.arrow(1,2,3,0, head_width=0.1, head_length=0.01, fc='k', ec='k') # arrow
rect = mpl.patches.Rectangle((1,2), 2,3, alpha = 0.1, facecolor='yellow',
linewidth=0 , label= 'lab4') #yellow area patch
ax.add_patch(rect)
plt.legend()
plt.title('title')
return fig,ax
and then call it with:
for i in range(3):
dummydata = pd.DataFrame({
'x1':np.arange(1+i,100,0.1),
'y1':np.arange(11+i,110,0.1),
'x2':np.arange(1+i,100,0.1),
'y2':np.arange(21+i,120,0.1)
})
fig,ax = plot_curve(dummydata) #get the chart
What should I change to not show the figure by default, and show it only by my command?
Thanks
Try disabling matplotlib interactive mode using plt.ioff(). With interactive mode disabled the plots will only be shown with an explicit plt.show().
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
%matplotlib inline
# Desactivate interactive mode
plt.ioff()
def plot_curve(dummydata):
# the same code as before
Then in another cell
for i in range(3):
dummydata = pd.DataFrame({
'x1':np.arange(1+i,100,0.1),
'y1':np.arange(11+i,110,0.1),
'x2':np.arange(1+i,100,0.1),
'y2':np.arange(21+i,120,0.1)
})
# I'am assuming this should not be in the for loop
# The plot will NOT be shown because we are not in interactive mode
fig, ax = plot_curve(dummydata) #get the chart
No plot will be shown yet.
Now in another cell
# Now ANY plot (figure) which was created and not shown yet will be finally shown
plt.show()
The plot is finally shown. Note that if you have created several plots all of them will be shown now.
Try this:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
%matplotlib
With this importing you should not see the figure after plotting.
But you can see the figure by writing fig to IPython cell:
dummydata = pd.DataFrame({
'x1':np.arange(1,100,0.1),
'y1':np.arange(11,110,0.1),
'x2':np.arange(1,100,0.1),
'y2':np.arange(21,120,0.1)
})
fig,ax = plot_curve(dummydata) #get the chart
fig # Will now plot the figure.
Is this the desired output?
I am producing a few hundred matplotlib plots, I work in Jupyter Notebook. Each have it's own title. I want to be able to search for these titles. So when I download the file as html, and open it in browser, I'd like to find the title via using ctrl-f. How can I do that?
More details, here is an MCVE:
import matplotlib.pyplot as plt
x=range(5);y=range(5)
for i in range(6):
plt.figure()
plt.plot(x,y)
plt.title("Title"+str(i))
This produces nice plots, titled Title0 Title1 ... Title5. Howevere, these titles are part of the file and not searchable by ctrl-f, or browser doesn't detect them as text, though it would be desired.
I can do it in gnuplot but now I'd like to stick to Python.
You can generate markdown within a Jupyter notebook. Try this:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from IPython.display import display, Markdown
display(Markdown('Title of graph goes here'))
x = np.linspace(0, 10, 30)
y = np.sin(x)
plt.plot(x, y, 'o', color='black');
Edit: I've just realised that in your example all the titles will be printed before the graphs. The solution is:
import matplotlib.pyplot as plt
# DO NOT USE %matplotlib inline
x=range(5);y=range(5)
for i in range(6):
ax = plt.figure()
_ = plt.plot(x,y)
title = "Title"+str(i)
display(Markdown(title))
display(ax)
You may print title for every figure (plt.show() necessary in this case):
import matplotlib.pyplot as plt
x=range(5);y=range(5)
for i in range(2):
plt.figure()
plt.plot(x,y)
plt.title("Title"+str(i))
print("Title"+str(i))
plt.show()
I am trying to change content of an image interactively using a slider (e.g. for applying a median operation with different kernel sizes).
While this works well if I only show one resulting image (cf commented lines), I run into trouble when using the subplot function, since the image will not get updated.
What am I missing?
%matplotlib inline
from ipywidgets import interact, widgets
import matplotlib.pyplot as plt
import warnings
from skimage.morphology import disk
from skimage.filters import rank
from skimage.color import rgb2gray
import skimage.data
def f(Median_Size):
selem = disk(int(Median_Size))
with warnings.catch_warnings():
warnings.simplefilter("ignore")
img_median = rank.median(img_gray, selem=selem)
ax_neu.imshow(img_median, cmap="gray")
fig.canvas.draw()
#plt.imshow(img_median, cmap="gray") #This would work
#plt.show()
image = skimage.data.camera() #plt.imread("Test.png")
img_gray = rgb2gray(image)
fig = plt.figure(figsize=(6, 4))
ax_orig = fig.add_subplot(121)
ax_neu = fig.add_subplot(122)
ax_orig.imshow(img_gray, cmap="gray")
ax_neu.imshow(img_gray, cmap="gray")
interact(f, Median_Size=widgets.IntSlider(min=1,max=21,step=2,value=1))
Using %matplotlib notebook
Instead of the inline backend, you may use the notebook backend. This will allow to call figure.canvas.draw() as expected from running the code as a script. Replace the line %matplotlib inline by
%matplotlib notebook
and restart the Kernel.
Using display
You may display the newly changed figure after it has been changed. The drawback is that it creates the output twice. A workaround would then be to put interact in a new cell and capture the output from the first cell.
%%capture
%matplotlib inline
from ipywidgets import interact, widgets
from IPython.display import display
import matplotlib.pyplot as plt
import warnings
from skimage.morphology import disk
from skimage.filters import rank
from skimage.color import rgb2gray
import skimage.data
def f(Median_Size):
selem = disk(int(Median_Size))
with warnings.catch_warnings():
warnings.simplefilter("ignore")
img_median = rank.median(img_gray, selem=selem)
ax_neu.imshow(img_median, cmap="gray")
fig.canvas.draw()
display(fig)
image = skimage.data.camera() #plt.imread("Test.png")
img_gray = rgb2gray(image)
fig = plt.figure(figsize=(6, 4))
ax_orig = fig.add_subplot(121)
ax_neu = fig.add_subplot(122)
ax_orig.imshow(img_gray, cmap="gray")
ax_neu.imshow(img_gray, cmap="gray")
In a new cell
interact(f, Median_Size=widgets.IntSlider(min=1,max=21,step=2,value=1));
The output would then look like:
How does matplotlib ensure that a dataset can be within plot with specified size.
How do i from a plot stored as numpy, How do i read the color of the pixels illustration a datapoint (0,4) - in the plot.
example:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image
import librosa
import librosa.display
from matplotlib import cm
fig = plt.figure(figsize=(12,4))
min = -1.828067
max = 22.70058
data = np.random.uniform(low=min, high=max, size=(474,40))
librosa.display.specshow(data.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)
plt.show()
raw_input("sadas")
convert = plt.get_cmap(cm.jet)
numpy_output_static = convert(data.T)
plt.imshow(numpy_output_static, aspect = 'auto')
plt.show()
raw_input("asds")
First plot being :
Second plot being:
so the first has been resized to plot size 12,4 where the last basically plots the same data but just using the data shape as size... how do i change that?
Librosa just performs pcolormesh according to the GitHub source code
You need to define another figure with its own size for the second figure.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image
import librosa
import librosa.display
from matplotlib import cm
fig = plt.figure(figsize=(12,4))
min = -1.828067
max = 22.70058
data = np.random.uniform(low=min, high=max, size=(474,40))
librosa.display.specshow(data.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)
plt.show()
raw_input("sadas")
convert = plt.get_cmap(cm.jet)
numpy_output_static = convert(data.T)
fig = plt.figure(figsize=(12,4))
plt.imshow(numpy_output_static, aspect = 'auto')
plt.show()
raw_input("asds")