yellowbrick
yellowbrick copied to clipboard
Rotate labels when many categorical labels on axis
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.
@rebeccabilbro ping PyLadies who were working on this project.
This is also an issue when labels are long in Parallel Coordinates.
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 ;-)
@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()
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)
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 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.
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:
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:
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!
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()
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!