我有下面的代码,我想放一个标题,我怎么做。原来的标题是"Principal component plot“,所以我想换成另一个。
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from yellowbrick.datasets import load_credit
from yellowbrick.features import PCA
import matplotlib.pyplot as plt
# Program extracting first column
text = data.iloc[:,0]
lable = data.iloc[:,1]
vecot_tfidf = CountVectorizer(ngram_range=(1,1))
en_vec = vecot_tfidf.fit_transform(text)
fe = en_vec.toarray()
visualizer = PCA(scale=False, colors=['red','blue','green'])
visualizer.fit_transform(fe, lable)
visualizer.show()发布于 2020-04-15 14:24:21
类PCA配置了一个默认标题"Principal Component Plot“,因此,实例化时设置标题将不起作用。因此,我们扩展了该类并将其配置为在实例化时接受title。show()方法完成绘图(添加标题、轴标签等),然后呈现图像。finalize()是为渲染准备图形的函数,添加图例、标题、轴标签等最后的修饰。因此,您必须创建一个PCA方法,定义一个扩展PCA或其一个子类的类,然后实现几个方法。
from yellowbrick.features import PCA
class myPCA(PCA):
def __init__(self, ax=None, **kwargs):
super(myPCA, self).__init__(ax, **kwargs)
def fit(self, X, y=None):
self.draw(X)
return self
def draw(self, X):
self.ax.plt(X)
return self.ax
def finalize(self):
self.set_title()
#intantiating myPCA works with title
visualizer = myPCA(scale=False, colors=['red','blue','green'],title = "My title")
#intantiating PCA will not work with title. You can uncomment following line and check
#visualizer = PCA(scale=False, colors=['red','blue','green'],title = "My title")
visualizer.show()我希望这能起作用!
https://stackoverflow.com/questions/61222445
复制相似问题