How to create major and minor gridlines with different linestyles in Python

前端 未结 2 1752
忘掉有多难
忘掉有多难 2020-12-02 07:28

I am currently using matplotlib.pyplot to create graphs and would like to have the major gridlines solid and black and the minor ones either greyed or dashed.

2条回答
  •  粉色の甜心
    2020-12-02 07:54

    A simple DIY way would be to make the grid yourself:

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    ax.plot([1,2,3], [2,3,4], 'ro')
    
    for xmaj in ax.xaxis.get_majorticklocs():
      ax.axvline(x=xmaj, ls='-')
    for xmin in ax.xaxis.get_minorticklocs():
      ax.axvline(x=xmin, ls='--')
    
    for ymaj in ax.yaxis.get_majorticklocs():
      ax.axhline(y=ymaj, ls='-')
    for ymin in ax.yaxis.get_minorticklocs():
      ax.axhline(y=ymin, ls='--')
    plt.show()
    

提交回复
热议问题