python - 使用 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 图的最佳方法是什么?
干杯
解决方案
经过一些修补后,我发现这个解决方案可行:
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()
推荐阅读
- javascript - Change falling hearts background animation to use multiple, random images
- docker - How to Use Environment Variables in a .env file to fill out other environment variable in the same .env file
- python - 如何在字符串数组中找到整数?
- squashfs - Squashfs check compressed file size
- angular - forkJoin some observables with name observable function in result?
- python - 使用脚本查找在 abaqus 中以线性模式创建的零件的边缘
- php - How to subscribe with Exact Online Webhooks
- unit-testing - 如何模拟 TWebRequest/TIdHTTPAppRequest?
- sql - How do NOT EXISTS and correlated subqueries work internally
- python - 迭代地切片列表直到结束