【问题标题】:How to change dot types in Python prince如何在 Python Prince 中更改点类型
【发布时间】:2021-01-25 18:13:31
【问题描述】:

我正在使用prince 进行主成分分析:

import pandas as pd
import matplotlib.pyplot as plt

pd.set_option('display.float_format', lambda x: '{:.6f}'.format(x))
X=pd.DataFrame(data=[
[1,0,0,0,1,0,0,0,1,0,0,1,0,1,0,0,0,0],
[0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,1,0],
[0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0],
[0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1],
[0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0],
[1,0,0,0,0,0,0,1,0,1,1,0,0,1,0,0,0,0],
[0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,1,0,0],
[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0],
[0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0],
[0,0,0,0,1,1,0,0,1,0,0,1,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0],
[0,0,0,0,1,0,0,1,1,0,0,0,0,0,0,1,0,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0],
[0,0,0,1,1,0,1,1,0,0,1,1,0,0,0,1,0,0],
[0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0],
[0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0],
[0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0],
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],
[1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0],
[0,1,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0],
[0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0],
[1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,1,1],
[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0],
[1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,0],
],
columns=pd.Series(['toilNnseC','iClRnroscde','DT','sERH','ioRgL','BN','NN','OSP','hntoPy','R','FR','MSV','Uwknnon','AcTESK','ebmndgied','emlma','arsngm','fitfd']),
index=pd.Series(['Asaf_r9h1','1vam_9anAbal','t_t5ggoB1a','l9arrB1teohi_','ie_Bd15','rd1ita9B_t','5er1_aCllr','_9orsCa1n','stoC_5a1r','Chyr7a1_','C1_rah8y','a17k_lrC','ko_61oC','mCehtpspo71_ri','p8motpr_Cis1eh','ncroro1C8a_','71D_ia','9seDa1_nelin','na1Do_7','De__1aL8uVny_','rasdee8_nFn1','nla17inFn_io','e_ar9usGl1','Gdn7io_w1o','1uu9Gt_knu','a20r_tH','e5_H1','nHoog7ndre_1oo','_l1a5Iqb','a_c7J1nsok','yo8_1reL','_16Luo','1eungaM_8','et6rM_e1gz','M1zu9e_k','_se18reMnos','P1apai_9hnrt','yat_lPi5J_leeelktaa1','P1etll_o5Layd_','2silP1er_','_s6nPtaei1','7oa_1Pdas','_8o1Ry','1ne5dRoys_','ehesu_Sw1rcg7','3ir_n1eSh','Sh11_on','Tankoa7_1','_ana6aT1n','o_19T','o1aTz9_p','_217rTn0a','_1nuT6g','1Veceu_ht9ar','_aY1n9','a81aYvrdz_a','_1Yu1','ogZh1n9_']),
)
X.columns.rename('Columns', inplace=True)
X.index.rename('Rows', inplace=True)

import prince
ca = prince.CA(n_components=2,n_iter=3,copy=True,check_input=True,engine='auto',random_state=42)
ca = ca.fit(X)
ca.row_coordinates(X)
ca.column_coordinates(X)
ax = ca.plot_coordinates(X=X,ax=None,figsize=(6, 6),x_component=0,y_component=1,show_row_labels=False,show_col_labels=False)

fig = ax.get_figure()
fig.savefig('figure01.png')

结果如下:

我的问题是,将发布此图的期刊要求两个类别的点使用不同的符号(为此我使用颜色)。如何更改点的形状? (例如有十字形、菱形或正方形……)。

【问题讨论】:

  • @Mr. T:我刚做了。而这次是完整的代码……

标签: python matplotlib graphics pca


【解决方案1】:

您可以追溯更改标记(请参阅 this SO answer 并表示感谢并点赞):

...
from matplotlib.markers import MarkerStyle
from matplotlib.collections import PathCollection
...
ca = prince.CA(n_components=2,n_iter=3,copy=True,check_input=True,engine='auto',random_state=42)
ca = ca.fit(X)
ca.row_coordinates(X)
ca.column_coordinates(X)

fig, ax = plt.subplots(figsize=(6, 6))

ca.plot_coordinates(X=X,
                    ax=ax,
                    x_component=0,
                    y_component=1,
                    show_row_labels=False,
                    show_col_labels=False,
                    color="black"
                  )



markerlist = ["o", "v", "S", "H"]
for item, marker in zip(ax.collections, markerlist): 
    if type(item) == PathCollection:        
        new_marker = MarkerStyle(marker)   
        item.set_paths((new_marker.get_path(),))
        item.set_sizes([10])
    
ax.legend()

plt.show()

【讨论】:

  • 我刚刚注意到王子把标记放在了栅栏后面。如果你不喜欢它,你可以阻止它并将zorder=10 添加到ca.plot_coordinates(...) 命令。
【解决方案2】:

行列分割后可以使用“标记”:

#coordinates
x = ca.row_coordinates(X)
y = ca.column_coordinates(X)

#labels
labels = ca.explained_inertia_
x_label = 'Component 0 ('+ str(round(labels[0]*100,2))+'% inertia)'
y_label = 'Component 1 ('+ str(round(labels[1]*100,2))+'% inertia)'

#figure
plt.figure(num=None, figsize=(8, 8), dpi=100, facecolor='w', edgecolor='k')
ax = plt.scatter(x[0], x[1], marker='^', label='Row')
ax2 = plt.scatter(y[0], y[1], marker='x', label='Columns')
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.grid()
plt.legend()
plt.axhline(y=0, color='k')
plt.axvline(x=0, color='k')

我不确定这是不是最好的方法,但它确实有效

【讨论】:

  • 重点不是应该用不同的颜色,而是用不同的标记吗?
  • 事实上,没有。期刊需要不同的标记:例如“行”类的三角形和“列”类的圆形。使用 FrancescoMartino 给出的代码我有同样的问题:只有颜色区分类......
  • 对不起,我误会了。我编辑答案
  • 厚颜无耻的做法。我喜欢这样。
  • 你不应该写ax = plt.scatter(...),因为scatter()返回的是一个点的集合,而不是ax(请注意,与这里的matplotlib代码相反,许多seaborn和pandas plot命令确实返回一个ax).
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-22
  • 2017-11-14
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
相关资源
最近更新 更多