Python matplotlib.pyplot 双字母图作为 Plotly 图
Python matplotlib.pyplot Bigram Plot as Plotly Plot
我正在尝试将我的 PCA 和我的加载图合并为一个二元组。我找到了 matplotlib.pyplot 的 解决方案,但是,我想在 plotly 中生成相同的图。有人可以帮我看看如何用 plotly 制作这些吗?
def myplot(score,coeff,labels=None):
xs = score[:,0]
ys = score[:,1]
n = coeff.shape[0]
scalex = 1.0/(xs.max() - xs.min())
scaley = 1.0/(ys.max() - ys.min())
plt.scatter(xs * scalex,ys * scaley, c = y)
for i in range(n):
plt.arrow(0, 0, coeff[i,0], coeff[i,1],color = 'r',alpha = 0.5)
if labels is None:
plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, featurenames[i], color = 'g', ha = 'center', va = 'center')
else:
plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, labels[i], color = 'g', ha = 'center', va = 'center')
plt.xlim(-1,1)
plt.ylim(-1,1)
plt.xlabel("PC{}".format(1))
plt.ylabel("PC{}".format(2))
plt.grid()
myplot(components,np.transpose(pca.components_))
plt.show()
使用 Pyplot 编写代码和绘图:
编辑: 因为我的问题应该更集中(: :
我可以在情节中重新描述 PCA 部分:
PlotlyPlot
但是我不知道如何甚至是否可以在这个情节中重新创建“加载情节”。
我可以在之前的地块上叠加一个 Quiver Plot 来实现我的目标吗?
它能像这样工作吗:Arrow Overlay ?
嗯。在浏览了更多 Plotly 文档之后,我找到了我要找的东西:
https://plotly.com/python/pca-visualization/ 在这个 link 的“可视化加载”部分下,他们描述了一个很好的方法来做到这一点 :)
import plotly.express as px
from sklearn.decomposition import PCA
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
df = px.data.iris()
features = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
X = df[features]
pca = PCA(n_components=2)
components = pca.fit_transform(X)
loadings = pca.components_.T * np.sqrt(pca.explained_variance_)
fig = px.scatter(components, x=0, y=1, color=df['species'])
for i, feature in enumerate(features):
fig.add_shape(
type='line',
x0=0, y0=0,
x1=loadings[i, 0],
y1=loadings[i, 1]
)
fig.add_annotation(
x=loadings[i, 0],
y=loadings[i, 1],
ax=0, ay=0,
xanchor="center",
yanchor="bottom",
text=feature,
)
fig.show()
Picture from docu
我正在尝试将我的 PCA 和我的加载图合并为一个二元组。我找到了 matplotlib.pyplot 的
def myplot(score,coeff,labels=None):
xs = score[:,0]
ys = score[:,1]
n = coeff.shape[0]
scalex = 1.0/(xs.max() - xs.min())
scaley = 1.0/(ys.max() - ys.min())
plt.scatter(xs * scalex,ys * scaley, c = y)
for i in range(n):
plt.arrow(0, 0, coeff[i,0], coeff[i,1],color = 'r',alpha = 0.5)
if labels is None:
plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, featurenames[i], color = 'g', ha = 'center', va = 'center')
else:
plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, labels[i], color = 'g', ha = 'center', va = 'center')
plt.xlim(-1,1)
plt.ylim(-1,1)
plt.xlabel("PC{}".format(1))
plt.ylabel("PC{}".format(2))
plt.grid()
myplot(components,np.transpose(pca.components_))
plt.show()
使用 Pyplot 编写代码和绘图:
编辑: 因为我的问题应该更集中(: :
我可以在情节中重新描述 PCA 部分: PlotlyPlot
但是我不知道如何甚至是否可以在这个情节中重新创建“加载情节”。
我可以在之前的地块上叠加一个 Quiver Plot 来实现我的目标吗?
它能像这样工作吗:Arrow Overlay ?
嗯。在浏览了更多 Plotly 文档之后,我找到了我要找的东西: https://plotly.com/python/pca-visualization/ 在这个 link 的“可视化加载”部分下,他们描述了一个很好的方法来做到这一点 :)
import plotly.express as px
from sklearn.decomposition import PCA
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
df = px.data.iris()
features = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
X = df[features]
pca = PCA(n_components=2)
components = pca.fit_transform(X)
loadings = pca.components_.T * np.sqrt(pca.explained_variance_)
fig = px.scatter(components, x=0, y=1, color=df['species'])
for i, feature in enumerate(features):
fig.add_shape(
type='line',
x0=0, y0=0,
x1=loadings[i, 0],
y1=loadings[i, 1]
)
fig.add_annotation(
x=loadings[i, 0],
y=loadings[i, 1],
ax=0, ay=0,
xanchor="center",
yanchor="bottom",
text=feature,
)
fig.show()
Picture from docu