我需要比较光谱列表的多个版本,我正在为每个光谱生成一个图形对象,我计划在其中显示相同光谱的不同版本。每个频谱的每个版本都有两个"翅膀"我想在同一页面上显示。为此我做了一些事情(我将光谱表示为numpy数组,我通常从fit文件中获取它们):
import os
import sys
import numpy as np
import matplotlib.pyplot as mplt
import matplotlib.gridspec as gs
import numpy.random as rnd
PageGrid = gs.GridSpec( 11, 1, hspace = 0.0)
Spec1 = rnd.randint(1, 100, 100)
Spec2 = rnd.randint(1, 100, 100)
Spec3 = rnd.randint(1, 100, 100)
LeftSpecList = ['Spec1', 'Spec2', 'Spec3']
RightSpecList = ['Spec1', 'Spec2', 'Spec3']
for leftSpec in LeftSpecList:
fig = mplt.figure(str(leftSpec))
axTop = fig.add_subplot( PageGrid[:5, :] )
axTop.plot(eval(leftSpec))
for rightSpec in RightSpecList:
fig = mplt.figure(str(rightSpec))
axBottom = fig.add_subplot( PageGrid[6:, :] )
axBottom.plot(eval(rightSpec))
然后我有第二个光谱列表,我想在其他光谱上绘制(可能有或没有相同的长度):
LeftSpecListNew = ['Spec1', 'Spec2']
RightSpecListNew = ['Spec2', 'Spec3']
for leftSpec in LeftSpecList:
fig = mplt.figure(str(leftSpec))
axTop.plot(eval(leftSpec))
for rightSpec in RightSpecList:
fig = mplt.figure(str(rightSpec))
axBottom.plot(eval(leftSpec))
mplt.show()
但是当我这样做时,它会在第一个数字上打印出来。有什么想法吗?
答案 0 :(得分:1)
axTop和axBottom在前两个循环中被覆盖。您需要跟踪每个图形的每个顶部和底部轴。这样的事情应该做你想要的。
axTopDict = {}
for leftSpec in LeftSpecList:
fig = mplt.figure(str(leftSpec))
axTop = fig.add_subplot( PageGrid[:5, :] )
axTop.plot(eval(leftSpec),'ro')
axTopDict[leftSpec] = axTop # associate this axis with this figure name
axBottomDict = {}
for rightSpec in RightSpecList:
fig = mplt.figure(str(rightSpec))
axBottom = fig.add_subplot( PageGrid[6:, :] )
axBottom.plot(eval(rightSpec),'b*')
axBottomDict[rightSpec] = axBottom # associate this axis with this figure name
LeftSpecListNew = ['Spec1', 'Spec2']
RightSpecListNew = ['Spec2', 'Spec3']
for leftSpec in LeftSpecList:
fig = mplt.figure(str(leftSpec))
axTop = axTopDict[leftSpec] # get the top axis associated with this figure name
axTop.plot(eval(leftSpec),'m')
for rightSpec in RightSpecList:
fig = mplt.figure(str(rightSpec))
axBottom = axBottomDict[rightSpec] # get the bottom axis associated with this figure name
axBottom.plot(eval(leftSpec),'g')
mplt.show()