python:画由抛物线: y^2=2x 与直线 y=x-4 所围成的图形

《高等数学》同济大学版 P339

编写 test_diff_3_area.py 如下

python 复制代码
# -*- coding: utf-8 -*-
""" 画由抛物线: y^2=2x 与直线 y=x-4 所围成的图形 """
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

def fun(x):
    return x-4

def fun1(x):
    return np.sqrt(2*x)

def fun2(x):
    return -1*np.sqrt(2*x)


x = np.linspace(0, 10, num=100)
y = fun(x)
y1 = fun1(x)
y2 = fun2(x)

fig, ax = plt.subplots()
plt.plot(x, y,  'r', linewidth=2)
plt.plot(x, y1, 'r', linewidth=2)
plt.plot(x, y2, 'r', linewidth=2)

a = 0.0
b = 8.0
# 坐标轴设置
ax.set_xticks([0, 2, 4, 6, 8])
ax.set_yticks([])
#ax.set_xticklabels(['$a$', '$b$']) # 换成公式字体
plt.figtext(0.98, 0.05, '$x$')
plt.figtext(0.01, 0.98, '$y$') #0~1代表在图的比例处

# 绘制灰色多边形 y轴>0
ix = np.linspace(4, 8.0)
iy = fun(ix)
ixy = zip(ix, iy)
verts  = [(0, 0)] + list(ixy) + [(0, 4)]
# 多边形 Polygon
poly = Polygon(verts, facecolor='0.9', edgecolor='0.3')
ax.add_patch(poly)

ix = np.linspace(0, 8.0)
iy1 = fun1(ix)
ixy1 = zip(ix, iy1)
verts1 = [(0, 0)] + list(ixy1) + [(0, 4)]
# 多边形 Polygon
poly1 = Polygon(verts1, facecolor='1.0', edgecolor='0.3')
ax.add_patch(poly1)

# 绘制灰色多边形 y轴<0
ix = np.linspace(0.0, 2.0)
iy2 = fun2(ix)
ixy2 = zip(ix, iy2)
verts2 = [(0, 0)] + list(ixy2) + [(2, 0)]
# 多边形 Polygon
poly2 = Polygon(verts2, facecolor='0.9', edgecolor='0.3')
ax.add_patch(poly2)

ix = np.linspace(2, 4.0)
iy = fun(ix)
ixy = zip(ix, iy)
verts  = [(2, 0)] + list(ixy) + [(4, 0)]
# 多边形 Polygon
poly = Polygon(verts, facecolor='0.9', edgecolor='0.3')
ax.add_patch(poly)

# 添加 LaTex数学公式
x_math = 3
y_math = 3
latex = r'$\int_{-2}^4 (y+4 - \frac{1}{2} y^2)dy $'
plt.text(x_math, y_math, latex, fontsize=14, horizontalalignment='center')
plt.grid()
plt.show()

运行 python test_diff_3_area.py

相关推荐
web135085886351 小时前
Python大数据可视化:基于python的电影天堂数据可视化_django+hive
python·信息可视化·django
东方芷兰1 小时前
伯克利 CS61A 课堂笔记 11 —— Mutability
笔记·python
不会Hello World的小苗4 小时前
Java——列表(List)
java·python·list
m0_748235956 小时前
Python大数据可视化:基于Python的王者荣耀战队的数据分析系统设计与实现_flask+hadoop+spider
hadoop·python·flask
Dyan_csdn6 小时前
【Python项目】基于Python的Web漏洞挖掘系统
网络·python·安全·web安全
Minner-Scrapy6 小时前
DApp 开发入门指南
开发语言·python·web app
&小刘要学习&6 小时前
anaconda不显示jupyter了?
python·jupyter
jerry-896 小时前
jupyterhub_config配置文件内容
python
奔跑吧邓邓子7 小时前
【Python爬虫(36)】深挖多进程爬虫性能优化:从通信到负载均衡
开发语言·爬虫·python·性能优化·负载均衡·多进程
学长学姐我该怎么办7 小时前
年前集训总结python
python