reiinakano / scikit-plot

An intuitive library to add plotting functionality to scikit-learn objects.
MIT License
2.43k stars 284 forks source link

estimators.plot_feature_importances x_tick_rotation is misaligned #121

Open aedavids opened 4 months ago

aedavids commented 4 months ago

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