1

I have this kind of an animation and I want to integrate it to my GUI. here is the plot

But, the background color is set to black right now. Here is the code. I am using Windows 10 and for GUI I am mostly using PyQt6 but for the matplotlib I used mlp.use("TkAgg") because it didn't create output if I dont use TkAgg.

I want to make it transparent. I only want the curves. I searched on the internet but everything is about save() function. Isn't there another solution for this? I don't want to save it, I am using animation, therefore it should be transparent everytime, not in a image.

import queue
import sys
from matplotlib.animation import FuncAnimation
import PyQt6.QtCore
import matplotlib as mlp
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg 
as FigureCanvas
mlp.use("TkAgg") 
import matplotlib.pyplot as plt
import numpy as np
import sounddevice as sd


plt.rcParams['toolbar'] = 'None'
plt.rcParams.update({
    "figure.facecolor":  "black",  # red   with alpha = 30%
    
}) 

# Lets define audio variables
# We will use the default PC or Laptop mic to input the sound

device = 0 # id of the audio device by default
window = 1000 # window for the data
downsample = 1 # how much samples to drop
channels = [1] # a list of audio channels
interval = 40 # this is update interval in miliseconds for plot

# lets make a queue
q = queue.Queue()
# Please note that this sd.query_devices has an s in the end.
device_info =  sd.query_devices(device, 'input')
samplerate = device_info['default_samplerate']
length  = int(window*samplerate/(1000*downsample))

plotdata =  np.zeros((length,len(channels)))

# next is to make fig and axis of matplotlib plt
fig,ax = plt.subplots(figsize=(2,1))
fig.subplots_adjust(0,0,1,1)
ax.axis("off")
fig.canvas.manager.window.overrideredirect(1)

# lets set the title
ax.set_title("On Action")

# Make a matplotlib.lines.Line2D plot item of color green
# R,G,B = 0,1,0.29

lines = ax.plot(plotdata,color = "purple")

# We will use an audio call back function to put the data in 
queue

def audio_callback(indata,frames,time,status):
    q.put(indata[::downsample,[0]])

# now we will use an another function 
# It will take frame of audio samples from the queue and update
# to the lines

def update_plot(frame):
    global plotdata
    while True:
        try: 
            data = q.get_nowait()
        except queue.Empty:
            break
        shift = len(data)
        plotdata = np.roll(plotdata, -shift,axis = 0)
        # Elements that roll beyond the last position are 
        # re-introduced 
        plotdata[-shift:,:] = data
    for column, line in enumerate(lines):
        line.set_ydata(plotdata[:,column])
    return lines
# Lets add the grid
ax.set_yticks([0])
# ax.yaxis.grid(True)

""" INPUT FROM MIC """

stream  = sd.InputStream(device = device, channels = max(channels), 
samplerate = samplerate, callback  = audio_callback)


""" OUTPUT """      


ani  = FuncAnimation(fig,update_plot,interval=interval,blit=True, )
plt.get_current_fig_manager().window.wm_geometry("200x100+850+450") 


with stream: 
    plt.show()
4
  • See this post Commented Feb 5, 2023 at 18:09
  • I tried it but it didn't work Commented Feb 5, 2023 at 22:36
  • Please edit your post and add an exact simple reproducible example of the code that you tried but didn't work. Did you also try one of the other linked answers (e.g. this)? You might want to add more information about the GUI, operating system etc you are using. Commented Feb 5, 2023 at 23:03
  • I put the code, sorry it is my first time asking here. Commented Feb 6, 2023 at 12:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.