[英]Matplotlib subplot content disappears using set_xlim, set_ylim
I am trying to construct a plot where I have a fixed number of rows but differing number of columns for each row. 我正在尝试构建一个图,其中行数固定,但每行的列数不同。 The code I use for plotting is equivalent to: 我用于绘图的代码等效于:
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
Now for some reason the data plotted in the subplots of the second row disappears. 现在由于某种原因,第二行子图中绘制的数据消失了。 When I debug it it seems as it is there until returning from the plotData() function within the for-loop (or possibly upon calling the next subplot command - but this does not explaing why the last subplot is empty ...). 当我调试它时,它似乎就在那里,直到从for循环中的plotData()函数返回为止(或者可能在调用下一个子图命令时-但这不能解释为什么最后一个子图为空...)。
There was code at the end of the plotData()
function that triggered the problem: plotData()
函数末尾有代码触发了该问题:
pl.gca().set_xlim(0,15)
pl.gca().set_ylim(0,15)
Does this mean that I can not set the x,y-limits after plotting the data, or what am I doing wrong here? 这是否意味着我在绘制数据后无法设置x,y限制,或者我在这里做错了什么?
A pastebin of the minimal example showing the problem can be found here 可以在此处找到显示问题的最小示例的pastebin
OK, that was embarrassing. 好,那很尴尬。 If you take a look at the code on pastebin the problem was that the xlim and ylim that I was setting did not take into account the fact that I switched direction of the coordinate system (in y-direction). 如果您看一下pastebin上的代码,问题是我设置的xlim和ylim没有考虑我切换坐标系方向(沿y方向)的事实。 In plotData()
: 在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])
My calls to set_xlim
and set_ylim
did exactly what they were supposed to, it was just that with the limits I set the data was no longer visible in these plots (since I switched the sign of the y-axis on the data but not on the limits). 我对set_xlim
和set_ylim
调用完全符合他们的预期,只是由于设置了限制,因此在这些图中不再可见数据(因为我在数据上切换了y轴的符号,但在限制)。
I agree with @Zhenya - I don't see this functionality with the latest matplotlib (the development version 1.3-dev and suspect it is fixed in at least v1.2.0, but maybe even as early as v1.0.1). 我同意@Zhenya-我在最新的matplotlib中看不到此功能(开发版本1.3-dev怀疑至少在v1.2.0中已修复,但甚至早在v1.0.1中就已修复)。
HTH, HTH,
UPDATE: Added output from mpl v1.2.0 : 更新:添加了mpl v1.2.0的输出 :
> python
Python 2.7.2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> print matplotlib.__version__
1.2.0
>>>
>>> import matplotlib.pyplot as plt
>>>
>>> plt.ion()
>>>
>>> plt.subplot(2,1,1)
<matplotlib.axes.AxesSubplot object at 0x283d790>
>>> plt.title("Sets the title for top pltot")
<matplotlib.text.Text object at 0x2851f50>
>>>
>>> plt.plot(range(10, 0, -1))
[<matplotlib.lines.Line2D object at 0x2c40790>]
>>>
>>> for i in range(3):
... plt.subplot(2,3,4+i)
... plt.title("Sets the title of each subplot in second row")
... plt.plot(range(10))
...
<matplotlib.axes.AxesSubplot object at 0x2c40e90>
<matplotlib.text.Text object at 0x2c64a90>
[<matplotlib.lines.Line2D object at 0x2c6e990>]
<matplotlib.axes.AxesSubplot object at 0x2c6ec10>
<matplotlib.text.Text object at 0x2e6c810>
[<matplotlib.lines.Line2D object at 0x2e78110>]
<matplotlib.axes.AxesSubplot object at 0x2e78390>
<matplotlib.text.Text object at 0x2e8ced0>
[<matplotlib.lines.Line2D object at 0x2e98610>]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.