DistrictDataLabs / yellowbrick

Visual analysis and diagnostic tools to facilitate machine learning model selection.
http://www.scikit-yb.org/
Apache License 2.0
4.3k stars 559 forks source link

How not to plot legend in RadViz plot? #1286

Closed ivan-marroquin closed 2 years ago

ivan-marroquin commented 2 years ago

Hi,

Many thanks for this great package!

I was wondering if there is a way not to plot the legend in a RadViz plot. I tried different ways without success. For instance, I tried this:

fig, ax= plt.subplots(figsize= (6, 6)) visualizer= RadViz(classes= None, features= features, colormap= cmap_subgraph, ax= ax) visualizer.fit(vertex_attrs, grouping_assignments) visualizer.ax.legend().set_visible(False) visualizer.show('communities_radviz.jpg', dpi= 600)

Thanks for your help, Ivan

ivan-marroquin commented 2 years ago

I forgot to mention that I am using Python 3.9.0 and yellowbrick 1.5

rebeccabilbro commented 2 years ago

Hello @ivan-marroquin and thank you for using Yellowbrick!

In the Yellowbrick API, the show method calls an internal finalize method, which does things like adding a legend (you can see the RadViz version of finalize here). That means that even if you turn off the legend, if you later call show on the visualizer, the legend will still get added. To avoid the legend, you could do something like this:

import matplotlib.pyplot as plt
from yellowbrick.datasets import load_occupancy
from yellowbrick.features import RadViz

# Load the classification dataset
X, y = load_occupancy()

# Specify the target classes
classes = ["unoccupied", "occupied"]

# Instantiate the visualizer
visualizer = RadViz(classes=classes)

visualizer.fit(X, y) 
visualizer.transform(X) 
plt.show()

Thanks again for being a YB user!

ivan-marroquin commented 2 years ago

Hi @rebeccabilbro

Many thanks for your prompt answer. I checked my input data. Although there were no nan values, I found that there were duplicated values in some columns. So I added a bit of noise and then, I was able to generate the RadViz plot.

Hoping this tip can help other uses of this great package!

Ivan

rebeccabilbro commented 2 years ago

Ah, good catch @ivan-marroquin, and thanks so much for sharing your findings here for others to find!