matplotlib 有没有办法在子情节之间画一条线或箭头?[副本]

pftdvrlh  于 2023-05-23  发布在  其他
关注(0)|答案(1)|浏览(126)

此问题已在此处有答案

Arrows between subplots(1个答案)
3小时前关闭
我目前有两个子图,一个饼图和一个条形图,我想知道是否有一种方法可以添加一条线或箭头来表示饼图的红色部分是指堆叠条形图。
从这里:

对此:

这是我目前为止的代码:

opfig, (ax1, ax2) = plt.subplots(1, 2, figsize = (12,6), dpi=80)

percentage = operator['Percentage']
labels = operator['Operator']
explode = (0,0,0,0.1)

label = [f'{lab}, {per:0.1f}%' for lab, per in zip(labels,percentage*100)]
ax1.pie(operator['Count'], explode = explode)
ax1.legend(labels=label, title='Operator Type')

militaryPer = military['Percentage']
militaryLab = military["Military"]

bottom = 1
width = 0.2

for j, (height, label) in enumerate([*zip(militaryPer, militaryLab)]):
    bottom -= height
    bc = ax2.bar(0, height, width, bottom = bottom, color="C0", label=label, alpha=0.1+0.25*j)
    ax2.bar_label(bc, labels=[F"{height:.0%}"], label_type='center')

ax2.set_title('Military Operators')
ax2.legend(loc='lower right', prop={'size': 10}, borderpad=1, framealpha=0.9)
ax2.axis('off')
ax2.set_xlim(-1.5 * width, 1.5 * width)

plt.show()
mxg2im7a

mxg2im7a1#

您可以使用Matplotlib添加箭头:

# Add a line or arrow connecting the pie chart and stacked bar chart
line_start = (0.5, 0.2)  # (x, y) coordinates for the start of the line
line_end = (2.5, 0.5)  # (x, y) coordinates for the end of the line
arrow_props = dict(arrowstyle='->', color='black')
ax1.annotate('', xy=line_start, xytext=line_end, arrowprops=arrow_props)

# Display the plot
plt.show()

可以根据需要修改坐标和箭头特性,以便根据特定的可视化效果正确定位直线或箭头。

相关问题