首页 > 解决方案 > 使用 matplotlib 绘制 2D 效率

问题描述

我正在尝试在 matplotlib 中绘制二维(无效)效率,基本上,我需要两个直方图的比例。我正在使用 panda DF 来填充它们:

X_arr = np.array(df['X'])
Y_arr = np.array(df['Y'])

Xeff_arr = np.array(df['X'][(df['condition'] == 1)]
Yeff_arr = np.array(df['Y'][(df['condition'] == 1)]

plt.figure()

denom_histo, xedges, yedges = np.histogram2d(Y_arr, X_arr, bins=(100, 100))
eff_histo, xedges, yedges = np.histogram2d(Yeff_arr, Xeff_arr, bins=(100, 100), weights=-1)
ones, xedges, yedges = np.histogram2d(np.array(100*[1]), np.array(100*[1]), bins=(100, 100))

ineff_histo = ones - eff_histo
ineff_histo = ineff_histo / denom_histo

plt.show()

我需要低效率,所以我通过“1 - (data_passing_condition)/(all_data)”计算它也这样做可以防止除以零。

但我收到错误,例如“ValueError:所需数组的深度太小”

您能否让我知道在 matplot 中处理 2D 图的最佳方法是什么?

干杯

标签: pythonarrayspandasnumpymatplotlib

解决方案


经过一些修补后,我发现这个解决方案可行:

def plot_eff2D(df):

        dfe = df[(df['Condition']==1)]

        x = np.array(df['X'])
        y = np.array(df['Y'])

        xe = np.array(dfe['X'])
        ye = np.array(dfe['Y'])

        fig = plt.figure()
        plt.title('Inefficiency map', fontsize=16)

        den, yedges, xedges = np.histogram2d(x, y, bins=100)
        num, _, _ = np.histogram2d(xe, ye, bins=(yedges, xedges))

        H = (den - num)/den  # 1-eff = inefficiency

        img = plt.imshow(H, interpolation='nearest', origin='low', vmin=0, vmax=0.2, extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])
        fig.colorbar(img, fraction=0.025, pad=0.04) # above plot H

        plt.tight_layout()


推荐阅读