I am trying to plot counts in gridded plots, but I am not being able to figure out how I go about it. I want to:
Have dotted grids at an interval of 5
Have major tick labels only every 20
I want the ticks to be outside the plot.
Have "counts" inside those grids
I have checked for potential duplicates such as here and here, but I have not been able to figure it out.
This is my code.
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
for key, value in sorted(data.items()):
x = value[0][2]
y = value[0][3]
count = value[0][4]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.annotate(count, xy = (x, y), size = 5)
# Overwrites and I only get the last data point
plt.close()
# Without this, I get "fail to allocate bitmap" error
plt.suptitle('Number of counts', fontsize = 12)
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.axes().set_aspect('equal')
plt.axis([0, 1000, 0, 1000])
# This gives an interval of 200
majorLocator = MultipleLocator(20)
majorFormatter = FormatStrFormatter('%d')
minorLocator = MultipleLocator(5)
# I want minor grid to be 5 and major grid to be 20
plt.grid()
filename = 'C:UsersOwlDesktopPlot.png'
plt.savefig(filename, dpi = 150)
plt.close()
This is what I get.
I also have a problem of overwriting the data points. Could anybody PLEASE help me with this problem?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…