代码之家  ›  专栏  ›  技术社区  ›  Md. Rezwanul Haque

如何在峰值上绘制具有指数值的时间序列?[副本]

  •  0
  • Md. Rezwanul Haque  · 技术社区  · 7 年前

    我试图做一个散点图,并用列表中不同的数字注释数据点。 例如,我想 y x 并用相应的数字注释 n .

    y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
    z = [0.15, 0.3, 0.45, 0.6, 0.75]
    n = [58, 651, 393, 203, 123]
    ax = fig.add_subplot(111)
    ax1.scatter(z, y, fmt='o')
    

    有什么想法吗?

    0 回复  |  直到 6 年前
        1
  •  385
  •   Jason Aller    8 年前

    我不知道有什么绘图方法可以使用数组或列表,但是你可以使用 annotate() 在中迭代值时 n .

    y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
    z = [0.15, 0.3, 0.45, 0.6, 0.75]
    n = [58, 651, 393, 203, 123]
    
    fig, ax = plt.subplots()
    ax.scatter(z, y)
    
    for i, txt in enumerate(n):
        ax.annotate(txt, (z[i], y[i]))
    

    有很多格式化选项 注释() ,请参见 matplotlib website:

    enter image description here

        2
  •  29
  •   Augustin    8 年前

    在版本早于matplotlib 2.0的版本中, ax.scatter 不需要打印没有标记的文本。在2.0版中,您需要 最大散射 为文本设置适当的范围和标记。

    y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
    z = [0.15, 0.3, 0.45, 0.6, 0.75]
    n = [58, 651, 393, 203, 123]
    
    fig, ax = plt.subplots()
    
    for i, txt in enumerate(n):
        ax.annotate(txt, (z[i], y[i]))
    

    在这里面 link 你可以在3d中找到一个例子。

        3
  •  14
  •   Heather Claxton    7 年前

    如果有人试图将上述解决方案应用于.scatter()而不是.subblot(),

    我试着运行以下代码

    y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
    z = [0.15, 0.3, 0.45, 0.6, 0.75]
    n = [58, 651, 393, 203, 123]
    
    fig, ax = plt.scatter(z, y)
    
    for i, txt in enumerate(n):
        ax.annotate(txt, (z[i], y[i]))
    

    但遇到错误,指出“无法解压缩不可iterable PathCollection对象”,错误特别指向代码行fig,ax=plt.scatter(z,y)

    我最终用下面的代码解决了这个错误

    plt.scatter(z, y)
    
    for i, txt in enumerate(n):
        plt.annotate(txt, (z[i], y[i]))
    

    我没想到.scatter()和.subblot()之间会有区别 我早该知道的。

        4
  •  6
  •   irudyak    7 年前

    你也可以使用 pyplot.text (见 here ).

    def plot_embeddings(M_reduced, word2Ind, words):
    """ Plot in a scatterplot the embeddings of the words specified in the list "words".
        Include a label next to each point.
    """
    for word in words:
        x, y = M_reduced[word2Ind[word]]
        plt.scatter(x, y, marker='x', color='red')
        plt.text(x+.03, y+.03, word, fontsize=9)
    plt.show()
    
    M_reduced_plot_test = np.array([[1, 1], [-1, -1], [1, -1], [-1, 1], [0, 0]])
    word2Ind_plot_test = {'test1': 0, 'test2': 1, 'test3': 2, 'test4': 3, 'test5': 4}
    words = ['test1', 'test2', 'test3', 'test4', 'test5']
    plot_embeddings(M_reduced_plot_test, word2Ind_plot_test, words)
    

    enter image description here

        5
  •  0
  •   andor kesselman    6 年前

    作为一行使用列表理解和numpy:

    [ax.annotate(x[0], (x[1], x[2])) for x in np.array([n,z,y]).T]

    设置和罗格的答案一样。

    推荐文章