2015-01-09 62 views
2

我有3个不同的图,当前每个图都保存为单独的图。然而,由于空间限制,我想层在他们身后互相抵消,像这样:以编程方式在matplotlib中绘制叠加的偏移图

Example image

我试图传达了类似的模式在每个情节存在,这是一个很好的和紧凑的方式这样做。我想以编程方式使用matplotlib绘制这样的图形,但我不确定如何使用通常的pyplot命令对图形进行分层和偏移。任何的意见都将会有帮助。以下代码是我目前所拥有的骨架。

import numpy as np 
import matplotlib.pyplot as plt 
import seaborn as sns 

window = 100 
xs = np.arange(100) 
ys = np.zeros(100) 
ys[80:90] = 1 
y2s = np.random.randn(100)/5.0+0.5 

with sns.axes_style("ticks"): 
    for scenario in ["one", "two", "three"]: 
     fig = plt.figure() 
     plt.plot(xs, ys) 
     plt.plot(xs, y2s) 
     plt.title(scenario) 
     sns.despine(offset=10) 

回答

4

您可以手动创建轴以绘制并定位它们,只要你喜欢。 为了突出这种方法修改您的示例如下

import numpy as np 
import matplotlib.pyplot as plt 
import seaborn as sns 

window = 100 
xs = np.arange(100) 
ys = np.zeros(100) 
ys[80:90] = 1 
y2s = np.random.randn(100)/5.0+0.5 

fig = plt.figure() 
with sns.axes_style("ticks"): 
    for idx,scenario in enumerate(["one", "two", "three"]): 
     off = idx/10.+0.1 
     ax=fig.add_axes([off,off,0.65,0.65], axisbg='None') 
     ax.plot(xs, ys) 
     ax.plot(xs, y2s) 
     ax.set_title(scenario) 
     sns.despine(offset=10) 

其给出类似
enter image description here

的曲线图在这里,我使用fig.add_axes添加手动创建的对象坐标轴与预定图形对象。参数指定新创建的轴的位置和大小,请参阅docs。 请注意,我还将轴背景设置为透明(axisbg='None')。

+0

谢谢!我的示例数字对此很糟糕,但您的方法运行良好。 – tlnagy 2015-01-09 22:36:59