0

Hi I'm new to this Site and appreciate your help. I have a variable resistor and it reads analog value and send to the python script. My objective is to plot this value using bubble plot which indicate the intensity by using radius of the circle. But I can not understand to clear the figure and update it. Your help will be highly appreciated.

import serial
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
colors = np.random.rand(1)
plt.ion()
fig=plt.figure()
ser = serial.Serial('COM12',9600)
ser.close()
ser.open()
   while True:
   data = ser.readline()
   y_val=int(data)
   print(data.decode())
   area = (10 * y_val)  # 0 to 15 point radii
   plt.scatter(5,10, s=area, c=colors, alpha=0.5)
   i += 1
   #plt.clf()
   plt.show()
   plt.pause(0.0001)  # Note this correction

1 Answers1

0

The matplotlib.animation documentation has a good example to get you started (I added some comments below). This works much faster than manually drawing and clearing the plot. Please try adapting your code using this example:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
xdata, ydata = [], []

# Create the axis for your data to be plotted on
ln, = ax.plot([], [], 'ro')

def init():
    ax.set_xlim(0, 2*np.pi)
    ax.set_ylim(-1, 1)
    return ln,

# This function is called once every frame
def update(frame):
    xdata.append(frame)
    ydata.append(np.sin(frame))
    # Update your axis with the new information
    ln.set_data(xdata, ydata)
    return ln,

ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
                    init_func=init, blit=True)
plt.show()
Mandias
  • 742
  • 5
  • 17