matplotlib 子图之间带有文本的箭头

35g0bw71  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(136)

我想在同一个图上绘制3个子图,表示3个数量A,B和C,它们在数学上相互联系。这些联系应该由带有文本的箭头表示(数学表达式)。所需的结果如下:


的数据

xqnpmsa8

xqnpmsa81#

这里有一个例子。我将一个图形划分为1x3的网格。对于箭头,您可以根据您的需求更改坐标:

import matplotlib.pyplot as plt
import numpy as np

# Create a figure and divide it into a 1x3 grid
fig = plt.figure(figsize=(12, 10))
grid = plt.GridSpec(3, 2)

# Create subplot 1 on the middle of the left side column
ax1 = plt.subplot(grid[1, 0])
ax1.set_title("Subplot 1")
ax1.set_xlim(0, 10)
ax1.set_ylim(0, 10)

# Create subplots 2 and 3 on the right side column
ax2 = plt.subplot(grid[0, 1])
ax2.set_title("Subplot 2")
ax2.set_xlim(0, 10)
ax2.set_ylim(0, 10)

ax3 = plt.subplot(grid[2, 1])
ax3.set_title("Subplot 3")
ax3.set_xlim(0, 10)
ax3.set_ylim(0, 10)

# Add arrows between the subplots
arrowprops = dict(arrowstyle="->", linewidth=2, color='red')

arrow1_2 = plt.annotate('', xy=(-0.1, 0.5), xycoords=ax2.transAxes, xytext=(1.1, 0.6), textcoords=ax1.transAxes, arrowprops=arrowprops)
plt.text(0.43, 0.7, 'Annotation 1-2', transform=fig.transFigure, fontsize=12, color='red')

arrow2_3 = plt.annotate('', xy=(0.5, -0.1), xycoords=ax2.transAxes, xytext=(0.5, 1.1), textcoords=ax3.transAxes, arrowprops=arrowprops)
plt.text(0.75, 0.5, 'Annotation 3-2', transform=fig.transFigure, fontsize=12, color='red')

arrow3_1 = plt.annotate('', xy=(-0.1, 0.5), xycoords=ax3.transAxes, xytext=(1.1, 0.4), textcoords=ax1.transAxes, arrowprops=arrowprops)
plt.text(0.43, 0.3, 'Annotation 1-3', transform=fig.transFigure, fontsize=12, color='red')

# Set the aspect ratio to be equal in all subplots
ax1.set_aspect('equal', adjustable='box')
ax2.set_aspect('equal', adjustable='box')
ax3.set_aspect('equal', adjustable='box')

plt.show()

字符串


的数据

相关问题