matplotlib 如何使用Python绘制散点图?

klsxnrf1  于 2023-05-07  发布在  Python
关注(0)|答案(1)|浏览(194)

我已经使用此代码创建集群,我想绘制集群的散点图。vectorAssembles_01生成具有ID和特征的数据。两者都应该用来绘制散点图。当我在google Collab中运行代码时,我收到一条错误消息,说明RecursionError:比较中超出了最大递归深度。如果我说错了请纠正。

from pyspark.ml.clustering import KMeans
from pyspark.ml.feature import VectorAssembler
import numpy as np
import matplotlib.pyplot as plt

FEATURES_COL = ['Height(CM)', 'Weight(KG)', 
                      'Crossing', 'Finishing', 'HeadingAccuracy', 
                      'ShortPassing', 'Volleys', 'Dribbling', 'Curve',
                      'FKAccuracy', 'LongPassing', 'BallControl', 
                      'Acceleration', 'SprintSpeed', 'Agility', 
                      'Reactions', 'Balance', 'ShotPower', 'Jumping', 
                      'Stamina', 'Strength', 'LongShots', 'Aggression', 
                      'Interceptions', 'Positioning', 'Vision', 'Penalties', 
                      'Composure', 'Marking', 'StandingTackle', 'SlidingTackle']

vecAssembler_01 = VectorAssembler(inputCols=FEATURES_COL, outputCol="features")
df_kmeansn = vecAssembler_01.transform(df).select('ID','features')
df_kmeansn.show()
#df_kmeansn.plot("ID","fearures",kind="Scatter")
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
x = df_kmeansn.ID
y = df_kmeansn.features
ax.scatter(x, y, alpha=0.8, edgecolors='none')

df_kmeansn的输出如下所示。

b5lpy0ml

b5lpy0ml1#

我不确定你是否可以直接绘制Spark Dataframe,也许你应该先调用to_pandas

# ...

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

df_pandas = df_kmeansn.to_pandas()

x = df_pandas.ID
y = df_pandas.features
ax.scatter(x, y, alpha=0.8, edgecolors='none')

相关问题