我正在制作一个绘制线条的 discord 机器人,每次我多次使用 plot 命令时,它都会绘制在上一张图上

I'm making a discord bot that graphs lines, and every time i use the plot command more than once, it plots over the previous graph

当我使用 plot 命令时,我希望它对数字进行排序并使用 matplotlib.pyplot 进行绘图,但我却得到了这个令人厌恶的东西
https://cdn.discordapp.com/attachments/710208448326795404/710593242177208491/Picture69lmao.png
发生这种情况是因为每次我 运行 plot 命令都会在之前的图形上绘制,弄乱轴和所有内容。我想知道您如何防止这种情况发生。这是我的代码:

import discord
import matplotlib.pyplot as plt
import os
import numpy as np
from discord.ext import commands

@bot.command()
async def plot(self, ctx, xvals, yvals):
  xList = []
  yList = []
  for varx in xvals:
    xList.append(varx)
  for vary in yvals:
    yList.append(vary)
  xList.sort()
  yList.sort()
  x = np.array(xList)
  y = np.array(yList)
  arr = np.vstack((x, y))
  plt.plot(arr[0], arr[1])
  plt.title(f'{ctx.message.author}\'s Graph')
  plt.savefig(fname='plot')
  await ctx.send(file=discord.File('plot.png'))
  os.remove('plot.png')

我对列表进行了排序,以免弄乱坐标轴,并且我 运行 命令如下:
.plot "x values" "y values"

你想让它绘制在之前的图表上吗?如果不是,您需要在使用以下内容绘图之前清除图形:

plt.clf() 

如果你想在之前的图表上绘制,但希望坐标轴固定,你可以使用坐标轴对象固定坐标轴。

fig, ax = plt.subplots()
ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax))
ax.plot(x,y)

这里xmin, xmax, ymin, ymax可以通过获取要绘制的数据的最小值和最大值来确定。