QuestionBank

Matplotlib Using SymPy for plotting 3d functions on a discord bot

Matplotlib Using SymPy for plotting 3d functions on a discord bot,matplotlib,plot,discord.py,sympy,Matplotlib,Plot,Discord.py,Sympy,I am not very good with programming, but I have been developing this discord bot on python that basically aims to fulfill some of the basic math functions one can do on Wolfram Alpha. So I figured I would use SymPy to plot implicit, parametric and 3d curves using plot_implicit,plot_parametric andplot3d respectively. Firstly, say I want to plot a parametric function (4t, t^2), the code fails to recognise 4t as 4*t. How do I fix that? Secondly, the 3d plotter shows me the same graph despite the fa

I am not very good with programming, but I have been developing this discord bot on python that basically aims to fulfill some of the basic math functions one can do on Wolfram Alpha. So I figured I would use SymPy to plot implicit, parametric and 3d curves using

``plot_implicit``
,
``plot_parametric``
and
``plot3d``
respectively. Firstly, say I want to plot a parametric function
``(4t, t^2)``
, the code fails to recognise
``4t``
as
``4*t``
. How do I fix that? Secondly, the 3d plotter shows me the same graph despite the fact I put
``plt.clear()``
in my code (Which shows an error, because it keeps saying 'clear' isn't defined). For reference, here is my code :

``````x, y = symbols('x y')
plt = plot3d(x * y, (x, -10, 10), (y, -10, 10), show=false)
img = BytesIO()
plt.save(img)
img.seek(0)
await ctx.send(file=discord.File(img, "graph.png"))
plt.clear()
``````

So, how do I fix this?

#1

## Implicit multiplication

This issue was apparently solved here.

## Repeated plots

I cannot help you with this since I couldn't replicate the problem and I do not use the discord library but here are a few debugging ideas:

• Make absolutely sure it is not sending a file in the current working directory but rather from the BytesIO object.
• Send file names that are different each time like `graph1.png`, `graph2.png`, ...
• Maybe reimport `plot3d` inside the function for a completely fresh instance?

If these don't work, maybe write it in a comment so that others know that it did not solve the problem. Maybe they could help.

SymPy's plotting library is kind of buggy and fairly old. If it says that `.clear` is not an attribute then it might be best to not add that in.

## Code

Here is my code with my advice without the discord library stuff.

``````from sympy import Expr
from io import BytesIO

def send_bio(bio: BytesIO):
# I don't use the discord library so I can't help with that
# this function stands in for the send file line
# let's try to make the file name unique
with open(f"output{hash(bio)}.png", "wb") as f:
f.write(bio.getbuffer())

def my_plot(expr: Expr):
# get the x and y variables with their specific traits
# like rational=True or something like that
x = [atom for atom in expr.expr_free_symbols if str(atom) == "x"]
y = [atom for atom in expr.expr_free_symbols if str(atom) == "y"]

from sympy.plotting import plot3d
plt = plot3d(expr, (x, -10, 10), (y, -10, 10), show=False)
img = BytesIO()
plt.save(img)
img.seek(0)
send_bio(img)

def string_to_expr(s: str) -> Expr:
# https://stackoverflow.com/questions/64311324/convert-xy-to-xy
from sympy.parsing.sympy_parser import parse_expr
from sympy.parsing.sympy_parser import standard_transformations, \
implicit_multiplication_application
transformations = (standard_transformations +
(implicit_multiplication_application,))
expr = parse_expr(s, transformations=transformations)
return expr

if __name__ == '__main__':
expr = string_to_expr("2xy")
my_plot(expr)
# next plot should be different
expr = string_to_expr("x+y")
my_plot(expr)

``````