使用matplotlib绘制2D效率

时间:2019-02-28 13:53:29

标签: python arrays pandas numpy matplotlib

我试图在matplotlib中绘制2D(无效)效率,基本上,我需要两个直方图的比率。我正在使用熊猫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绘图的最佳方法是什么吗?

欢呼

1 个答案:

答案 0 :(得分:0)

经过一番修补后,我发现此解决方案有效:

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