【发布时间】:2014-07-24 21:07:48
【问题描述】:
我正在尝试在网格图中绘制计数,但我无法弄清楚如何去做。
我想要:
-
以 5 为间隔的点状网格;
-
每 20 个主要刻度标签;
-
刻度线位于绘图之外;和
-
在这些网格中进行“计数”。
我已经检查了潜在的重复项,例如 here 和 here,但无法弄清楚。
这是我的代码:
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 a "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 the minor grid to be 5 and the major grid to be 20.
plt.grid()
filename = 'C:\Users\Owl\Desktop\Plot.png'
plt.savefig(filename, dpi = 150)
plt.close()
这就是我得到的。
我也遇到了数据点被覆盖的问题。
有人可以帮我解决这个问题吗?
【问题讨论】:
标签: python matplotlib plot grid label