Answer the question
In order to leave comments, you need to log in
How to change the scale of the graph's drawing field in Python?
if, for example, I draw a function on a segment from 0 to 1, and the function values on this segment are huge, then the drawing turns out to be terrible, in terms of the fact that I don’t know how to make the graph drawing window always be one axes.set_aspect
("equal")
<- draws a one-to-one scale
<source lang="python">
import math
import pylab
from matplotlib import mlab
import matplotlib.patches
import matplotlib.lines
import matplotlib.path
import random
n = 5
a = 100
b = 101
dx = 0.01
k = [0] * (n+1);
t = [0] * (n+1);
def y(a):
return(math.sqrt((a-100.5)*(a-100.5) + 1)) # <- функция
c = (b - a)/(n); # <- равномерно
for j in range(0, n+1, 1):
t[j] = a + j*c
k[j] = y(t[j])
t.sort();
for i in range(n+1):
k[i] = y(t[i])
print(t[i], k[i])
def g(i, x): # start of Lagrange
l = 1;
for j in range(len(t)):
if i != j:
l = l*(x - t[j])/(t[i] - t[j])
return l
def f(x):
y = 0;
for i in range(len(t)):
y = y + k[i]*g(i,x)
return y # end of Lagrange
xlist1 = mlab.frange (t[0], t[n], dx)
xlist = mlab.frange (a , b , dx)
ylist1 = [f(x) for x in xlist]
ylist2 = [y(x) for x in xlist]
pylab.plot (xlist, ylist1)
pylab.plot (xlist, ylist2)
def drawCircles (axes):
for i in range(n+1):
circle1 = matplotlib.patches.Circle((t[i], k[i]), radius=0.01, fill=True)
axes.add_patch (circle1)
def kl(x):
y = 0
for i in range(0, n+1, 1):
if x == t[i]:
return k[i]
for i in range(0, n, 1):
if (x > t[i]) and (x < t[i+1]):
y = ((x - t[i])*(k[i+1] - k[i]))/(t[i+1]-t[i]) + k[i]
return y
ylist3 = [kl(x) for x in xlist1]
pylab.plot (xlist1, ylist3)
ymax = max(ylist3)
ymin = min(ylist3)
axes = pylab.gca()
axes.set_aspect((b-a)/(ymax-ymin))
pylab.xlim (a-(b-a)/10, b+(b-a)/10)
pylab.ylim (ymin - abs(ymin - ymax)/10, ymax + abs(ymin - ymax)/10)
drawCircles (axes)
pylab.show()
</source>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question