pythonmatplotlibdata-visualizationyellowbrick

Yellowbrick change legend and add title


I created a graph with yellowbrick RadViz:

visualizer = RadViz(classes=labels)
visualizer.fit(X, y) 
visualizer.transform(X)  
visualizer.show()

As you can see, the legend overrides some of the feature names: enter image description here Moreover, I want to edit the title. I tried with:

visualizer.ax.set_title("new title")
visualizer.fig.legend(bbox_to_anchor=(1.02, 1), loc=0, borderaxespad=0., title = "level")

But set_title had no effect. using fig.legend , a new legend was indeed created but I couldn't remove the original legend.

How can it be done?


Solution

  • You can modify the title of a Yellowbrick plot using the title parameter, and use the size parameter to increase the size of the axes, which may help with overlapping labels. Size is specified as a tuple of pixel dimensions:

    from yellowbrick.features import RadViz
    from yellowbrick.datasets import load_occupancy
    
    
    X, y = load_occupancy()
    
    visualizer = RadViz(
        classes=["occupied", "vacant"], 
        title="My custom title", 
        size=(800, 600)
    )
    visualizer.fit(X, y)
    visualizer.transform(X)
    visualizer.show()
    

    Radial visualization with custom title and size

    Alternatively, it is possible to skip the step of adding the Yellowbrick legend and title by circumventing the visualizer's show() and finalize() methods, and then directly modifying the ax object using whatever custom legend position you need for your plot:

    from yellowbrick.features import RadViz
    from yellowbrick.datasets import load_occupancy
    
    
    X, y = load_occupancy()
    
    visualizer = RadViz()
    visualizer.fit(X, y)
    visualizer.transform(X)
    
    custom_viz = visualizer.ax
    custom_viz.set_title("New title")
    custom_viz.figure.legend(
        bbox_to_anchor=(1.02, 1), 
        borderaxespad=0.0,
        title="level",
        loc=0,
    )
    custom_viz.figure.show()