Trying Matplotlib animation but MatplotlibDeprecationWarning is occur

huangapple go评论50阅读模式
英文:

Trying Matplotlib animation but MatplotlibDeprecationWarning is occur

问题

I'm trying to create live data plot. This is my script:

import time
import serial
import comport as com
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def animate(i, dataList, ser):
    arduinoData_string = ser.readline().decode('ascii')  # 解码接收的Arduino数据为格式化字符串

    try:
        arduinoData_float = float(arduinoData_string)  # 转换为浮点数
        dataList.append(arduinoData_float)  # 添加到持有固定点数以进行动画的列表

    except:  # 如果数据点不合格,则跳过
        pass

    dataList = dataList[-50:]  # 修复列表大小,以便动画绘制窗口包含x个数据点

    ax.clear()  # 清除上一个数据帧
    ax.plot(dataList)  # 绘制新的数据帧

    ax.set_ylim([0, 1200])  # 设置图的Y轴限制
    ax.set_title("Arduino Data")  # 设置图的标题
    ax.set_ylabel("Value")  # 设置Y轴标题

dataList = []  # 创建用于以后使用的空列表变量

fig = plt.gcf()  # 创建Matplotlib绘图,fig是较高级别的绘图窗口
ax = fig.add_subplot(111)  # 在主fig窗口中添加子图

ser = com.ini('COM2')
time.sleep(2)  # 为Arduino串口初始化添加时间延迟

# Matplotlib动画函数,负责实时绘图
# 注意,'fargs'参数是我们传入dataList和Serial对象的地方。
# ani = FuncAnimation(fig, animate, frames=100, fargs=(dataList, ser), interval=100)

ani = animation.FuncAnimation(fig, animate, frames=100, fargs=(dataList, ser), interval=100)

plt.show()  # 保持Matplotlib绘图在屏幕上持久显示,直到关闭
ser.close()

当我尝试运行它时,我收到了这个消息: MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.

我使用的是最新版本的PyCharm,以确保使用了正确的默认后端。Matplotlib版本是3.7.1。

有人知道如何处理这个错误并修复它吗?

英文:

I'm trying to create live data plot. This is my script:

import time
import serial
import comport as com
import matplotlib.pyplot as plt
import matplotlib.animation as animation


def animate(i, dataList, ser):
    arduinoData_string = ser.readline().decode('ascii')  # Decode receive Arduino data as a formatted string
    # print(i)                                           # 'i' is a incrementing variable based upon frames = x argument

    try:
        arduinoData_float = float(arduinoData_string)  # Convert to float
        dataList.append(arduinoData_float)  # Add to the list holding the fixed number of points to animate

    except:  # Pass if data point is bad
        pass

    dataList = dataList[-50:]  # Fix the list size so that the animation plot 'window' is x number of points

    ax.clear()  # Clear last data frame
    ax.plot(dataList)  # Plot new data frame

    ax.set_ylim([0, 1200])  # Set Y axis limit of plot
    ax.set_title("Arduino Data")  # Set title of figure
    ax.set_ylabel("Value")  # Set title of y axis


dataList = []  # Create empty list variable for later use

fig = plt.gcf()  # Create Matplotlib plots fig is the 'higher level' plot window
ax = fig.add_subplot(111)  # Add subplot to main fig window

ser = com.ini('COM2')
time.sleep(2)  # Time delay for Arduino Serial initialization

# Matplotlib Animation Fuction that takes takes care of real time plot.
# Note that 'fargs' parameter is where we pass in our dataList and Serial object.
# ani = FuncAnimation(fig, animate, frames=100, fargs=(dataList, ser), interval=100)

ani = animation.FuncAnimation(fig, animate, frames=100, fargs=(dataList, ser), interval=100)

plt.show()  # Keep Matplotlib plot persistent on screen until it is closed
ser.close()

When I try to run it, I got this message: MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later..

I'm using latest PyCharm version just to be sure the right default backend is used. Matplotlib version is 3.7.1.

Does anyone have an idea how to deal with error and how to fix it?

答案1

得分: 0

I should've switched the animation backend. By default on Windows 10 in PyCharm, matplotlib uses module://backend_interagg. I switched to TkAgg backend by placing this code at the beginning of the script.

我本应该切换动画后端。在PyCharm中,默认情况下,Windows 10上的matplotlib使用module://backend_interagg。我通过将以下代码放在脚本开头来切换到TkAgg后端。

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import matplotlib.animation as animation

然后我询问ChatGPT如何改进我的代码,使其正常工作。所以,它给了我一段奇怪的代码,但它可以正常工作。现在我可以绘制实时数据!

英文:

I should've switched the animation backend. By default on Windows 10 in PyCharm, matplotlib uses module://backend_interagg. I switched to TkAgg backend by placing this code in the beggining of the script

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import matplotlib.animation as animation

Then I ask ChatGPT, how I can improve my code and make it works properly. So, It gives me an akward code, but it works. And now I can plot live data!

huangapple
  • 本文由 发表于 2023年5月6日 16:53:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76187996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定