【问题标题】:How can I set color and marker in one parameter for scatter function in pythonpython - 如何在一个参数中为python中的散点函数设置颜色和标记
【发布时间】:2018-10-30 07:23:50
【问题描述】:

我尝试根据标签对一系列数据使用不同的颜色和标记:

我写道:

   for x,l in zip(X,labels):
        ax.scatter(x[0],x[1], 'xb' if l == -1 else 'or')

“xb”是指标记的“x”和颜色的“b”,但它不起作用。

所以我写成:

for x,l in zip(X,labels):
    ax.scatter(x[0],x[1], marker='x' if l == -1 else 'o', c='r' if l == -1 else 'b')

不使用两个单独的命名参数的正确参数是什么?或任何其他缩短语句的技巧。

【问题讨论】:

    标签: python scatter-plot


    【解决方案1】:

    到目前为止,我还没有在绘图函数中看到 if-else 构造。我会更改标签列表:

    X = [(1, 4), (2, 6), (3, 2), (4, 8), (5, 3), (6,1)]
    labels = [-1, 0, 0, -1, 0, -1]
    
    fig, ax = plt.subplots()
    labels = ["xb" if item == -1 else "or" for item in labels]
    
    for x,l in zip(X, labels):
        ax.plot(x[0],x[1], l)
    
    plt.show()
    

    还值得注意的是,scatter 和 plot 对标记定义使用不同的约定。对于 scatter,plot 的快捷方式不起作用,其中 size s、color c 和 markerstyle m 必须单独提供。

    【讨论】:

    • 你认为 'xb' 有效吗?这实际上是我对marker='x' and c='b' 的替代品的问题
    • 正如我所说,恕我直言,您不能将标记格式字符串“xb”等用于ax.scatter,只能用于ax.plot。如果要使用分散,则必须拆分标签,例如marker = l[0], c = l[1]
    • 谢谢,我没有注意到你使用了plot,在这种情况下它就像scatter
    • 这种方法的问题在于you have to create a custom legend,因为现在每个数据点都被解释为一个独立的序列。
    猜你喜欢
    • 2020-10-28
    • 2021-03-11
    • 2020-12-20
    • 2021-04-20
    • 2013-07-14
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    相关资源
    最近更新 更多