Search code examples
pythonmatplotlibgridlines

Setting the distance between dots on grid lines


I am plotting a matplotlib graph with grid along both major and minor axis. Also I have set the minor axis line style to ":", so that grid lines are dots instead of solid line. But the dots are very closely spaced, that it is hard to differentiate. Is there any way to control the spacing between these dots? Following is my code:

import matplotlib.pyplot as plt

plt.grid(which='major', linewidth='0.7')
plt.grid(which='minor', linewidth='0.7', ls=":")
plt.semilogy(np.linspace(-4, 20, 25), some_data, 'C1', marker='o', markersize=20)

I get the following graph graph

Though the minor axis is dotted it looks almost like the major axis, which is solid line. I want to know if it is possible to specify this distance between the dots so that it would look a little less dense. Thanks.


Solution

  • The dashes parameter/argument lets you specify that.

    plt.grid(which='minor', linewidth='0.7', ls=":", dashes=(1,10,1,10))
    

    dashes is a Line2D property so you should be able to set the spacing for any Line2D artist.