Skip to content

Instantly share code, notes, and snippets.

@initbrain
Created March 16, 2017 00:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save initbrain/c79387cee71411fb679adeab2b5eda56 to your computer and use it in GitHub Desktop.
Save initbrain/c79387cee71411fb679adeab2b5eda56 to your computer and use it in GitHub Desktop.
Kivy + Matplotlib + Scipy spectrogram test
#!/usr/bin/env python2.7
# Kivy + Matplotlib + Scipy spectrogram test
# Julien Deudon (initbrain)
# 201703160114
# See :
# https://github.com/kivy-garden/garden.matplotlib/tree/master/examples
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.spectrogram.html
import matplotlib
matplotlib.use('module://kivy.garden.matplotlib.backend_kivy')
import matplotlib.pyplot as plt
import numpy as np
from scipy import signal
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvas
def press(event):
print ('press released from test', event.x, event.y, event.button)
def release(event):
print ('release released from test', event.x, event.y, event.button)
def keypress(event):
print ('key down', event.key)
def keyup(event):
print ('key up', event.key)
def motionnotify(event):
print ('mouse move to ', event.x, event.y)
def resize(event):
print ('resize from mpl ', event)
def scroll(event):
print ('scroll event from mpl ', event.x, event.y, event.step)
def close(event):
print ('closing figure')
def axes_enter(event):
print ('enter_axes', event.inaxes)
event.inaxes.patch.set_facecolor('blue')
event.canvas.draw()
def axes_leave(event):
print ('leave_axes', event.inaxes)
event.inaxes.patch.set_facecolor('black')
event.canvas.draw()
def figure_enter(event):
print ('enter_figure', event.canvas.figure)
event.canvas.figure.patch.set_facecolor('darkred')
event.canvas.draw()
def figure_leave(event):
print ('leave_figure', event.canvas.figure)
event.canvas.figure.patch.set_facecolor('black')
event.canvas.draw()
print event.canvas.figure
kv = """
<Test>:
orientation: 'vertical'
Button:
size_hint_y: None
height: 40
"""
Builder.load_string(kv)
class Test(BoxLayout):
def __init__(self, *args, **kwargs):
super(Test, self).__init__(*args, **kwargs)
self.add_plot()
def get_fc(self, i):
#print plt.style.available
with plt.style.context(('dark_background')):
figure = plt.figure()
figure.suptitle('mouse hover over figure or axes to trigger events' +
str(i))
ax1 = figure.add_subplot(211)
ax2 = figure.add_subplot(212)
wid = FigureCanvas(figure)
self.add_bindings(figure)
return wid
def get_sinusoide(self):
# Generate a test signal, a 2 Vrms sine wave whose frequency is slowly
# modulated around 3kHz, corrupted by white noise of exponentially
# decreasing magnitude sampled at 10 kHz.
fs = 10e3
N = 1e5
amp = 2 * np.sqrt(2)
noise_power = 0.01 * fs / 2
time = np.arange(N) / float(fs)
mod = 500*np.cos(2*np.pi*0.25*time)
carrier = amp * np.sin(2*np.pi*3e3*time + mod)
noise = np.random.normal(scale=np.sqrt(noise_power), size=time.shape)
noise *= np.exp(-time/5)
x = carrier + noise
return x, fs
def get_spectrogram(self, x, fs):
#print plt.style.available
with plt.style.context(('dark_background')):
figure = plt.figure()
#figure.set_facecolor('gray')
#figure.patch.set_alpha(0.3)
# Compute and plot the spectrogram.
f, t, Sxx = signal.spectrogram(x, fs)
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.pcolormesh(t, f, Sxx)
plt.show()
wid = FigureCanvas(figure)
self.add_bindings(figure)
return wid
def add_plot(self):
self.add_widget(self.get_fc(1))
#self.add_widget(self.get_fc(2))
x, fs = self.get_sinusoide()
self.add_widget(self.get_spectrogram(x, fs))
def add_bindings(self, figure):
figure.canvas.mpl_connect('button_press_event', press)
figure.canvas.mpl_connect('button_release_event', release)
figure.canvas.mpl_connect('key_press_event', keypress)
figure.canvas.mpl_connect('key_release_event', keyup)
figure.canvas.mpl_connect('motion_notify_event', motionnotify)
figure.canvas.mpl_connect('resize_event', resize)
figure.canvas.mpl_connect('scroll_event', scroll)
figure.canvas.mpl_connect('figure_enter_event', figure_enter)
figure.canvas.mpl_connect('figure_leave_event', figure_leave)
figure.canvas.mpl_connect('axes_enter_event', axes_enter)
figure.canvas.mpl_connect('axes_leave_event', axes_leave)
figure.canvas.mpl_connect('close_event', close)
class TestApp(App):
def build(self):
return Test()
if __name__ == '__main__':
TestApp().run()
@efan4ik
Copy link

efan4ik commented May 15, 2020

Kivy==1.11.1
matplotlib==3.2.1

Line 138 plt.show() should be removed otherwise it causes following error

 "created by pyplot.figure()." % err) AttributeError: 'FigureCanvasKivyAgg' object has no attribute 'manager' Figure.show works only for figures managed by pyplot, normally created by pyplot.figure().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment