C:\Users\35323\anaconda3\python.exe C:\Users\35323\PycharmProjects\PythonProject\BYCATCH.py Traceback (most recent call last): File "C:\Users\35323\PycharmProjects\PythonProject\BYCATCH.py", line 43, in <module> arial_font = FontProperties(family='sans-serif', name='Arial', size=14) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: FontProperties.__init__() got an unexpected keyword argument 'name'
时间: 2025-06-01 12:07:42 浏览: 12
从错误信息中可以看出,`FontProperties` 的构造函数中没有 `name` 参数。因此,我们需要调整代码以正确设置字体名称。
以下是修复后的代码:
```python
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 数据读取与预处理部分省略(与原代码一致)
# 创建图表
fig, ax1 = plt.subplots(figsize=(16, 6), dpi=1200)
# 渐变柱状图
norm = plt.Normalize(df['Feature Importance'].min(), df['Feature Importance'].max())
colors = plt.cm.Blues(norm(df['Feature Importance']))
ax1.bar(df['Feature'], df['Feature Importance'], color=colors)
# 定义 Arial 字体属性
arial_font = FontProperties(family='Arial', size=14) # 使用 family 参数直接指定字体名称
# 设置 X 轴和 Y 轴标签字体为 Arial
ax1.set_xlabel("Features", fontproperties=arial_font, fontsize=18, fontweight='bold')
ax1.set_ylabel("Feature Importance", fontproperties=arial_font, fontsize=18, fontweight='bold')
# x轴颜色标记
x_colors = ['red' if feature in top_features else 'black' for feature in df['Feature']]
for tick_label, color in zip(ax1.get_xticklabels(), x_colors):
tick_label.set_color(color)
tick_label.set_fontproperties(arial_font) # 设置 X 轴刻度标签字体为 Arial
# 双Y轴设置
ax2 = ax1.twinx()
ax2.plot(df['Feature'][:n_features+1], df['Mean_ROC'][:n_features+1], color="red", marker='o', linestyle='-', linewidth=2.5)
ax2.plot(df['Feature'][n_features:], df['Mean_ROC'][n_features:], color="black", marker='o', linestyle='-', linewidth=2.5)
ax2.fill_between(df['Feature'], df['CI_Lower'], df['CI_Upper'], color='red', alpha=0.15)
# 设置右侧 Y 轴标签字体为 Arial
ax2.set_ylabel("AUC", fontproperties=arial_font, fontsize=18, fontweight='bold')
# 显示图表
plt.show()
# 保存图表
plt.savefig("output3.tiff", bbox_inches='tight', dpi=500)
```
### 解释:
1. **FontProperties 参数调整**:将 `name='Arial'` 替换为 `family='Arial'`,因为 `FontProperties` 类的构造函数中没有 `name` 参数。
2. **其余部分不变**:代码的其他部分保持不变,仍然通过 `fontproperties=arial_font` 参数将 X 轴和 Y 轴标签以及 X 轴刻度标签的字体设置为 Arial。
---
阅读全文
相关推荐


















