使用 YellowBrick 的 KElbowVisualizer 时如何更改轴标签?

How to change axis labels when using YellowBrick's KElbowVisualizer?

我正在使用以下代码通过 KElbowVisualizer 创建轮廓系数图:

# Import the KElbowVisualizer method 

# Instantiate a scikit-learn K-Means model
model = KMeans(random_state=0)

# Instantiate the KElbowVisualizer with the number of clusters and the metric 
titleKElbow = "title"

visualizer = KElbowVisualizer(model, k=(2,7), metric='silhouette', timings=False,title = titleKElbow)

# Fit the data and visualize
visualizer.fit(df[['a','b','c']])    
visualizer.poof()  

在结果图中,x 轴标签是 'k'。如何更改结果图上的轴标签?我试过 documentation,但据我所知,它只显示了如何在 plt 样式图中添加轴标签。

您可以检索可视化工具的 ax 属性 并直接在其上使用 set_xlabel 方法:

import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from yellowbrick.cluster import KElbowVisualizer


model = KMeans(random_state=0)
visualizer = KElbowVisualizer(
    model, 
    k=(2,7), 
    metric="silhouette", 
    timings=False,
    title="custom title"
)

visualizer.fit(df[["a", "b", "c"]])
visualizer.ax.set_xlabel("custom x label")
plt.show()

感谢您查看 Yellowbrick!