【问题标题】:How to plot multiple data usind add_axes without over writing existing如何使用 add_axes 绘制多个数据而不覆盖现有数据
【发布时间】:2016-01-16 11:40:27
【问题描述】:

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

为了模拟代码中不同位置的绘图,最小(不工作)示例循环遍历绘图例程。

子图可以工作,但每次都会覆盖它们的插图。插入轴是使用 add_axes 创建的。

我已经尝试过,不是每次都创建子轴(add_axes),而是只创建它们,如果还没有的话:

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

这也没有帮助!

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

感谢您的帮助!!!

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
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,1)
    #ax.plot(x_data,y_data+iterator*0.4,marker=None)              
    ax.errorbar(x_data,y_data+iterator*0.4,yerr=y_data_err)
    #plt.semilogy()
    plt.xlim(1.87,2.25)
    plt.ylim(0,3.7)   

    #####################
    #  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.4,yerr=y_data_err)

    plt.xlim(1.87,2.25)
    plt.ylim(0,3.7)

【问题讨论】:

  • 我稍微编辑了代码,使其更加清晰:子图 (3,1,1) 中应该有三个误差线图,插图中应该有三个误差线图。然而,插图仅显示一个。清楚我的意思吗?有人可以帮忙吗?据我了解文档,如果参数相同(它们在上面的示例中), add_axes 命令不应覆盖现有轴。是bug吗??来自 matplotlib.org 的引用:“如果图形已经有一个具有相同参数的轴,那么它只会使该轴成为当前轴并返回它......”

标签: python matplotlib plot subplot errorbar


【解决方案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,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)

输出:

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

ax = plt.subplot(3,1,iterator+1)

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

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

【讨论】:

  • 感谢您的快速回复。最后我想要几个子图,但这不是我的问题。我发布的代码旨在创建: * 单个图形 * 带有一个子图 * 其中单个子图包含单个插图(由 add_axes 创建) * 在每次运行中,应在子图中绘制一个稍微偏移的图形以及在插图中。 *它适用于子图,但不适用于插图,其中总是只保留最后绘制的,
  • 即:在插图中只有一个误差线图(蓝线),而应该有三个(如在子图中)。这就是问题所在:插图在每个循环中都被重置
  • 我稍微编辑了代码,使其更加清晰:子图(3,1,1)中应该有三个误差线图,插图中应该有三个误差线图。然而,插图仅显示一个。清楚我的意思吗?有人可以帮忙吗?据我了解文档,如果参数相同(它们在上面的示例中), add_axes 命令不应覆盖现有轴。是bug吗??来自 matplotlib.org 的引用:“如果图形已经有一个具有相同参数的轴,那么它只会使该轴成为当前轴并返回它......”
【解决方案2】:

问题似乎是由 subplot 命令引起的:由于 inset plot/axes 完全在 subplot 内,因此在再次调用 subplot 时删除了 inset axes/plot。使用 add_axes 创建周围的轴而不是 subplot(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) 

【讨论】:

    猜你喜欢
    • 2012-04-24
    • 1970-01-01
    • 2012-07-24
    • 2020-09-26
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多