This question is related to the earlier question I asked "matplotlib: Change grid interval and specify tick labels" but now I want to change the scale for x and y axes. When I set the range for x and y axes and then specify the intervals for major and minor ticks, it forces the x and y axes to be the same.
This is my code.
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
for key, value in sorted(data.items()):
x = value[0][2]
y = value[0][3]
count = value[0][4]
ax.annotate(count, xy = (x, y), size = 3)
plt.suptitle('Number of counts', fontsize = 12)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_aspect('equal')
# I want max x axis to be 500
ax.set_xlim(0, 501)
# I want max y axis to be 300
ax.set_ylim(0, 301)
# I want major ticks to be every 20
major_ticks = np.arange(0, 501, 20)
# I want minor ticks to be every 5
minor_ticks = np.arange(0, 501, 5)
# If I do minor_ticks = np.arange(0, 301, 5), I will not get minor ticks for the entire plot
# Specify tick label size
ax.tick_params(axis = 'both', which = 'major', labelsize = 4)
ax.tick_params(axis = 'both', which = 'minor', labelsize = 0)
# Suppress minor tick labels
ax.set_xticks(major_ticks)
ax.set_xticks(minor_ticks, minor = True)
ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks, minor = True)
# Set both ticks to be outside
ax.tick_params(which = 'both', direction = 'out')
# Specify different settings for major and minor grids
ax.grid(which = 'minor', alpha = 0.3)
ax.grid(which = 'major', alpha = 0.7)
filename = 'C:\Users\Owl\Desktop\Plot.png'
plt.savefig(filename, dpi = 150)
plt.close()
and this is what I get.
How could I get different ranges for x and y axes and still have major and minor ticks like this? I might be missing something very simple but if somebody could point it out, I am very thankful for that!!