Plot x-axis with string array as in the same order in original array and not sort it alphabetically in matplotlib

前端 未结 2 1856
孤街浪徒
孤街浪徒 2021-01-14 02:02

Either Numpy or Matplotlib is changing the order of my np.array and it\'s conflicting with my plot. It\'s causing the months to be out of order while the corresponding data

2条回答
  •  时光取名叫无心
    2021-01-14 02:33

    Since month is a string array, plt.plot() command is sorting it alphabetically. So, we have to use the xticks and then plot it like below to get the strings in the same order as it were in the original array month.

    In [16]: f = np.array([53, 56, 63, 72, 79, 86, 89, 88, 83, 74, 65, 56])
        ...: month = np.array(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])
        ...: plt.xticks(range(len(f)), month)
        ...: plt.plot(f)
    

    Plot:

    Note: For more customized plots refer: pylab date demo

提交回复
热议问题