Plot with non-numerical data on x axis (for ex., dates)

前端 未结 2 727
梦毁少年i
梦毁少年i 2020-12-16 01:01

I\'d like to plot numerical data against non numerical data, say something like this:

import matplotlib.pyplot as pl
x=[\'a\',\'b\',\'c\',\'d\']
y=[1,2,3,4]
         


        
2条回答
  •  醉梦人生
    2020-12-16 01:43

    import matplotlib.pyplot as plt
    x = ['a','b','c','d']
    y = [1,2,3,4]
    plt.plot(y)
    plt.xticks(range(len(x)), x)
    plt.show()
    

    enter image description here

    On a side note, dates are numerical in this sense (i.e. they have an inherent order and spacing).

    Matplotlib handles plotting temporal data quite well and very differently than the above example. There's an example in the matplotlib examples section, but it focuses on showing off several different things. In general, you just use either plot_date or just plot the data and call ax.xaxis_date() (or yaxis_date) to tell matplotlib to use the various date locators and tickers.

提交回复
热议问题