【问题标题】: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 命令时,它都会在上一个图表上绘制
【发布时间】:2020-05-15 05:23:56
【问题描述】:

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

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"

【问题讨论】:

    标签: python numpy matplotlib plot discord.py-rewrite


    【解决方案1】:

    您希望它在之前的图表上绘制吗?如果没有,您需要在绘制之前使用以下命令清除图形:

    plt.clf() 
    

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

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多