setting a legend matching the colours in pyplot.scatter

后端 未结 3 1585
迷失自我
迷失自我 2021-01-07 07:03

Suppose my data is organized in the following way:

x_values = [6.2, 3.6, 7.3, 3.2, 2.7]
y_values = [1.5, 3.2, 5.4, 3.1, 2.8]
colours = [1, 1, 0, 1, -1]
label         


        
3条回答
  •  耶瑟儿~
    2021-01-07 07:23

    Just a remark, not exactly answering the question:

    If use "seaborn" it would be EXACTLY ONE LINE:

    import seaborn as sns 
    x_values = [6.2, 3.6, 7.3, 3.2, 2.7]
    y_values = [1.5, 3.2, 5.4, 3.1, 2.8]
    #colors = [1, 1, 0, 1, -1]
    labels = ["a", "a", "b", "a", "c"]
    ax = sns.scatterplot(x=x_values, y=y_values, hue=labels)
    

    PS

    But the question is about matplotlib, so. We have answers above, also one might look at: https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/scatter_with_legend.html Subsection: "Automated legend creation".

    However I feel not easy to modify those examples to what you need.

提交回复
热议问题