【问题标题】:Change color of scatter point w.r.t a feature改变散点的颜色 w.r.t 一个特征
【发布时间】:2016-08-05 00:36:35
【问题描述】:

我有一个熊猫数据框,它有 3 个特征(列),即 X、Y 和 Z。 X 和 Y 是包含数据的列表,而 Z 包含字符串值列表(颜色 - 红色和蓝色)。我想要一个散点图,x 轴作为特征 X,y 轴作为特征 Y,散点根据 Z 中的值着色。

例如。如果某行 Z 中的值为红色,则将点着色为红色,如果为蓝色,则将其着色为蓝色。

data_frame['X'] = pandas.Series(X)
data_frame['Y'] = pandas.Series(Y)
data_frame['Z'] = pandas.Series(Z)

data_frame.plot(kind='scatter', x='X', y='Y')
pl.show()

情节运行良好,但我不知道如何实现颜色部分。有任何想法吗??

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    您可以尝试使用 seaborn:

    import seaborn as sns
    sns.lmplot("X", "Y", data= data_frame, hue='Z', fit_reg=False)
    

    【讨论】:

    • 它确实工作正常,但你能告诉我如何使用 matplotlib 或 pandas 吗?我想使用尽可能少的模块。
    【解决方案2】:
    import numpy as np
    import matplotlib.pyplot as plt
    N = 50
    x = np.random.rand(N)      # pandas series works too
    y = np.random.rand(N)
    colors = np.random.rand(N)
    circle = 100* np.ones(50)
    plt.scatter(x, y, c=colors, s=circle, alpha=0.6)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 2022-11-11
      • 1970-01-01
      • 2016-01-17
      • 2019-01-03
      • 1970-01-01
      • 2023-01-13
      相关资源
      最近更新 更多