所以我有一个16个整数列表的列表。我怎么能用matplotlib制作一个图形,这样一个15x15的空间完全填满了该位置的整数标签?我问的太多了,还是有某种方法可以调整清单? 列表示例:
[[9, 6, 13, 14, 0, 8, 10, 2, 4, 7, 1, 3, 5, 12, 15, 11],
[26, 30, 27, 16, 17, 24, 20, 18, 29, 23, 25, 19, 28, 31, 22, 21],
[35, 46, 37, 41, 39, 40, 33, 36, 45, 42, 47, 43, 34, 44, 32, 38],
[58, 56, 53, 61, 54, 63, 50, 62, 49, 55, 48, 51, 57, 59, 52, 60],
[66, 76, 69, 75, 72, 71, 73, 68, 79, 67, 65, 64, 78, 74, 77, 70],
[80, 91, 83, 93, 85, 90, 92, 86, 95, 81, 89, 94, 84, 87, 88, 82],
[103, 97, 98, 104, 111, 109, 102, 108, 96, 107, 110, 106, 99, 101, 105, 100],
[115, 122, 126, 121, 127, 113, 118, 116, 125, 119, 117, 120, 112, 123, 114, 124],
[140, 130, 141, 132, 143, 131, 133, 137, 138, 134, 129, 135, 128, 139, 142, 136],
[158, 147, 156, 144, 157, 152, 150, 149, 154, 159, 148, 151, 153, 155, 146, 145],
[163, 169, 175, 166, 174, 173, 165, 160, 170, 162, 167, 171, 168, 161, 164, 172],
[187, 188, 186, 180, 182, 184, 177, 189, 185, 181, 179, 176, 183, 178, 191, 190],
[200, 203, 204, 195, 196, 205, 206, 197, 201, 193, 194, 207, 198, 199, 192, 202],
[218, 220, 212, 213, 222, 219, 223, 221, 215, 216, 209, 217, 211, 214, 210, 208],
[239, 236, 233, 230, 235, 229, 226, 238, 227, 234, 231, 225, 237, 228, 232, 224],
[254, 247, 240, 251, 241, 245, 252, 243, 255, 248, 242, 249, 250, 246, 244, 253]]
我如何绘制这样可以占用15x15的空格并标记整数? (每个点都用它在数组中表示的整数标记)
答案 0 :(得分:3)
你可以使用text来执行此操作,正如tom10所说。这是一个例子。
import matplotlib.pyplot as plt
data = [[9, 6, 13, 14, 0, 8, 10, 2, 4, 7, 1, 3, 5, 12, 15, 11],
[26, 30, 27, 16, 17, 24, 20, 18, 29, 23, 25, 19, 28, 31, 22, 21],
[35, 46, 37, 41, 39, 40, 33, 36, 45, 42, 47, 43, 34, 44, 32, 38],
[58, 56, 53, 61, 54, 63, 50, 62, 49, 55, 48, 51, 57, 59, 52, 60],
[66, 76, 69, 75, 72, 71, 73, 68, 79, 67, 65, 64, 78, 74, 77, 70],
[80, 91, 83, 93, 85, 90, 92, 86, 95, 81, 89, 94, 84, 87, 88, 82],
[103, 97, 98, 104, 111, 109, 102, 108, 96, 107, 110, 106, 99, 101, 105, 100],
[115, 122, 126, 121, 127, 113, 118, 116, 125, 119, 117, 120, 112, 123, 114, 124],
[140, 130, 141, 132, 143, 131, 133, 137, 138, 134, 129, 135, 128, 139, 142, 136],
[158, 147, 156, 144, 157, 152, 150, 149, 154, 159, 148, 151, 153, 155, 146, 145],
[163, 169, 175, 166, 174, 173, 165, 160, 170, 162, 167, 171, 168, 161, 164, 172],
[187, 188, 186, 180, 182, 184, 177, 189, 185, 181, 179, 176, 183, 178, 191, 190],
[200, 203, 204, 195, 196, 205, 206, 197, 201, 193, 194, 207, 198, 199, 192, 202],
[218, 220, 212, 213, 222, 219, 223, 221, 215, 216, 209, 217, 211, 214, 210, 208],
[239, 236, 233, 230, 235, 229, 226, 238, 227, 234, 231, 225, 237, 228, 232, 224],
[254, 247, 240, 251, 241, 245, 252, 243, 255, 248, 242, 249, 250, 246, 244, 253]]
# Create a figure and axis
fig = plt.figure()
ax = plt.subplot(111)
# Plot each number based on its position
y = 1
for row in data:
x = 1
for n in row:
plt.text(x,y,str(n))
x +=1
y += 1
# Set the x and y axes limits
xMax = len(data)
yMax = len(data[0])
ax.set_xlim(0, xMax + 1)
ax.set_ylim(0, yMax + 1)
plt.show()