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()
  • See [this post](https://stackoverflow.com/a/62466259/12046409) – JohanC Feb 05 '23 at 18:09
  • I tried it but it didn't work – Harun Harman Feb 05 '23 at 22:36
  • Please [edit](https://stackoverflow.com/posts/75352486/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](https://stackoverflow.com/questions/62110823/matplotlib-how-to-make-the-background-transparent-on-windows))? You might want to add more information about the GUI, operating system etc you are using. – JohanC Feb 05 '23 at 23:03
  • I put the code, sorry it is my first time asking here. – Harun Harman Feb 06 '23 at 12:30

0 Answers0