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?
Add a comment
|
3 Answers
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()
1 Comment
ashim
I don't have minor ticks, how can I create them?
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
Community
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.