用数据子集绘图

时间:2017-09-14 02:52:01

标签: python plot subset

所以我有各种数据阵列,其中治疗用虚拟变量编码,如

x=[0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1]
z=[1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0]
d=[1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]
y=[1,2,3,4,5,0,0,1,0,1,1,10,20,35,50,1,10,15,20,25]

我想在单独的数字中同时绘制四个地块,每个处理组合作为一个地块。

所以

plt.figure(1)
plt.subplot(411)
plt.plot(d,y[x==0],[z==0])

plt.figure(2) 
plt.subplot(412)
plt.plot(d,y[x==1],[z==0])

plt.figure(3)
plt.subplot(421)
plt.plot(d,y[x==0],[z==1])

plt.figure(4)
plt.subplot(422)
plt.plot(d,y[x==1],[z==1])

p.show()

但我得到一个错误,说第三个参数必须是格式字符串。

我不想在绘图之前将数据分解成更小的块,因为我将在绘图之前通过回归模型运行数据,看起来好像它可能使事情变得复杂太多。

任何帮助都会受到赞赏,因为我对python缺乏经验。 谢谢

1 个答案:

答案 0 :(得分:0)

来自OP的澄清: 我试图在一个窗格中获得4个图表;

  1. 当x = 0且z = 0时,第一个图是a-y,
  2. 当x = 1且z = 0
  3. 时,第二个图是a y
  4. 当x = 0且z = 1且
  5. 时,第三个是〜y
  6. 当x = 1且z = 1
  7. 时,最后一个是〜y

    要实现这一目标,您需要在绘制之前提取正确的数据。

    import matplotlib.pyplot as plt
    
    x=[0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1]
    z=[1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0]
    y=[1,2,3,4,5,0,0,1,0,1,1,10,20,35,50,1,10,15,20,25]
    d=[1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]
    
    
    plt.figure(1)
    
    zipped = list(zip(x, z, y, d))
    filtered = [(ey, ed) for ex, ez, ey, ed in zipped if ex==0 and ez==0]
    unzipped = list(zip(*filtered))
    plotx = unzipped[1]
    ploty = unzipped[0]
    
    plt.subplot(411)
    plt.title("x=0 z=0")
    plt.plot(plotx, ploty, 'o-')
    
    zipped = list(zip(x, z, y, d))
    filtered = [(ey, ed) for ex, ez, ey, ed in zipped if ex==0 and ez==1]
    unzipped = list(zip(*filtered))
    plotx = unzipped[1]
    ploty = unzipped[0]
    
    plt.subplot(412)
    plt.title("x=1 z=0")
    plt.plot(plotx, ploty, 'o-')
    
    zipped = list(zip(x, z, y, d))
    filtered = [(ey, ed) for ex, ez, ey, ed in zipped if ex==1 and ez==0]
    unzipped = list(zip(*filtered))
    plotx = unzipped[1]
    ploty = unzipped[0]
    
    plt.subplot(413)
    plt.title("x=0 z=1")
    plt.plot(plotx, ploty, 'o-')
    
    zipped = list(zip(x, z, y, d))
    filtered = [(ey, ed) for ex, ez, ey, ed in zipped if ex==1 and ez==1]
    unzipped = list(zip(*filtered))
    plotx = unzipped[1]
    ploty = unzipped[0]
    
    plt.subplot(414)
    plt.title("x=1 z=1")
    plt.plot(plotx, ploty, 'o-')
    
    plt.show()