Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I render the dots completely while keeping all the grid lines?

I’m trying to reproduce the figure from page 405 of "Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein. Introduction to Algorithms, 3rd Edition. The MIT Press, 2009"

enter image description here

Here is the code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

plt.plot(0,6,'o',c='k')
plt.plot(1,0,'o',c='k')
plt.plot((0,1),(6,0))
plt.xlim([0, 9])
plt.ylim([0, 7])
plt.grid()

which renders the dots incompletely

enter image description here

In contrast, the following code renders the dots completely though, the grid lines at x=1, 3, disappears

enter image description here

How do I render the dots completely while keeping all the grid lines?

>Solution :

Using MultipleLocator. Also, it would be better to use the object-oriented interface (fig, ax = plt.subplots(), ax.plot()), instead of the pyplot interface (plt.plot()).

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator

fig, ax = plt.subplots()
ax.plot(0,6,'o',c='k')
ax.plot(1,0,'o',c='k')
ax.plot((0,1),(6,0))
ax.set_xlim([-1, 9])
ax.set_ylim([-1, 7])
ax.yaxis.set_major_locator(MultipleLocator(1))
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.grid()

Output:

enter image description here

The entire figure:

fig, ax = plt.subplots()
x = [0, 1, 6, 8, 7, 5, 2]
y = [6, 0, 1, 2, 5, 4, 3]
ax.fill(x, y, fill=False)
ax.scatter(x, y, c='k')
ax.set_xlim([-1, 9])
ax.set_ylim([-1, 7])
ax.yaxis.set_major_locator(MultipleLocator(1))
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.grid()
ax.set_axisbelow(True)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading