使用网格评估数据

Use grid to evaluate data

假设我有这样的列表

x = [1,2,3,4,5]
y = [1,4,9,16,25]

我想绘制这些列表,但在某种程度上,例如我会将绘制区域分成 4 个同样大的正方形。

我想要这样的东西:

plt.plot(x,y)
plt.grid(True but into 2x2 squares)
plt.show()

另外,我想成为网格,不仅在情节上看起来不错,而且有实际用途。

例如,我希望能够检查我感兴趣的任何给定方格中有多少点。例如,如果我想检查有多少点在左下角的方块(假设我们有 2x2 的方块)我想要一个清晰的结果。

此外,如果可能的话,我希望能够更改正方形的 size/amount。也许我想要 2x2 的正方形,但也许我想要它更精确并且想要 10x10 的正方形。

最后,如果我决定将绘图区域划分为 10x10 的正方形,这样我就可以访问有关每个正方形的信息,相同的正方形也将在视觉上显示剧情.

提前致谢!

我编写了以下函数,它允许您在图表上方动态创建您喜欢的大小(和颜色)的网格。
我给你输入函数plot_grid()你要绘制的数据xy,网格的大小(size_grid_xsize_grid_y) 和可选的颜色 (color_) 和透明度 (alpha_)。

函数以及绘图 returns 矩阵 map_points(使用 np.histogram2d)包含网格每个单元格中的点数。

代码如下:

import matplotlib.pyplot as plt
import numpy as np

def plot_grid(lx, ly, legend_labels=[], size_grid_x=2, size_grid_y=2, alpha_=0.9, color_='red'):

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    min_x, max_x = np.min(np.min(lx)), np.max(np.max(lx))
    min_y, max_y = np.min(np.min(ly)), np.max(np.max(ly)) 

    ticks_x = np.arange(min_x, max_x, (max_x-min_x)/size_grid_x)
    ticks_y = np.arange(min_y, max_y, (max_y-min_y)/size_grid_y)

    ax.set_xticks(ticks_x)
    ax.set_yticks(ticks_y)
    ax.set_xlim([min_x,max_x])
    ax.set_ylim([min_y, max_y])

    ax.grid(which='major', alpha=alpha_, color=color_)
    ax.grid(True)

    if not isinstance(lx[0], list):
        lx, ly = [lx], [ly]

    map_points_tot = np.zeros((size_grid_x,size_grid_y))
    for x,y in zip(lx, ly):
        ax.scatter(x,y)
        map_points, _, _ = np.histogram2d(x, y, bins=[size_grid_x, size_grid_y])
        map_points_tot += map_points

    if legend_labels:    
        plt.legend(legend_labels,loc='lower right')
    plt.show()
    plt.close()

    return map_points_tot

使用示例

给定以下数据:

x = [1,2,3,4,5,9,8,7]
y = [1,4,9,16,25,7,4,18]

可以通过将网格尺寸作为参数传递给 plot_grid() 函数来动态绘制网格。例如,如果我想创建一个 2x2 网格:

map_points = plot_grid(x, y, size_grid_x=2, size_grid_y=2)

# Map points in every cell:
array([[3., 1.],
       [2., 2.]])

注意: 如何解释 map_points 中的结果?
在这个例子中 map_points 必须解释如下:图表的 lower left 单元格中将有 3 个点,upper left 单元格中有 1 个点,lower right 单元格中有 2 个点,而 lower right 单元格中有 2 个点upper right 单元格。

或者如果我想创建一个 4x4 网格:

map_points = plot_grid(x, y, size_grid_x=4, size_grid_y=4)

# Map points in every cell:
array([[2., 0., 0., 0.],
       [0., 1., 1., 0.],
       [0., 0., 0., 1.],
       [1., 1., 1., 0.]])

或者如果我想创建一个绿色的 4x3 网格:

plot_grid(x, y, size_grid_x=4, size_grid_y=3, color_='green')

# Map points in every cell:
array([[2., 0., 0.],
       [0., 2., 0.],
       [0., 0., 1.],
       [2., 0., 1.]])

编辑:如果你想在同一张图中打印更多数据,只需切换到函数,你只需要将列表列表作为值传递,如果你想要添加图例,您只需将标签列表作为参数传递:

lx = [[1,2,3,4,5,9,8,7], [5,4,10,11,3], [3,6,7,12,7,9]]
ly = [[1,4,9,16,25,7,4,18], [8,20,21,11,17], [13,8,10,12,17,19]]
legend_labels = ['data1', 'data2', 'data3']

plot_grid(lx, ly, legend_labels, size_grid_x=4, size_grid_y=3)

# Map points in every cell:
array([[2., 1., 2.],
       [3., 2., 1.],
       [0., 0., 2.],
       [3., 1., 2.]])

回答你的第二个问题,如果你想在同一个图中打印更多行,请指定标记和颜色(以及更多... see):

import numpy as np
import matplotlib.pyplot as plt

x1, y1 = np.arange(0., 5., 0.2), np.exp(np.arange(0., 5., 0.2)) + 25
x2, y2 = np.arange(0., 5., 0.2), np.arange(0., 5., 0.2)**2
x3, y3 = np.arange(0., 5., 0.2), np.arange(0., 5., 0.2)**3
x4, y4 = np.arange(0., 5., 0.2), np.arange(0., 5., 0.2)**2.5 + 10

# red dot, blue squares and green triangles.
# You can add the plots all in the same command 
plt.plot(x1, y1, 'ro', x2, y2, 'bs', x3, y3, 'g^')
# or individually
plt.plot(x4, y4, 'y*')

plt.show()

在这里您可以找到 table 和 matplotlib.markers, and here the official documentation