I’ve browsed everywhere but there is no answer that can be used as a reference for making coordinates on a sympy python Cartesian graph. Here and here I’ve studied but still can’t solve the problem I found in my code.
JavaScript
x
15
15
1
import numpy as np
2
from sympy import Array
3
from sympy import *
4
x, y = symbols("x y")
5
ekpr1 = x
6
ekpr2 = x
7
a = ([1])
8
b = ([3])
9
10
yvals1 = [ekpr.subs(x, xi) for xi in a]
11
yvals2 = [ekpr.subs(x, xi) for xi in b]
12
plot = plot(ekpr1, ekpr2, xlim=[-2, 7], ylim= [-2, 7],
13
markers=[{'args': [a, yvals1, 'ro',
14
b, yvals2, 'ro',]}])
15
I want a = 1 and b = 3 to be connected to the x and y axes. Any help will be highly appreciated.
Advertisement
Answer
This is how I would do it:
JavaScript
1
21
21
1
from sympy import *
2
x = symbols("x")
3
4
ekpr = x
5
# evaluates ekpr at known locations
6
xvals = [1, 3]
7
yvals = [ekpr.subs(x, xi) for xi in xvals]
8
# add markers to the evaluated coordinates
9
markers = [{'args': [xvals, yvals, 'ro']}]
10
11
# for each of the evaluated coordinates, draw a
12
# line starting from the y-axis going to the
13
# x-axis passing to the evaluated point
14
xvals2 = [(0, xi, xi) for xi in xvals]
15
yvals2 = [(yi, yi, 0) for yi in yvals]
16
# add these lines to the markers dictionary
17
for xv, yv in zip(xvals2, yvals2):
18
markers.append({'args': [xv, yv, 'r--']})
19
20
p = plot(ekpr, xlim=[-2, 7], ylim= [-2, 7], markers=markers)
21