Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sympy plotting graph upside down?

Am using sympy plotting backend. The graphs of functions are being inverted and I don’t know why. For example y=x^2 is upsidedown with the negative y axis on top.

from sympy import *
from sympy.parsing.sympy_parser import *
import spb
import sympy as sympy


def queryMatplotlibEq(qVal,labels, color, xBounds, yBounds):
    toShow = None
    if (labels == "true"):
        toShow = "center"

    qVal = qVal.replace('^', "**")

    x, y = symbols('x y', real=True)

    lhs, rhs = qVal.split('=')

    lhs = eval(lhs)
    rhs = eval(rhs)

    expr = lhs - rhs
    sols = solve(expr, y)

    plot = spb.plot(*sols, (x, xBounds[0], xBounds[1]), {"color": f"{color}", "markersize": "1"}, legend=False, aspect="equal", ylim=(
        yBounds[0], yBounds[1]), xlim=(xBounds[0], xBounds[1]), grid=toShow, show=False, adaptive=False, n=70000, is_point=True, axis_center=(0, 0))

    plot.show()
question = "y=x**2"
x, y = symbols("x y")
# [[-2.0, 2.3], [26.75, 31.0]]


xExtents = [-13, 13]
yExtents = [10, -30]

queryMatplotlibEq(question,"true","blue" ,xExtents,yExtents)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

yExtents gets passed to matplotlib’s set_ylim. If the first value is negative and the second is positive you’ll get what you expect. Instead, you specified the first value positive and the second value negative, hence you get a reverse y-axis.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading