
作者:小小明【江湖人称"明佬"】
链接:blog.csdn.net/as604049322/article/details/118388505
注明:本文经过作者小小明授权发布,可戳原文链接关注原文作者!
本文简介
大家好,我是黄同学🚀
前段时间,我在群里面发布了一道题。没想到明佬用心了,很快记下了,马上就用Python解答出来了。
题目如下:
绘图步骤
1. 画圆
from sympy.abc import x, y
import sympy
c1, c2 = sympy.solve((x-5)**2+(y-5)**2 - 5**2, y)
display(c1)
display(c2)
display(sympy.expand(c1))
display(sympy.expand(c2))
print(str(c1))
print(str(c2))
5 - sqrt(-x*(x - 10))
sqrt(x*(10 - x)) + 5
import numpy as np
from numpy import sqrt
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(5, 5))
x = np.linspace(0, 10, 1024)
plt.plot(x, eval(str(c1)), color="r")
plt.plot(x, eval(str(c2)), color="r")
plt.xlim(-0.2, 10.2)
plt.ylim(-0.2, 10.2)
plt.show()
2. 画扇形
sympy.solve((x-10)**2+y**2 - 10**2, y)
[sqrt(x*(20 - x)), -sqrt(-x*(x - 20))]
s1, _ = sympy.solve((x-10)**2+y**2 - 10**2, y)
s1
s2, _ = sympy.solve(x**2+(y-10)**2 - 10**2, y)
s2
import numpy as np
from numpy import sqrt
import matplotlib.pyplot as plt
from sympy.abc import x, y
import sympy
%matplotlib inline
c1, c2 = sympy.solve((x-5)**2+(y-5)**2 - 5**2, y)
s1, _ = sympy.solve((x-10)**2+y**2 - 10**2, y)
s2, _ = sympy.solve(x**2+(y-10)**2 - 10**2, y)
x = np.linspace(0, 10, 1024)
plt.figure(figsize=(5, 5))
plt.plot(x, eval(str(c1)), color="r")
plt.plot(x, eval(str(c2)), color="r")
plt.plot(x, eval(str(s1)), color="b")
plt.plot(x, eval(str(s2)), color="b")
plt.xlim(-0.2, 10.2)
plt.ylim(-0.2, 10.2)
plt.show()
3. 绘制阴影
import numpy as np
from numpy import sqrt
import matplotlib.pyplot as plt
from sympy.abc import x, y
import sympy
%matplotlib inline
c1, c2 = sympy.solve((x-5)**2+(y-5)**2 - 5**2, y)
s1, _ = sympy.solve((x-10)**2+y**2 - 10**2, y)
s2, _ = sympy.solve(x**2+(y-10)**2 - 10**2, y)
x = np.linspace(0, 10, 1024)
plt.figure(figsize=(5, 5))
plt.plot(x, eval(str(c1)), label="c1")
plt.plot(x, eval(str(c2)), label="c2")
plt.plot(x, eval(str(s1)), label="s1")
plt.plot(x, eval(str(s2)), label="s2")
plt.xlim(-0.2, 10.2)
plt.ylim(-0.2, 10.2)
plt.legend()
plt.show()
from sympy.abc import x, y
x1, = sympy.solve(c1-s1, x)
x2, = sympy.solve(c1-s2, x)
x3, = sympy.solve(c2-s1, x)
x4, = sympy.solve(c2-s2, x)
x1, x2, x3, x4
(15/4 - 5*sqrt(7)/4,
25/4 - 5*sqrt(7)/4,
5*sqrt(7)/4 + 15/4,
5*sqrt(7)/4 + 25/4)
import numpy as np
from numpy import sqrt
import matplotlib.pyplot as plt
from sympy.abc import x, y
import sympy
%matplotlib inline
c1, c2 = sympy.solve((x-5)**2+(y-5)**2 - 5**2, y)
s1, _ = sympy.solve((x-10)**2+y**2 - 10**2, y)
s2, _ = sympy.solve(x**2+(y-10)**2 - 10**2, y)
x1, = sympy.solve(c1-s1, x)
x2, = sympy.solve(c1-s2, x)
x3, = sympy.solve(c2-s1, x)
x4, = sympy.solve(c2-s2, x)
x = np.linspace(0, 10, 1024)
plt.figure(figsize=(5, 5))
plt.plot(x, eval(str(c1)), label="c1")
plt.plot(x, eval(str(c2)), label="c2")
plt.plot(x, eval(str(s1)), label="s1")
plt.plot(x, eval(str(s2)), label="s2")
plt.plot([x1, x2, x3, x4], [x2, x1, x4, x3], 'rx')
plt.text(x1, x2+0.1, "p1", ha="center", va="bottom")
plt.text(x2, x1+0.1, "p2", ha="center", va="bottom")
plt.text(x3, x4+0.1, "p3", ha="center", va="bottom")
plt.text(x4, x3+0.1, "p4", ha="center", va="bottom")
plt.xlim(-0.2, 10.2)
plt.ylim(-0.2, 10.2)
plt.legend()
plt.show()
import numpy as np
from numpy import sqrt
import matplotlib.pyplot as plt
from sympy.abc import x, y
import sympy
%matplotlib inline
c1, c2 = sympy.solve((x-5)**2+(y-5)**2 - 5**2, y)
s1, _ = sympy.solve((x-10)**2+y**2 - 10**2, y)
s2, _ = sympy.solve(x**2+(y-10)**2 - 10**2, y)
x1 = sympy.solve(c1-s1, x)[0].evalf()
x2 = sympy.solve(c1-s2, x)[0].evalf()
x3 = sympy.solve(c2-s1, x)[0].evalf()
x4 = sympy.solve(c2-s2, x)[0].evalf()
x = np.linspace(0, 10, 1024)
c1, c2, s1, s2 = map(lambda s: eval(str(s)), (c1, c2, s1, s2))
plt.figure(figsize=(5, 5))
plt.plot(x, c1, label="c1")
plt.plot(x, c2, label="c2")
plt.plot(x, s1, label="s1")
plt.plot(x, s2, label="s2")
plt.fill_between(x[x <= x1], c1[x <= x1], c2[x <= x1], color="grey")
plt.fill_between(x[(x1 < x) & (x <= x3)], s1[(x1 < x) &
(x <= x3)], c2[(x1 < x) & (x <= x3)], color="grey")
plt.fill_between(x[(x2 <= x) & (x <= x4)], c1[(x2 <= x) & (
x <= x4)], s2[(x2 <= x) & (x <= x4)], color="grey")
plt.fill_between(x[x > x4], c1[x > x4], c2[x > x4], color="grey")
plt.plot([x1, x2, x3, x4], [x2, x1, x4, x3], 'rx')
plt.xlim(-0.2, 10.2)
plt.ylim(-0.2, 10.2)
plt.legend()
plt.show()
4. 绘图完整代码
import numpy as np
from numpy import sqrt
import matplotlib.pyplot as plt
from sympy.abc import x, y
import sympy
%matplotlib inline
c1, c2 = sympy.solve((x-5)**2+(y-5)**2 - 5**2, y)
s1, _ = sympy.solve((x-10)**2+y**2 - 10**2, y)
s2, _ = sympy.solve(x**2+(y-10)**2 - 10**2, y)
x1 = sympy.solve(c1-s1, x)[0].evalf()
x2 = sympy.solve(c1-s2, x)[0].evalf()
x3 = sympy.solve(c2-s1, x)[0].evalf()
x4 = sympy.solve(c2-s2, x)[0].evalf()
x = np.linspace(0, 10, 1024)
c1, c2, s1, s2 = map(lambda s: eval(str(s)), (c1, c2, s1, s2))
plt.figure(figsize=(5, 5))
plt.plot(x, c1, label="c1", color="r")
plt.plot(x, c2, label="c2", color="r")
plt.plot(x, s1, label="s1", color="b")
plt.plot(x, s2, label="s2", color="b")
plt.fill_between(x[x <= x1], c1[x <= x1], c2[x <= x1], color="grey")
plt.fill_between(x[(x1 < x) & (x <= x3)], s1[(x1 < x) &
(x <= x3)], c2[(x1 < x) & (x <= x3)], color="grey")
plt.fill_between(x[(x2 <= x) & (x <= x4)], c1[(x2 <= x) & (
x <= x4)], s2[(x2 <= x) & (x <= x4)], color="grey")
plt.fill_between(x[x > x4], c1[x > x4], c2[x > x4], color="grey")
plt.xlim(-0.2, 10.2)
plt.ylim(-0.2, 10.2)
plt.show()
积分求解面积
from sympy.abc import x, y
from sympy import integrate
import sympy
c1, c2 = sympy.solve((x-5)**2+(y-5)**2 - 5**2, y)
s1, _ = sympy.solve((x-10)**2+y**2 - 10**2, y)
s2, _ = sympy.solve(x**2+(y-10)**2 - 10**2, y)
x1 = sympy.solve(c1-s1, x)[0].evalf()
x2 = sympy.solve(c1-s2, x)[0].evalf()
x3 = sympy.solve(c2-s1, x)[0].evalf()
x4 = sympy.solve(c2-s2, x)[0].evalf()
r = integrate(c2-c1, (x, 0, x1))+integrate(c2-s1, (x, x1, x3)) + \
integrate(s2-c1, (x, x2, x4))+integrate(c2-c1, (x, x4, 10))
r.round(4)
from sympy.abc import x, y
from sympy import integrate
import sympy
c1, c2 = sympy.solve((x-5)**2+(y-5)**2 - 5**2, y)
s1, _ = sympy.solve((x-10)**2+y**2 - 10**2, y)
s2, _ = sympy.solve(x**2+(y-10)**2 - 10**2, y)
x1 = sympy.solve(c1-s1, x)[0]
x2 = sympy.solve(c1-s2, x)[0]
x3 = sympy.solve(c2-s1, x)[0]
x4 = sympy.solve(c2-s2, x)[0]
r = integrate(c2-c1, (x, 0, x1))+integrate(c2-s1, (x, x1, x3)) + \
integrate(s2-c1, (x, x2, x4))+integrate(c2-c1, (x, x4, 10))
r.evalf()


