import matplotlib.pyplot as plt
import numpy as np
# 解决中文显示问题
plt.rcParams [ 'font.sans-serif' ] = [ 'SimHei' ]
plt.rcParams [ 'axes.unicode_minus' ] = False
# 示例数据:温度及对应三种物质的密度
temperatures = np.array ( [ 0 , 20 , 40 , 60 , 80 ] ) # 温度(℃)
density_water = np.array ( [ 0.9998 , 0.9982 , 0.9922 , 0.9832 , 0.9718 ] ) # 纯水密度
density_ethanol = np.array ( [ 0.8062 , 0.7893 , 0.7725 , 0.7549 , 0.7365 ] ) # 乙醇密度
density_glycerol = np.array ( [ 1.2613 , 1.2611 , 1.2580 , 1.2549 , 1.2518 ] ) # 甘油密度
bar_width = 0.25 # 柱子宽度
x = np.arange ( len( temperatures) ) # X轴位置
# 绘制三组柱状图,错开排列避免重叠
plt.bar ( x - bar_width, density_water, width= bar_width, label= '纯水' , color= 'skyblue' )
plt.bar ( x, density_ethanol, width= bar_width, label= '乙醇' , color= 'orange' )
plt.bar ( x + bar_width, density_glycerol, width= bar_width, label= '甘油' , color= 'lightgreen' )
# 设置图表信息
plt.title ( '不同物质在多温度下的密度对比图' , fontsize= 14 )
plt.xlabel ( '温度(℃)' , fontsize= 12 )
plt.ylabel ( '密度(g/cm³)' , fontsize= 12 )
plt.xticks ( x, temperatures) # X轴刻度对应温度值
plt.legend ( ) # 显示图例
# 为柱子添加数值标签
for i, v in enumerate( density_water) :
plt.text ( i - bar_width, v + 0.005 , f'{v:.4f}' , ha= 'center' , fontsize= 8 )
for i, v in enumerate( density_ethanol) :
plt.text ( i, v + 0.005 , f'{v:.4f}' , ha= 'center' , fontsize= 8 )
for i, v in enumerate( density_glycerol) :
plt.text ( i + bar_width, v + 0.005 , f'{v:.4f}' , ha= 'center' , fontsize= 8 )
plt.tight_layout ( )
plt.show ( )
aW1wb3J0IG1hdHBsb3RsaWIucHlwbG90IGFzIHBsdAppbXBvcnQgbnVtcHkgYXMgbnAKCiMg6Kej5Yaz5Lit5paH5pi+56S66Zeu6aKYCnBsdC5yY1Bhc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