scikit-plot icon indicating copy to clipboard operation
scikit-plot copied to clipboard

estimators.plot_feature_importances x_tick_rotation is misaligned

Open aedavids opened this issue 1 year ago • 0 comments

When I have very long label names and use x_tick_rotation argument the xtick labels are not aligned with the bar graph Screen Shot 2024-05-12 at 5 04 57 PM

ref: ref: https://scikit-plot.readthedocs.io/en/stable/estimators.html#scikitplot.estimators.plot_feature_importances

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import plot_tree

# Load the Iris dataset
X, y = load_iris(return_X_y=True)
# print(f'type(X): {type(X)} X :\n{X}')
# print(f'type(y): {type(y)} y : \n{y}')

# Train a RandomForestClassifier
model = RandomForestClassifier(random_state=42, n_estimators=10)  # using 10 trees for simplicity
model.fit(X, y)

skplt.estimators.plot_feature_importances(
 model, feature_names=['petal length aaaaaaaaaaaaaaaaa', 
                       'petal width bbbbbbbbbbbbbbbbbbbb',
                        'sepal length cccccccccccccccccccc', 
                       'sepal width ddddddddddddddd'],
    x_tick_rotation=45
)

proposed fix

ax.set_xticks(range(len(featureNames)), featureNames,  
                     rotation=xTickRotation, ha='right', rotation_mode='anchor')

aedavids avatar May 13 '24 00:05 aedavids