Matplotlib: automatically modify axis labels

后端 未结 1 1630
无人及你
无人及你 2021-01-20 04:54

I\'m aware that it\'s possible to change axis labels by setting them manually. (E.g.: Modify tick label text)

However, this obviously only works if you know what lab

相关标签:
1条回答
  • 2021-01-20 05:27

    If I truly understand your question you are looking for function formatter from matplotlib.ticker:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib import ticker
    
    # I added 'y' to fit second argument (position) of FuncFormatter
    tick_to_date = lambda x,y: ("day " + str(x))
    
    x = np.array([27, 38, 100, 300])
    y = np.array([0.5, 2.5, 1.0, 0.8])
    plt.scatter(x, y)
    
    ax = plt.gca()
    # tick_to_date will affect all tick labels through MyFormatter
    myFormatter = ticker.FuncFormatter(tick_to_date)
    # apply formatter for selected axis
    ax.xaxis.set_major_formatter(myFormatter)
    plt.show()
    
    0 讨论(0)
提交回复
热议问题