代码B调用代码A+5个函数画在一张图里

import math
def add(a,b):
    return(a+b)
def subtract(a,b):
    return(a-b)
def mutiple(a,b):
    return(a*b)
def devide(a,b):
    if b==0:
        return('除数为0,无法计算')
    else:
        return(a/b)

代码A定义模块

代码B调用

import A
import inspect
# 获取 A.py 文件中的所有对象
members = inspect.getmembers(A)
# 遍历并筛选函数对象
for name, obj in members:
    if inspect.isfunction(obj):
        print(f"函数名称:{name}")

result=A.add(3,4)
print('相加的结果是',result)
result=A.subtract(3,4)
print('相减的结果是',result)
result=A.mutiple(3,4)
print('相乘的结果是',result)
result=A.devide(3,4)
print('相除的结果是',result)


from A import add
from A import subtract
from A import mutiple
from A import devide

re=add(2,8)
print('相加的结果是',re)
re=subtract(2,8)
print('相减的结果是',re)
re=mutiple(2,8)
print('相乘的结果是',re)
re=devide(2,8)
print('相除的结果是',re)



5个函数画在一张图里

import matplotlib.pyplot as plt
import numpy as np

# 创建 x 值的范围
x = np.linspace(0.1, 10, 100)

# 计算对应的 y 值
y1 = x
y2 = x**2
y3 = np.sqrt(x)
y4 = np.log(x)
y5 = 1 / x
y6 = np.sin(x)

# 创建并配置图形和子图
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(16, 8))
fig.suptitle('Graphs of Functions')

# 在第一个子图上绘制 y = x, y = x^2, y = sqrt(x), y = log(x), y = 1/x
ax1.plot(x, y1, label='y = x', color='red')
ax1.plot(x, y2, label='y = x^2', color='blue')
ax1.plot(x, y3, label='y = sqrt(x)', color='green')
ax1.plot(x, y4, label='y = log(x)', color='purple')
ax1.plot(x, y5, label='y = 1/x', color='orange')

# 添加标题和标签到第一个子图
ax1.set_title('Group of five function')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.legend()

# 在第二个子图上绘制 y = sin(x)
ax2.plot(x, y6, label='y=sin(x)', color='red')

# 添加标题和标签到第二个子图
ax2.set_title('y = sin(x)')
ax2.set_xlabel('x')
ax2.set_ylabel('y')

# 调整子图布局
plt.tight_layout()

# 显示图形
plt.show()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值