【物理教学】不准确温度计图像代码分享

这段Python代码用于绘制温度计校准的图像。它包括以下功能:

用户输入:允许用户输入温度计在冰水混合物和沸水中的读数,以及一个实际温度值。
计算校准因子:根据用户输入的冰水混合物和沸水的读数,计算温度计的校准因子。
计算显示温度:根据用户输入的实际温度,计算温度计的显示温度。
绘制图像:绘制实际温度与显示温度的关系图,并在图像上标记特定点。
添加时间戳:在图像的右下角添加生成图像的时间。
在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime

# 设置中文字体和负号正常显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

def calculate_calibration_factor(T_ice_reading, T_boiling_reading, T_ice_actual, T_boiling_actual):
    # 计算校准因子
    return (T_boiling_actual - T_ice_actual) / (T_boiling_reading - T_ice_reading)

def calculate_thermometer_reading(T_actual, calibration_factor, T_ice_reading, T_ice_actual):
    # 计算温度计的读数
    return (T_actual - T_ice_actual) / calibration_factor + T_ice_reading

# 用户输入
T_ice_reading = float(input("请输入温度计在冰水混合物中的读数:"))
T_boiling_reading = float(input("请输入温度计在沸水中的读数:"))
T_ice_actual = 0  # 冰水混合物的实际温度
T_boiling_actual = 100  # 标准大气压下沸水的实际温度

# 计算校准因子
calibration_factor = calculate_calibration_factor(T_ice_reading, T_boiling_reading, T_ice_actual, T_boiling_actual)

# 用户输入实际温度
T_actual = float(input("请输入实际温度:"))

# 计算温度计的读数
T_r = calculate_thermometer_reading(T_actual, calibration_factor, T_ice_reading, T_ice_actual)
print(f"在实际温度 {T_actual}℃ 下,温度计的读数为:{T_r:.2f}℃")

# 获取当前时间
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

# 绘制图像
plt.figure(figsize=(10, 5))
plt.plot([0, 100], [T_ice_reading, T_boiling_reading], label='显示温度', color='blue')
plt.plot([0, 100], [0, 100], label='真实温度', linestyle='--', color='red')

# 相交点
plt.scatter(T_actual, T_r, color='black', edgecolor='red', s=80, marker='o', facecolors='none')  # 绘制数据点

# 向横纵坐标轴做垂线
plt.axvline(x=T_actual, color='black', linestyle='--')
plt.axhline(y=T_r, color='black', linestyle='--')

# 标记数值
plt.text(T_actual, T_r + 2, f'({T_actual:.0f}°C, {T_r:.2f}°C)', color='red', verticalalignment='bottom')

# 添加生成图像的时间
plt.text(0.95, 0.05, f'生成时间:{current_time}', horizontalalignment='right', verticalalignment='bottom', transform=plt.gca().transAxes)

plt.title('温度校准图像')
plt.xlabel('实际温度 (°C)')
plt.ylabel('显示温度 (°C)')
plt.legend()
plt.grid(True)
plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值