【问题标题】:How to plot top %1 in different color in a scatter plotting?如何在散点图中以不同颜色绘制顶部 %1?
【发布时间】:2021-12-13 13:49:57
【问题描述】:

我有两列由 .csv 文件中的数字组成。 让我们想象一下

column1 = [1, 2, 3, 7, 9, 3, 14, 7, 9]

column2 = [2, 5, 2, 67, 8, 3, 6, 5, 2]

column1 和 column2 基本上是时间序列,但 csv 文件中没有索引号。 我想以不同颜色绘制位于顶部 %1 的值。

所以,我的代码是

features = ['column1' , 'column2']
df = pd.read_csv('XX.csv', usecols=features, sep=';', encoding='ISO-8859-1')
for features in df:
    new_df = df[[features]].quantile(q=.99, axis=0, numeric_only=True).iloc[0]

此代码为代表每列顶部 %1 的每列生成两个阈值数字。下一步是为 a 和 b 列绘制散点图,如果每列的数字高于这两个阈值,则以不同的颜色显示。 我在这里搞砸了。

【问题讨论】:

    标签: python plot conditional-statements scatter


    【解决方案1】:

    在您的数据框中添加一列以指定特定条件下的颜色。在这里,我将您的要求解释为如果 column1column2 超过其各自的阈值,则它会获得不同的颜色。我对阈值进行了硬编码,以使代码更易于阅读;您可能会在条件中使用new_df["column1"]new_df["column2"]

    dftest = pd.DataFrame({
        "column1": [1, 2, 3, 7, 9, 3, 14, 7, 9],
        "column2": [2, 5, 2, 67, 8, 3, 6, 5, 2]
    }) # set up test df
    dftest["colour"] = "green" # put green everywhere in the new colour column
    dftest.loc[(dftest["column1"] > 13.6) | (dftest["column2"] > 62),"colour"] = "red" # overwrite with red where you need to.
    dftest.plot.scatter(x="column1", y="column2", c=dftest["colour"])
    

    输出:

    【讨论】:

    • 这非常有效。但是,如果我们有两个以上的列怎么办。想象一下有 15 列这样的列,我们应该在混淆矩阵上显示它吗?
    • 我想我做到了。非常感谢:)
    • 很高兴您解决了问题。由于您是新用户,我希望您不介意我指出本网站上的良好礼仪:如果我按要求回答了您的问题(并且只提到了 2 列,而不是 15 列),那么请考虑给出一个投票和/或接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 2021-01-08
    • 2015-03-29
    相关资源
    最近更新 更多