【问题标题】:matlplotlib scatterplot with numbers as symbols + legendmatplotlib 散点图,以数字为符号 + 图例
【发布时间】:2021-11-13 23:19:49
【问题描述】:

我想绘制一个散点图,其中包含彩色数字而不是点作为符号。 我做了如下

n=np.arange(1,14,1)

fig, axs = plt.subplots(1, 2)

axs[0].scatter(x, y, linestyle='None', color="white")

for i, txt in enumerate(n):
     axs[0].annotate(txt, (x[i], y[i]), color=x_y_colours[i], ha="center", va="center")
       

它成功了,但现在我不知道如何创建图例!我想用彩色数字作为符号,然后是标签。

谢谢大家!

【问题讨论】:

    标签: matplotlib legend scatter-plot


    【解决方案1】:

    您可以使用markers in latex form,使用文本或数字作为标记。为此,您可以编写 marker='$...$',类似于 matplotlib 标签中使用乳胶的方式。请注意,这些标记会自动居中。

    import matplotlib.pyplot as plt
    import numpy as np
    
    n = np.arange(1, 14)
    theta = np.pi * n * (3 - np.sqrt(5))
    r = np.sqrt(n)
    x = r * np.cos(theta)
    y = r * np.sin(theta)
    x_y_colours = plt.get_cmap('hsv')(n / n.max())
    x_y_labels = [*'abcdefghijklm']
    
    fig, ax = plt.subplots()
    for xi, yi, color_i, label_i, txt in zip(x, y, x_y_colours, x_y_labels, n):
        ax.scatter(xi, yi, marker=f'${txt}$', s=200, color=color_i, label=label_i)
    ax.legend(markerscale=0.5, bbox_to_anchor=[1.01, 1.01], loc='upper left')
    plt.tight_layout()
    plt.show()
    

    【讨论】:

    • 非常感谢!效果很好!!
    猜你喜欢
    • 2013-06-29
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2020-07-22
    相关资源
    最近更新 更多