简体   繁体   English

fig.add_subplot()* transform *不起作用?

[英]fig.add_subplot() *transform* doesn't work?

Regarding to the post Embedding small plots inside subplots in matplotlib , I'm working on this solution, but for some reason, transform is ignored! 关于在matplotlib中的子图中嵌入小图的帖子,我正在研究这个解决方案,但由于某种原因,转换被忽略了!

I'm in a mistake? 我错了? Or there is a bug? 或者有一个错误?

import matplotlib.pyplot as plt
import numpy as np

axes = []
x = np.linspace(-np.pi,np.pi)
fig = plt.figure(figsize=(10,10))
subpos = (0,0.6)

for i in range(4):
   axes.append(fig.add_subplot(2,2,i))

for axis in axes:
    axis.set_xlim(-np.pi,np.pi)
    axis.set_ylim(-1,3)
    axis.plot(x,np.sin(x))
    fig.add_axes([0.5,0.5,0.1,0.1],transform=axis.transAxes)

plt.show()
import matplotlib.pyplot as plt
import numpy as np

def axis_to_fig(axis):
    fig = axis.figure
    def transform(coord):
        return fig.transFigure.inverted().transform(
            axis.transAxes.transform(coord))
    return transform

def add_sub_axes(axis, rect):
    fig = axis.figure
    left, bottom, width, height = rect
    trans = axis_to_fig(axis)
    figleft, figbottom = trans((left, bottom))
    figwidth, figheight = trans([width,height]) - trans([0,0])
    return fig.add_axes([figleft, figbottom, figwidth, figheight])

x = np.linspace(-np.pi,np.pi)
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10,10))

for axis in axes.ravel():
    axis.set_xlim(-np.pi, np.pi)
    axis.set_ylim(-1, 3)
    axis.plot(x, np.sin(x))
    subaxis = add_sub_axes(axis, [0.2, 0.6, 0.3, 0.3])
    subaxis.plot(x, np.cos(x))

plt.show()

yields 产量

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Matplotlib:删除子图的轴上的编号,该子图是用fig.add_subplot()创建的 - Matplotlib: Removing the numbering on an axes of the subplot, created with fig.add_subplot() 为什么matplotlib.gridspec的紧密布局不能与pyplot.subplot一起正常工作,而与fig.add_subplot一起工作呢? - Why does matplotlib.gridspec's tight_layout not work correctly with pyplot.subplot whereas it does with fig.add_subplot? 在 Matplotlib 中,fig.add_subplot(111) 中的参数是什么意思? - In Matplotlib, what does the argument mean in fig.add_subplot(111)? Seaborn:联合图的子图不起作用 - Seaborn: Subplot of jointplots doesn't work 有没有办法让图 2 成为图 1 的子图? - Is there a way to make fig 2 be a subplot of fig 1? 为什么使用 fig.update_traces(xaxis='x1') 时不打印轴刻度并在第二个子图下方显示标签? - Why when using fig.update_traces(xaxis='x1') doesn't print axis tick and labels bellow second subplot? 为什么spines不能与plot()一起使用,而与matplotlib中的subplot()一起使用? - why spines doesn't work with plot() but works with subplot() in matplotlib? Matplotlib subplot2grid无法正常工作 - Matplotlib subplot2grid doesn't work properly 将 python matplotlib 子图添加到退出 plot 不起作用 - Adding python matplotlib subplot to exiting plot doesn't work 使用set_fig(宽度/高度)调整matplotlib图形的大小不起作用 - Resizing matplotlib figure with set_fig(width/height) doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM