0

I am trying to plot a 2D (in)efficiency in matplotlib, basically, I need the ration of two histograms. I am using panda DF to fill them:

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

I need inefficiency, so I calculate it by "1 - (data_passing_condition)/(all_data)" Also doing this prevents division by zero.

But I get errors, like "ValueError: object of too small depth for desired array"

Could you let me know what's the best way to deal with 2D plots in matplot?

Cheers

1
  • Which line are you getting the error in? Can you provide us some sample data so we can reproduce your error? Also, it doesn't seem to me that you ever actually tell your code to plot the ratio. You could do something like this example to plot it. Commented Feb 28, 2019 at 14:53

1 Answer 1

0

after some tinkering I found this solution to work:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.