pythonplotyellowbrick

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


I am using the following code to create a silhouette coefficient plot with 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()  

In the resulting plot the x axis label is 'k'. How can I change the axis labels on the resulting plot? I have tried the documentation, but as far as I know it only shows how to add axis labels in a plt style plot.


Solution

  • You can retrieve the ax property of the visualizer and use the set_xlabel method on it directly:

    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()
    

    Thanks for checking out Yellowbrick!