Plot string values in matplotlib

后端 未结 5 2102
南旧
南旧 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 03:13

    From matplotlib 2.1 on you can use strings in plotting functions.

    import matplotlib.pyplot as plt
    x = ["Apple", "Banana", "Cherry"]
    y = [5,2,3]
    
    plt.plot(x, y)
    
    plt.show()
    

    Note that in order to preserve the order of the input strings on the plot you need to use matplotlib >=2.2.

提交回复
热议问题