python 阉割的matplotlib让它折线图显示出来中文

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
matplotlib.rc("font",family='YouYuan')
matplotlib.use('TkAgg')
from matplotlib.dates import DateFormatter

# 读取数据(替换为你的文件路径)
data = pd.read_excel('F:NM_b3857_202507151312.xlsx')
data['时间'] = pd.to_datetime(data['时间'], format='%H%M%S')  # 解析为时间戳

# 创建图形
fig, ax1 = plt.subplots(figsize=(12, 6))

# 绘制湿度(左侧 y 轴)
ax1.set_xlabel('时间')
ax1.set_ylabel('湿度/%', color='blue')
ax1.plot(data['时间'], data['湿度'], color='blue', linestyle='--', label='湿度')
ax1.tick_params(axis='y', labelcolor='blue')

# 绘制温度(右侧 y 轴)
ax2 = ax1.twinx()
ax2.set_ylabel('温度/℃', color='red')
ax2.plot(data['时间'], data['温度'], color='tab:red', linestyle='-', label='温度')
ax2.tick_params(axis='y', labelcolor='red')

# 绘制海拔高度(第三个 y 轴)
ax3 = ax1.twinx()
ax3.spines['right'].set_position(('outward', 60))  # 调整位置避免重叠
ax3.set_ylabel('海拔高度/km', color='black')
ax3.plot(data['时间'], data['高度'], color='black', linestyle='-.', label='高度')
ax3.tick_params(axis='y', labelcolor='black')

# 设置时间格式
date_form = DateFormatter("%H:%M")
ax1.xaxis.set_major_formatter(date_form)

# 合并图例
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
lines3, labels3 = ax3.get_legend_handles_labels()
ax1.legend(lines1 + lines2 + lines3, labels1 + labels2 + labels3, loc='upper right')

plt.title('飞机催化时间')
plt.tight_layout()  # 避免标签重叠
plt.show()

赤峰

 

posted @ 2025-08-14 19:04  秋刀鱼CCC  Views(18)  Comments(0)    收藏  举报