【问题标题】:Python matplotlib.pyplot Bigram Plot as Plotly PlotPython matplotlib.pyplot Bigram Plot 作为 Plotly Plot
【发布时间】:2020-10-16 14:40:00
【问题描述】:

我正在尝试将我的 PCA 和加载图组合成一个二元组。我找到了 matplotlib.pyplot 的 this 解决方案,但是,我想在 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

【问题讨论】:

    标签: python matplotlib plotly pca


    【解决方案1】:

    嗯。在浏览了更多 Plotly 文档之后,我能够找到我正在寻找的内容: https://plotly.com/python/pca-visualization/ 在“可视化加载”部分下的此链接上,他们描述了一种很好的方法:)

    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

    【讨论】:

      猜你喜欢
      • 2022-11-09
      • 2017-09-18
      • 2017-09-30
      • 1970-01-01
      • 2021-07-23
      • 2020-08-12
      • 2021-12-23
      • 2021-01-05
      • 2023-03-09
      相关资源
      最近更新 更多