Plot string values in matplotlib

后端 未结 5 2104
南旧
南旧 2020-12-06 02:17

I am using matplotlib for a graphing application. I am trying to create a graph which has strings as the X values. However, the using plot function expects a nu

5条回答
  •  无人及你
    2020-12-06 02:58

    Here's one way which i know works, though i would think creating custom symbols is a more natural way accomplish this.

    from matplotlib import pyplot as PLT
    
    # make up some data for this example
    t = range(8)
    s = 7 * t + 5
    # make up some data labels which we want to appear in place of the symbols
    x = 8 * "dp".split()
    y = map(str, range(8))
    data_labels = [ i+j for i, j in zip(x, y)]
    fig = PLT.figure()
    ax1 = fig.add_subplot(111)
    ax1.plot(t, s, "o", mfc="#FFFFFF")     # set the symbol color so they are invisible
    for a, b, c in zip(t, s, data_labels) :
        ax1.text(a, b, c, color="green")
    
    PLT.show()
    

    So this puts "dp1", "dp2",... in place of each of the original data symbols--in essence creating custom "text symbols" though again i have to believe there's a more direct way to do this in matplotlib (without using Artists).

提交回复
热议问题