How to set axis ticks in multiples of pi (Python) (matplotlib)

前端 未结 6 1758
长发绾君心
长发绾君心 2020-12-08 04:37

I\'d like to make a plot in Python and have x range display ticks in multiples of pi.

Is there a good way to do this, not manually?

I\'m thinking of using

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 05:23

    I created a PyPi Package that creates formatter and locator instances like Scott Centoni's answer.

    """Show a simple example of using MultiplePi."""
    
    import matplotlib.pyplot as plt
    import numpy as np
    
    from matplot_fmt_pi import MultiplePi
    
    fig = plt.figure(figsize=(4*np.pi, 2.4))
    axes = fig.add_subplot(111)
    x = np.linspace(-2*np.pi, 2*np.pi, 512)
    axes.plot(x, np.sin(x))
    
    axes.grid(True)
    axes.axhline(0, color='black', lw=2)
    axes.axvline(0, color='black', lw=2)
    axes.set_title("MultiplePi formatting")
    
    pi_manager = MultiplePi(2)
    axes.xaxis.set_major_locator(pi_manager.locator())
    axes.xaxis.set_major_formatter(pi_manager.formatter())
    
    plt.tight_layout()
    plt.savefig("./pi_graph.png", dpi=120)
    

提交回复
热议问题