25

I am plotting using Matplotlib in Python. I want create plot with grid, and here is an example from a plotting tutorial. In my plot range if the y axis is from 0 to 14 and if I use pylab.grid(True) then it makes a grid with the size of square of two, but I want the size to be 1. How can I force it?

3 Answers 3

26

Try using ax.grid(True, which='both') to position your grid lines on both major and minor ticks, as suggested here.

EDIT: Or just set your ticks manually, like this:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3,14],'ro-')

# set your ticks manually
ax.xaxis.set_ticks([1.,2.,3.,10.])
ax.grid(True)

plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

I don't have minor ticks, how can I create them?
8

If you want to follow along with the example you cited:

>>> import numpy
>>> import pylab
>>> t = numpy.arange(0.0, 1.0+0.1, 0.01)
>>> s = numpy.cos(2*2*numpy.pi*t)
>>> pylab.plot(t,s)
>>> pylab.grid(True)
>>> pylab.xticks([i/10.0 for i in range(0,12)])
>>> pylab.show()

Comments

0
import numpy as np
import matplotlib.pyplot as plt

# Define the crossword grid size
grid_size = 15  # Adjust as needed

# Initialize an empty grid with spaces
grid = np.full((grid_size, grid_size), ' ', dtype=str)

# Place words in the grid (simplified, approximate positioning)
words_across = {
    (0, 0): "ALBERTEINSTEIN",
    (2, 0): "BARACKOBAMA",
    (4, 0): "FREDERICKBANTING",
    (6, 0): "MURRAYGELLMANN",
    (8, 0): "TONIMORRISON",
    (10, 0): "JOHNNASH",
}

words_down = {
    (0, 2): "ERNESTLAWRENCE",
    (0, 4): "AZIZSANCAR",
    (0, 6): "JOHNSTEINBECK",
    (0, 8): "MILTONFRIEDMAN",
    (0, 10): "SELMAWAKSMAN",
    (0, 12): "LECHWALESA",
    (0, 14): "LOUISEGLUCK",
}

# Fill the grid with words
for (row, col), word in words_across.items():
    for i, letter in enumerate(word):
        grid[row, col + i] = letter

for (row, col), word in words_down.items():
    for i, letter in enumerate(word):
        grid[row + i, col] = letter

# Plot the crossword puzzle
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_xticks(np.arange(grid_size + 1) - 0.5, minor=True)
ax.set_yticks(np.arange(grid_size + 1) - 0.5, minor=True)
ax.grid(which="minor", color="black", linestyle='-', linewidth=2)
ax.tick_params(which="both", bottom=False, left=False, labelbottom=False, labelleft=False)

# Add letters to the grid
for i in range(grid_size):
    for j in range(grid_size):
        if grid[i, j] != ' ':
            ax.text(j, i, grid[i, j], ha='center', va='center', fontsize=12, fontweight='bold')

plt.show()

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.