我如何绘制带有箭头指向特定点的嵌套图的子图?
How can I plot subplots with nested plot arrowed at a specific point?
我在论文中看到这张图表,需要重现它。
如何在 Python 中绘制这样的图形?
注意:
- 我怀疑更大的子图可能是使用 seaborn 或使用 matplotlib 的子图绘制的
- 较小的图指向较大图中曲线的特定部分。
一个策略可以使用 mpl_toolkits.axes_grid1.inset_locator
,正如这个问题的答案中所建议的:
我做了一个简单的例子:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import math
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
x = [n/10 for n in range(0,101)]
y = [n*n*(1-math.sin(n*10)/5) for n in x] # just create some kind of function
ax.plot(x,y) # this is the main plot
# This produces the line that points to the location.
ax.annotate("", (x[50],y[50]),
xytext=(4.0,65),
arrowprops=dict(arrowstyle="-"),)
#this is the small figure
ins_ax = inset_axes(ax, width=1.5, height=1.5,
bbox_transform=ax.transAxes, bbox_to_anchor=(0.45,0.95),)
# the small plot just by slicing the original data
ins_ax.plot(x[45:56],y[45:56])
plt.show()
这更像是一个概念证明,可以专门解决您提出的问题。它显然需要根据您的情况进行调整和调整,以适合发布。希望对您有所帮助。
我在论文中看到这张图表,需要重现它。
如何在 Python 中绘制这样的图形?
注意:
- 我怀疑更大的子图可能是使用 seaborn 或使用 matplotlib 的子图绘制的
- 较小的图指向较大图中曲线的特定部分。
一个策略可以使用 mpl_toolkits.axes_grid1.inset_locator
,正如这个问题的答案中所建议的:
我做了一个简单的例子:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import math
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
x = [n/10 for n in range(0,101)]
y = [n*n*(1-math.sin(n*10)/5) for n in x] # just create some kind of function
ax.plot(x,y) # this is the main plot
# This produces the line that points to the location.
ax.annotate("", (x[50],y[50]),
xytext=(4.0,65),
arrowprops=dict(arrowstyle="-"),)
#this is the small figure
ins_ax = inset_axes(ax, width=1.5, height=1.5,
bbox_transform=ax.transAxes, bbox_to_anchor=(0.45,0.95),)
# the small plot just by slicing the original data
ins_ax.plot(x[45:56],y[45:56])
plt.show()
这更像是一个概念证明,可以专门解决您提出的问题。它显然需要根据您的情况进行调整和调整,以适合发布。希望对您有所帮助。