如果你想在较大的一个中插入一个小图,你可以使用Axes,就像这里一样.
问题是我不知道如何在子图中做同样的事情.
我有几个子图,我想在每个子图中绘制一个小图.示例代码将是这样的:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
for i in range(4):
ax = fig.add_subplot(2,2,i)
ax.plot(np.arange(11),np.arange(11),'b')
#b = ax.axes([0.7,0.7,0.2,0.2])
#it gives an error, AxesSubplot is not callable
#b = plt.axes([0.7,0.7,0.2,0.2])
#plt.plot(np.arange(3),np.arange(3)+11,'g')
#it plots the small plot in the selected position of the whole figure, not inside the subplot
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
提前致谢!
我想在matplotlib图中的某些文本周围获取边界框(尺寸).这里的帖子帮助我意识到我可以使用该方法text.get_window_extent(renderer)获取边界框,但我必须提供正确的渲染器.有些后端没有这个方法figure.canvas.get_renderer(),所以我试图matplotlib.backend_bases.RendererBase()获得渲染器并且它没有产生令人满意的结果.这是一个简单的例子
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
fig = plt.figure()
ax = plt.subplot()
txt = fig.text(0.15,0.5,'afdjsklhvvhwd', fontsize = 36)
renderer1 = fig.canvas.get_renderer()
renderer2 = mpl.backend_bases.RendererBase()
bbox1 = txt.get_window_extent(renderer1)
bbox2 = txt.get_window_extent(renderer2)
rect1 = Rectangle([bbox1.x0, bbox1.y0], bbox1.width, bbox1.height, \
color = [0,0,0], fill = False)
rect2 = Rectangle([bbox2.x0, bbox2.y0], bbox2.width, bbox2.height, \
color = [1,0,0], fill = False)
fig.patches.append(rect1)
fig.patches.append(rect2)
plt.draw()
Run Code Online (Sandbox Code Playgroud)
这会产生以下情节:

显然红色框太小了.我认为保罗的答案在这里找到了同样的问题.黑匣子看起来很棒,但我不能使用MacOSX后端,或任何其他没有这种方法的后端figure.canvas.get_renderer(). …
我的问题很简单:在matplotlib中,如何轻松地将Axis系统中的坐标转换为数据系统(从理想情况来说,我正在寻找一个简单的函数output_coords = magic_func(input_coords))
实际上我的确切问题是:我想matplotlib.patches.Ellipse用他的中心在Axis系统中绘制一个但是他的数据系统中的大小(宽度和长度).但是这种transforms.blended_transform_factory方法在这种情况下不起作用.
谢谢 !
python matplotlib coordinate-systems coordinate-transformation