首页 > 解决方案 > 如何在从另一个子图中放大的子图之间创建连接线?

问题描述

我有一个如下所示的数据框: 在此处输入图像描述

我已经能够创建一个放大的子图,以在一个 1990 年到 2019 年的图中描绘 1990 年到 2019 年的值。我想添加连接线,如附图中红色所示:在此处输入图像描述

我用来获取上图的代码如下:

fig, ax = plt.subplots(figsize=(20, 12))
ax = df.plot(kind="area", stacked=True, ax=ax,
             color=colors)
ax.set_ylabel("TWh")
ax.set_title(
    "Global Energy Consumption from 1800 to 2019 \nSource: BP Statistical Review of World Energy 2021")
ax.legend(bbox_to_anchor=(1, 1))

# Create a plot for zooming in
# left, bottom, width, height
ax_new = fig.add_axes([0.4, 0.5, 0.3, 0.3])
ax_new = df.loc[1990:].plot(kind="area", stacked=True, ax=ax_new, color=colors)
ax_new.set_ylabel("TWh")
ax_new.set_title("1990 to 2019")
ax_new.legend("", frameon=False)

# plt.tight_layout()
plt.savefig("Global energy consumption", dpi=300)
plt.show()

如何在两个子图中的 xaxes 之间添加连接线?

标签: pythonpython-3.xpandasmatplotlibsubplot

解决方案


推荐阅读