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

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

用户输入:允许用户输入温度计在冰水混合物和沸水中的读数,以及一个实际温度值。

计算校准因子:根据用户输入的冰水混合物和沸水的读数,计算温度计的校准因子。

计算显示温度:根据用户输入的实际温度,计算温度计的显示温度。

绘制图像:绘制实际温度与显示温度的关系图,并在图像上标记特定点。

添加时间戳:在图像的右下角添加生成图像的时间。

haskell 复制代码
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()
相关推荐
whoarethenext9 分钟前
使用 C++ 实现 MFCC 特征提取与说话人识别系统
开发语言·c++·语音识别·mfcc
ITfeib19 分钟前
Flutter
开发语言·javascript·flutter
想躺平的咸鱼干1 小时前
Volatile解决指令重排和单例模式
java·开发语言·单例模式·线程·并发编程
Owen_Q1 小时前
Denso Create Programming Contest 2025(AtCoder Beginner Contest 413)
开发语言·算法·职场和发展
·云扬·2 小时前
【Java源码阅读系列37】深度解读Java BufferedReader 源码
java·开发语言
liulilittle2 小时前
C++ i386/AMD64平台汇编指令对齐长度获取实现
c语言·开发语言·汇编·c++
巴里巴气2 小时前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
19892 小时前
【零基础学AI】第26讲:循环神经网络(RNN)与LSTM - 文本生成
人工智能·python·rnn·神经网络·机器学习·tensorflow·lstm
JavaEdge在掘金2 小时前
Redis 数据倾斜?别慌!从成因到解决方案,一文帮你搞定
python
ansurfen3 小时前
我的第一个AI项目:从零搭建RAG知识库的踩坑之旅
python·llm