我正在尝试构建一个图,其中我有固定数量的行,但每行的列数不同。我用于绘图的代码等效于:
import matplotlib.pyplot as pl
pl.figure()
pl.ion()
pl.subplot(2,1,1)
pl.title("Sets the title for top plot")
plotData(data[0]) # Function that plots data in this first row
for i in range(3):
pl.subplot(2,3,4+i)
pl.title("Sets the title of each subplot in second row")
plotData(data[i+1]) # Plots the data in second row
现在由于某种原因,第二行子图中绘制的数据消失了。当我调试它时,它似乎一直存在,直到从 for 循环中的 plotData() 函数返回(或者可能在调用 下一个子图命令 - 但这并不能解释为什么最后一个子图是空的......)。
编辑:
plotData()
函数末尾的代码触发了问题:
pl.gca().set_xlim(0,15)
pl.gca().set_ylim(0,15)
这是否意味着我无法在绘制数据后设置 x、y 限制,或者我在这里做错了什么?
可以找到显示问题的最小示例的 pastebin here
最佳答案
好吧,那太尴尬了。如果你看一下 pastebin 上的代码,问题是我设置的 xlim 和 ylim 没有考虑到我切换坐标系方向(在 y 方向)的事实。在 plotData()
中:
for i,dset in enumerate(data):
x = np.array([point['x'] for point in dset])
y = np.array([-point['y'] for point in dset])
我对 set_xlim
和 set_ylim
的调用完全按照预期进行,只是由于我设置的限制,数据在这些图中不再可见(因为我在数据上切换了 y 轴的符号,但没有在极限上切换)。
关于python - Matplotlib子图内容消失使用set_xlim,set_ylim,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14475856/