python - 使用网格评估数据

标签 python plot grid evaluation

假设我有这样的列表

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()

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

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

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

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

提前致谢!

最佳答案

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

该函数以及绘图返回一个矩阵 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必须解释如下:图的左下单元格中有 3 个点,左上 单元格中有 1 个点,右下角有 2 个点 单元格和 右上角 单元格中的 2 个点。 enter image description here

或者如果我想创建一个 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.]])

enter image description here

或者如果我想创建一个绿色的 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.]])

enter image description here

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

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.]])

enter image description here

关于python - 使用网格评估数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58062393/

相关文章:

python - 将一个类的多个函数绑定(bind)到另一个类的类事件: is there a better/shorter way?

python - 在世界地图上绘制 GeoIP 数据

r - 如何获得R中回归分析生成的四个图?

c# - 如何在 ZedGraph 中将特定曲线置于最前面

java - 最新的Java swing JTABLE GRID advance 免费开源库有哪些?

javascript - Angular UI 网格 : How do I make a second ui grid visible from a button in the first ui grid?

python - Django 表单提交给我一个 405 错误

python - 将 PyGUI 添加到项目中

html - 按钮的宽度不会与其所在的列相同

javascript - 如何在 MongoDB 集合上循环文档并对每个文档执行发布请求?