Yellowbrick 更改图例并添加标题

oku*_*oub 3 python data-visualization matplotlib yellowbrick

我用黄砖 RadViz 创建了一个图表:

visualizer = RadViz(classes=labels)
visualizer.fit(X, y) 
visualizer.transform(X)  
visualizer.show()
Run Code Online (Sandbox Code Playgroud)

如您所见,图例覆盖了一些功能名称: 在此处输入图片说明 此外,我想编辑标题。我试过:

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

但是set_title 没有效果。使用fig.legend ,确实创建了一个新图例,但我无法删除原始图例。

怎么做到呢?

小智 7

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()
Run Code Online (Sandbox Code Playgroud)

具有自定义标题和大小的径向可视化

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()
Run Code Online (Sandbox Code Playgroud)