import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
import warnings
warnings.simplefilter("ignore")
2. 设置全局字体大小
from matplotlib import rcParams
# 设置全局字体大小
rcParams['font.size']=20# 设置全局字体大小
import matplotlib.pyplot as plt
import seaborn as sns
# 设置图形大小和布局
plt.figure(figsize=(20,30))
plt.subplots_adjust(hspace=0.5)# 遍历每个列名,绘制箱线图for i, column inenumerate(df.columns,1):plt.subplot(8,4, i)# 创建子图,8行4列布局,第 i 个子图sns.boxplot(x=df[column], orient='v')# 绘制箱线图plt.title(column)# 设置子图标题plt.tight_layout()# 调整子图布局,避免重叠
plt.savefig("./img/df_box.png")
plt.show()# 显示图形
绘制混淆矩阵
for i, best_model inenumerate(best_estimators):plt.figure(figsize=(6,4))# Predict on test sety_pred = best_model.predict(sx_test)# Compute confusion matrixcm = confusion_matrix(y_test, y_pred)# Plot confusion matrix using seaborn heatmapsns.heatmap(cm, annot=True, fmt='d', cmap='Blues')plt.title(f'Confusion Matrix - {type(best_model).__name__}')plt.xlabel('Predicted labels')plt.ylabel('True labels')# Ensure the plot doesn't overlap with previous plotsplt.tight_layout()# Save the figure if neededplt.savefig(f'./img/ConfusionMatrix_{type(best_model).__name__}2.png')# Show the plotplt.show()
import matplotlib.pyplot as plt
import numpy as np# Create some mock data
t = np.arange(0.01,10.0,0.01)
data1 = np.exp(t)
data2 = np.sin(2* np.pi * t)fig, ax1 = plt.subplots()color ='tab:red'
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color=color)
ax1.plot(t, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)ax2 = ax1.twinx()# instantiate a second Axes that shares the same x-axiscolor ='tab:blue'
ax2.set_ylabel('sin', color=color)# we already handled the x-label with ax1
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)fig.tight_layout()# otherwise the right y-label is slightly clipped
plt.show()