如何根据直方图中的箱制作辅助图

问题描述 投票:0回答:1

我的数据框中有一列名为

val_a
的列。我绘制了该变量的直方图。我还有一个名为
row_accuracy
的列,我正在尝试将
row_accuracy
值作为
val_a
箱的函数绘制箱线图(具体来说,在下面的示例中,我有三个箱。左箱大多数负值介于 -10 到 -3 之间。我想获取与此 bin 相对应的
row_accuracy
值,并在该 bin 的图上方绘制
row_accuracy
值的箱线图)。我现在的代码如下所示:

import pandas as pd
import matplotlib.pyplot as plt

data = {"val_a"       : [-10, -8, -6, -4, 0, 2, 4, 6, 8, 10],
        "row_accuracy": [0.7, 0.8, 0.95, 0.9, 0.8, 0.7, 0.8, 0.7, 0.85, 0.9], 
}

df = pd.DataFrame(data)

fig, ax = plt.subplots(1, 1)
ax.hist(df['val_a'], bins = 3)
ax1 = ax.twinx()

编辑:

我想出了如何通过这样做来获取

row_accuracy
的相应值

bins = pd.cut(df['val_a'], bins=3)
grouped = df.groupby(bins)['row_accuracy']

现在我只是在努力在直方图上制作箱线图。

python pandas matplotlib data-analysis
1个回答
0
投票

您可以尝试放置插入轴,就像在这个演示中一样:

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

data = {"val_a"       : [-10, -8, -6, -4, 0, 2, 4, 6, 8, 10],
        "row_accuracy": [0.7, 0.8, 0.95, 0.9, 0.8, 0.7, 0.8, 0.7, 0.85, 0.9], 
}

df = pd.DataFrame(data)

# group row_accuracy data according to 3 bins of val_a
bin_labels=['A', 'B', 'C']
bins = pd.cut(df['val_a'], bins=3, labels=bin_labels)
grouped = df.groupby(bins)['row_accuracy']
grouped_values = grouped.apply(list)

fig, ax = plt.subplots(1, 1)
# make a histogram of val_a
ax.hist(df['val_a'], bins = 3)

# slightly expand the plot limits to accommodate a small inset plot above the first bin from the left
min_x, max_x = ax.get_xlim()
min_y, max_y = ax.get_ylim()
min_x, max_x = ax.set_xlim(min_x - (max_x - min_x) * 0.1, max_x + (max_x - min_x) * 0.1)
min_y, max_y = ax.set_ylim(min_y, max_y + (max_y - min_y) * 0.7)

# find absolute coordinates and dimensions of the first bin
rects = ax.patches
b_height = rects[0].get_height()
b_width = rects[0].get_width()
b_x = rects[0].get_x()

# find relative coordinates and dimensions of the first bin
ax_width = (max_x - min_x)
ax_height = (max_y - min_y)
b_x_rel = (b_x - min_x) / ax_width
b_width_rel = b_width / ax_width
b_height_rel = b_height / ax_height

# create a small inset plot inside a bounding box in relative units of the big plot
axins = inset_axes(ax, width="100%", height="100%",
                   bbox_to_anchor=(b_x_rel, b_height_rel + 0.1, b_width_rel, 0.3),
                   bbox_transform=ax.transAxes, loc=3, borderpad=0)
# box plot row_accuracy values for the first bin
axins.boxplot(grouped_values['A'])
axins.xaxis.set_visible(False)

plt.show()

结果:

© www.soinside.com 2019 - 2024. All rights reserved.