Yellowbrick:如何增加Yellowbrick生成的图表字体大小?

3
有没有办法调整 Yellowbrick 生成的图表字体大小?我发现文字很难读。在文档中我没有找到任何相关信息。
我使用的是 Python 3.6,Yellowbrick 0.5 在 Jupyter Notebook 中。 enter image description here
1个回答

9

更新:Yellowbrick API现在使用viz.show而不是viz.poof

Yellowbrick将matplotlib封装起来以生成可视化效果,因此您可以通过直接调用matplotlib来影响图形的所有可视设置。我发现最简单的方法是通过访问Visualizer.ax属性并在那里直接设置内容,当然,您也可以直接使用plt来管理全局图形。

下面是一些生成类似于您示例的代码:

import pandas as pd 

from yellowbrick.classifier import ConfusionMatrix 
from sklearn.ensemble import AdaBoostClassifier
from sklearn.model_selection import train_test_split as tts

data = pd.read_csv('examples/data/occupancy/occupancy.csv') 

features = ["temperature", "relative humidity", "light", "C02", "humidity"]

# Extract the numpy arrays from the data frame 
X = data[features].as_matrix()
y = data.occupancy.as_matrix()

X_train, X_test, y_train, y_test = tts(X, y, test_size=0.2)

clf = AdaBoostClassifier()
viz = ConfusionMatrix(clf)

viz.fit(X_train, y_train)
viz.score(X_test, y_test)
viz.show()

这将生成以下图像:

Small Font Confusion Matrix

您可以按照以下方式在scoreshow之间开始管理图形:

viz.fit(X_train, y_train)
viz.score(X_test, y_test)

for label in viz.ax.texts:
    label.set_size(12)

viz.show()

这将生成以下图像,内部字体略大:

Large Font Confusion Matrix

这里发生的是我直接访问包含绘图所有元素的可视化器上的 matplotlib Axes 对象。网格中间的标签是 Text 对象,所以我循环遍历所有文本对象并将它们的大小设置为 12pt。如果需要,在显示之前可以使用此技术修改任何可视元素(通常我用它来在可视化上添加注释)。
但请注意,show 调用 finalize 函数,因此某些内容(例如标题、轴标签等)应在调用 show 后进行修改,或者通过短路 show 调用 finalize 然后 plt.show() 进行修改。
这段代码仅适用于 ConfusionMatrix,但我已在 Yellowbrick 库中添加了一个 issue,希望未来能使其更容易或更易读。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接