如何在不重写现有数据的情况下使用添加轴绘制多个数据

2024-04-29 10:46:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试创建一个带有缩放的插入图的图形,其中整个图形(所有子图和插图)的数据绘制在代码的不同位置。在

为了模拟代码中不同位置的绘图,最小(不起作用)示例将在绘图例程上循环。在

子图可以工作,但是它们的插入每次都被覆盖。使用“添加轴”创建插入轴。在

我尝试过,不是每次都创建子轴(添加轴),而是仅创建子轴(如果尚未通过以下方式呈现):

try:
    subax1
except NameError:
    subax1 = fig666.add_axes([0.5,0.71,0.35,0.16]) 

这也没用!在

我如何解决这个问题/我的概念性误解是什么?在

谢谢你的帮助!!!在

^{pr2}$

Tags: 数据代码add图形绘图示例方式绘制
2条回答

这是你想要的吗?在

import matplotlib.pyplot as plt
import numpy

x_data=numpy.array([0,    1,   1.85,   1.9,  1.95,   2, 2.1, 2.5,   5,  10, 25])
y_data=numpy.array([0,  2.5,    1.8,   0.5,   0.2,  11, 1.2, 0.5, 0.15, 10, 25])
y_data_err=y_data*0.1

number_of_runs=3
index_lim=2

for iterator in range(number_of_runs):
    fig666=plt.figure(666,figsize=(22.0/2.54,18.0/2.54))
#############################
    # subplot
    ax = plt.subplot(3,1,iterator+1)
    ax.plot(x_data,y_data+iterator*0.2,marker=None)              
    ax.errorbar(x_data,y_data+iterator*0.2,yerr=y_data_err)
    plt.semilogy()


    #####################
    #  zoomed inset to subplot ##
    [x0,y0], [x1, y1] = fig666.transFigure.inverted().transform(
        ax.transAxes.transform([[0.5, 0.2], [0.95, 0.9]]))
    subax1 = fig666.add_axes([x0, y0, x1-x0, y1-y0])
      #subax1.plot(x_data,y_data+iterator*0.2+0.1,marker=None)              
    subax1.errorbar(x_data,y_data+iterator*0.2,yerr=y_data_err)

    subax1.set_xlim(1.87,2.25)
    subax1.set_ylim(0,3.7)

输出:

enter image description here

要创建三个大轴,您需要更改subplot()的第三个参数:

^{pr2}$

为了创建缩放轴,我使用transFiguretransAxes将轴上的点转换为图形中的点。在

[x0,y0], [x1, y1] = fig666.transFigure.inverted().transform(
        ax.transAxes.transform([[0.5, 0.2], [0.95, 0.9]]))

这个问题似乎是由subblot命令引起的:由于inset plot/axes完全在子blot内,当再次调用subblot时,inset axes/plot被删除。使用add_axes来创建周围的轴而不是子批(3,1,1)修复了这个问题:

import matplotlib.pyplot as plt
import numpy

x_data=numpy.array([0,    1,   1.85,   1.9,  1.95,   2, 2.1, 2.5,   5,  10, 25])
y_data=numpy.array([0,  2.5,    1.8,   0.5,   0.2,  11, 1.2, 0.5, 0.15, 10, 25])
y_data_err=y_data*0.1



number_of_runs=3
index_lim=2

for iterator in range(number_of_runs):
    fig666=plt.figure(666,figsize=(22.0/2.54,18.0/2.54))
#############################
    # subplot
    #ax = plt.subplot(3,1,3)
    ax=fig666.add_axes([0.125,0.666,0.775,0.235])
    ax.plot(x_data,y_data+iterator*0.2,marker=None)              
    ax.errorbar(x_data,y_data+iterator*0.2,yerr=y_data_err)
    plt.semilogy()


    #####################
    #  zoomed inset to subplot ##
    subax1 = fig666.add_axes([0.5,0.71,0.35,0.16])
      #subax1.plot(x_data,y_data+iterator*0.2+0.1,marker=None)              
    subax1.errorbar(x_data,y_data+iterator*0.5,yerr=y_data_err)

    plt.xlim(1.87,2.25)
    plt.ylim(0,3.7)
plt.show()
ax = plt.subplot(3,1,2) 

相关问题 更多 >