yellowbrick icon indicating copy to clipboard operation
yellowbrick copied to clipboard

Rotate labels when many categorical labels on axis

Open morganmendis opened this issue 7 years ago • 4 comments

Need label rotation to angled text when there are too many categorical labels on an axis (example: ClassBalance report)

This was identified by [email protected] in their user study response.

morganmendis avatar Apr 05 '17 03:04 morganmendis

@rebeccabilbro ping PyLadies who were working on this project.

bbengfort avatar May 10 '17 19:05 bbengfort

This is also an issue when labels are long in Parallel Coordinates. screen shot 2017-09-07 at 11 26 22 pm

Aylr avatar Sep 08 '17 05:09 Aylr

This hasn't been closed so I thought I would add a workaround to this 2-year old issue. If you directly access the visualizer axes, you can call tick_params as follows:

visualizer.ax.tick_params(axis='x', labelrotation=90.)

Here is the output on 110 features ;-) xtick_rotation

jaydee829 avatar May 21 '19 15:05 jaydee829

@jaydee829 great workaround thank you for sharing! My only suggestion/addition is to make sure that you call this after visualizer.finalize() to make sure that it doesn't modify your labels! E.g. it would look like:

viz = Visualizer()
viz.fit_transform(X)
viz.finalize()

viz.ax.tick_params(axis='x', labelrotation=90.)
plt.show()

bbengfort avatar May 21 '19 19:05 bbengfort

I use Jupyter Notebook in VSCode and the proposed workaround does not work unless show=False is set, e.g:

visualizer = parallel_coordinates(X_train, y_train, normalize="standard", show=False)
visualizer.ax.tick_params(axis='x', labelrotation=45)

GegznaV avatar Jun 11 '23 10:06 GegznaV

Is there a way to achieve the same effect with

visualizer.ax.tick_params(axis='x', labelrotation=45)

as with ha='right' in:

import matplotlib.pyplot as plt

plt.xticks(rotation=45, ha='right');

Argument ha is not implemented in visualizer.ax.tick_params(). Is there another way to right-align labels using visualizer.ax.tick_params()?

GegznaV avatar Jun 11 '23 10:06 GegznaV

@GegznaV thank you so much for using Yellowbrick. @pdamodaran is going to take a look at your question and respond to you as soon as she can.

bbengfort avatar Jun 12 '23 15:06 bbengfort

Hi @GegznaV - based on my investigation, I believe you should be able to achieve the desired result without having to set show=False. Here is a screenshot of an example:

image

If you want to use the ha parameter, here is another option, but this results in a warning and the resulting visualization is slightly different:

image

Looking into the warning message, it appears that the community seems to be in favor of using ax.tick_params but there is no option to use ha as you have discovered.

Let me know if this answers your question or if you have any additional questions. Thanks for using Yellowbrick!

pdamodaran avatar Jun 14 '23 02:06 pdamodaran

Both examples work. As I understand, using class instead of function has more flexibility. Thank you.

I add copyable code in case anybody needs to reproduce the examples.

from yellowbrick.datasets import load_credit 
from yellowbrick.features import ParallelCoordinates
X, y = load_credit()

# Instantiate the visualizer
visualizer = ParallelCoordinates(
    features=X.columns, sample=0.05, shuffle=True, normalize="standard"
)
# Fit and transform the data to the visualizer 
visualizer.fit_transform(X, y)
visualizer.ax.set_xticklabels(visualizer.ax.get_xticklabels(), rotation=45, ha="right") 
visualizer.show()

GegznaV avatar Jun 14 '23 17:06 GegznaV

You're welcome - glad this worked out for you! And thanks for adding a code snippet for reference. I am going to close this issue. Thanks for using Yellowbrick!

pdamodaran avatar Jun 20 '23 04:06 pdamodaran