所以我计算了一些现在应该可视化的数据。对于每个数据元素,我想放置一个单独的子图,以便整个图尽可能紧凑。以下是五个元素的示例:
这是我想出的任意元素计数原型:
import matplotlib.pyplot as plt
import numpy as np
import math
data = ... # some list of pairs of numpy arrays, for x and y axes
size = len(data)
cols = math.floor(math.sqrt(size))
rows = math.ceil(size / cols)
f, diags = plt.subplots(rows, cols)
for (row, col), diag in np.ndenumerate(diags):
dataIdx = row * cols + col
if dataIdx < size:
x = data[dataIdx][0]
y = data[dataIdx][1]
diag.scatter(x, y)
diag.set_title('Regressor {}'.format(dataIdx + 1))
else: # discard empty subplots
f.delaxes(diag)
f.show()
一个简短的解释:对于紧凑性,如果可能的话,我试图以方阵的形式调整图。如果没有,我为剩余的图添加另一行。然后我迭代图表,计算数据元素的相应位置并绘制其值。如果没有找到图表的数据元素,则表示该图表是最后一行的余数,可以丢弃。
然而,这是我可能用C ++或Java编写的代码,问题是,pythonic方式会是什么?
此外,在迭代数据而不是图表时,最佳解决方案是什么?我当然可以从元素索引中计算图表的行/列,就像我在初始行/列计算中一样,但也许有更好的方法来做到这一点......
提前致谢!
答案 0 :(得分:1)
我可能会创建这样的情节:
size = len(data)
cols = round(math.sqrt(size))
rows = cols
while rows * cols < size:
rows += 1
f, ax_arr = plt.subplots(rows, cols)
ax_arr = ax_arr.reshape(-1)
for i in range(len(ax_arr)):
if i >= size:
ax_arr[i].axis('off')
x = data[i][0]
y = data[i][1]
ax_arr[i].scatter(x,y)