import numpy as np
import matplotlib.pyplot as plt
# 苯甲酸数据
time_ben = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10,
10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5, 20]
temp_ben = [15.731, 15.808, 15.893, 15.944, 15.975, 15.993, 16.005, 16.015, 16.022, 16.029, 16.034,
16.038, 16.043, 16.044, 16.048, 16.050, 16.052, 16.054, 16.056, 16.059, 16.111, 16.469,
16.767, 17.048, 17.224, 17.331, 17.404, 17.469, 17.512, 17.546, 17.576, 17.601, 17.619,
17.635, 17.646, 17.654, 17.661, 17.667, 17.671, 17.675]
# 计算平均温度对应时间
coeff_post_ben = np.polyfit(time_ben[-20:], temp_ben[-20:], 1)
t_avg_ben = (16.830 - coeff_post_ben[1]) / coeff_post_ben[0]
# 提取10min和16min温度
T_10 = temp_ben[time_ben.index(10)] # 16.059℃
T_16 = temp_ben[time_ben.index(16)] # 17.601℃
# 插值数据曲线在t_avg处的温度
T_data_avg = np.interp(t_avg_ben, time_ben, temp_ben)
# 绘图
plt.figure(figsize=(12, 7))
plt.plot(time_ben, temp_ben, 'k-o', markersize=8, markerfacecolor='none', markeredgewidth=1.5, linewidth=1.5, label='苯甲酸')
# 绘制参考线
plt.axvline(t_avg_ben, color='purple', linestyle='--', linewidth=2, label=f'校正时间 ({t_avg_ben:.1f} min)')
plt.hlines(T_10, xmin=10, xmax=t_avg_ben, colors='red', linestyles=':', linewidth=1.5)
plt.hlines(T_16, xmin=16, xmax=t_avg_ben, colors='green', linestyles=':', linewidth=1.5)
plt.hlines(T_10, xmin=0, xmax=10, colors='red', linestyles=':', linewidth=1.5)
plt.hlines(T_16, xmin=0, xmax=16, colors='green', linestyles=':', linewidth=1.5)
plt.hlines(T_data_avg, xmin=0, xmax=t_avg_ben, colors='blue', linestyles=':', linewidth=1.5)
# 标注关键点
plt.plot(t_avg_ben, T_10, 'ro', markersize=8, markerfacecolor='none', markeredgewidth=1.5)
plt.plot(t_avg_ben, T_16, 'go', markersize=8, markerfacecolor='none', markeredgewidth=1.5)
plt.plot(t_avg_ben, T_data_avg, 'bo', markersize=8, markerfacecolor='none', markeredgewidth=1.5)
plt.annotate(f'T={T_10}℃', (0, T_10), (5,5), textcoords='offset points', color='red', fontsize=10)
plt.annotate(f'T={T_16}℃', (0, T_16), (5,5), textcoords='offset points', color='green', fontsize=10)
plt.annotate(f'T={T_data_avg:.2f}℃', (0, T_data_avg), (5,5), textcoords='offset points', color='blue', fontsize=10)
# 图表美化
plt.xlabel('时间 (分钟)', fontsize=12)
plt.ylabel('温度 (℃)', fontsize=12)
plt.title('苯甲酸温度校正图(校正温度: 16.830℃)', fontsize=14)
plt.legend(loc='lower right')
plt.grid(linestyle='--', alpha=0.6)
plt.tight_layout()
plt.show()
根据要求修改代码:请分别制作苯甲酸和萘的温度矫正图(需要的坐标轴的),要求如下:将各数据点设置为空心,外缘线用黑色,把数据用线连接起来,苯甲酸的平均温度是16.830,萘的平均温度是17.232,然后在平均温度处做一条竖直线,然后在10min和16min的点往平均温度的那条竖直线做温度趋势延长线(拟合好一点)交于两个点并标注出来,同时往纵轴做一条水平虚线段,交纵轴于两个点,数据的曲线交平均温度的那条竖直线与一个点,这个点也要往纵轴做水平虚线段,交纵轴于一点并标注出来。