【问题标题】:How to efficiently scatter plot a numpy 2d array如何有效地散点图一个 numpy 二维数组
【发布时间】:2020-03-27 17:15:07
【问题描述】:

我有一个 numpy,每行包含 x、y 对,我想在不使用 for 循环的情况下显示散点图,因此我使用 pandas 的以下方法:

def visualize_k_means_output(self, centroids):
    fig, ax = plt.subplots()
    self.visualize_box_relative_sizes()
    frame = pd.DataFrame(centroids, columns=['X', 'Y'])
    ax.scatter(frame['X'], frame['Y'], marker='*', s=200, c='black')

问题是如何在不使用for循环的情况下将第一项提取为x,将第二项提取为y:

ax.scatter(x=[item[0] for item in centroids], y=[item[1] for item in centroids], ...)

【问题讨论】:

    标签: python-3.x pandas numpy matplotlib


    【解决方案1】:

    如果我理解正确,你想对你的 numpy 数组进行切片:

    x = centroids[:, 0]
    y = centroids[:, 1]
    

    【讨论】:

      猜你喜欢
      • 2015-07-13
      • 2021-06-24
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 2017-01-21
      相关资源
      最近更新 更多