0

我是一个相对较新的 Julia 编码器,我想在一个循环中绘制多个 Gadfly 图并将它们保存为 pdf。

如果我在没有循环的情况下执行此操作,仅针对一个块,代码如下:

plot(x=[0, B_length[2,1,1],B_length[2,1,1],0],
 y=[0,0,1,1],
 layer(x=[B_length[2,1,1]/2], y=[0.5], label=[string("Spec: ", string(K_names[Kblock1[1]]))], Geom.label(position=:centered)),   #specialty label for block 1
 layer(x=[B_length[2,1,1]/2], y=[4.5], label=[string("Surgeon: ", string(S_names[Sblock1[1]]))], Geom.label(position=:centered)),   #surgeon label for block 1
 layer(x=[B_length[2,1,1]/2], y=[2.5], label=[string("No. P = ", string(Pblock1), " L = ", string(Lblock1))], Geom.label(position=:centered)),  #number of patients and emergency
 layer(x=[0, B_length[2,1,1],B_length[2,1,1],0],y=[4,4,5,5], #Surgeon - Block1
             Geom.polygon(fill=true),
             Theme(default_color=color("#00CCCC"))),
 layer(x=[0, block2pt_end,block2pt_end,0],y=[2,2,3,3], #Patient and Emergency - Block1
             Geom.polygon(fill=true),
             Theme(default_color=color("gray"))),

 Geom.polygon(preserve_order=true, fill=true))

并生成此图:

牛虻情节 1

当我返回并用它们的变量替换值并在循环中构建时,我有以下代码:

for i=1:I
  for r=1:R
    B2used = sum(x_blockval[i, :, 2, r])
    if (B2used >= 1)
      k_block2[i,r] = findfirst([x_blockval[i,k,2,r] for k=1:K]) #block=1
      s_block2[i,r] = findfirst([x_sval[r,i,2,s,k_block2[i,r]] for s=1:S])
      p_block2[i,r] = x_pval[r,i,2,k_block2[i,r]]
      l_block2[i,r] = x_lval[r,i,2,k_block2[i,r]]
      block2pt_end[i,r] = F_lμ[k_block2[i,r]] + F_lσ[k_block2[i,r]]
      println("Block 2 used on Day ", i, ", Room ", r, ", Specialty = ", k_block2[i,r], ", Surgeon = ", s_block2[i,r], ", num emergent patients = ", p_block2[i,r], ", num emergency patients = ", l_block2[i,r], ", end time of surgery = ", block2pt_end[i,r])

        plot(x=[0, B_length[r,i,2],B_length[r,i,2],0],
           y=[0,0,1,1],
           layer(x=[B_length[r,i,2]/2], y=[0.5], label=[string("Spec: ", string(K_names[k_block2[i,r]]))], Geom.label(position=:centered)),   #specialty label for block 1
           layer(x=[B_length[r,i,2]/2], y=[4.5], label=[string("Surgeon: ", string(S_names[k_block2[i,r]]))], Geom.label(position=:centered)),   #surgeon label for block 1
           layer(x=[B_length[r,i,2]/2], y=[2.5], label=[string("No. P = ", string(p_block2[i,r]), " L = ", string(l_block2[i,r]))], Geom.label(position=:centered)),  #number of patients and emergency
           layer(x=[0, B_length[r,i,2],B_length[r,i,2],0],y=[4,4,5,5], #Surgeon - Block1
                       Geom.polygon(fill=true),
                       Theme(default_color=color("#00CCCC"))),
           layer(x=[0, block2pt_end,block2pt_end,0],y=[2,2,3,3], #Patient and Emergency - Block1
                       Geom.polygon(fill=true),
                       Theme(default_color=color("gray"))),

           Geom.polygon(preserve_order=true, fill=true))

    end
  end
end

但它不会产生任何图表。理想情况下,我想将所有图表保存在 pdf 中。我知道我可以使用'draw(PDF(“filename.pdf”,6inch,9inch),vstack(p 1,p [2])'或类似的东西......

4

1 回答 1

0

您可以尝试以下简单示例:

using DataFrames
using Distributions
using Gadfly

df = DataFrame(reshape(rand(Normal(600, 5), 1000), 100, 10))

所以我的随机数据看起来像:

100×10 DataFrames.DataFrame
│ Row │ x1      │ x2      │ x3      │ x4      │ x5      │ x6      │ x7      │
├─────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ 1   │ 602.263 │ 605.084 │ 601.423 │ 604.667 │ 600.886 │ 601.023 │ 605.838 │
│ 2   │ 594.87  │ 595.692 │ 598.958 │ 605.384 │ 597.867 │ 597.473 │ 601.041 │
│ 3   │ 607.446 │ 598.39  │ 603.289 │ 603.193 │ 596.1   │ 605.915 │ 606.762 │
│ 4   │ 594.34  │ 599.959 │ 599.37  │ 600.762 │ 593.688 │ 598.451 │ 610.389 │
│ 5   │ 599.136 │ 598.936 │ 603.524 │ 602.486 │ 603.15  │ 615.217 │ 595.568 │
│ 6   │ 602.579 │ 592.314 │ 607.15  │ 599.523 │ 595.064 │ 599.134 │ 595.103 │
│ 7   │ 607.965 │ 593.523 │ 597.015 │ 596.653 │ 587.134 │ 601.784 │ 602.604 │
│ 8   │ 594.854 │ 601.6   │ 595.535 │ 601.941 │ 598.745 │ 598.318 │ 598.639 │
⋮
│ 92  │ 602.218 │ 594.57  │ 598.399 │ 598.049 │ 605.2   │ 599.202 │ 601.325 │
│ 93  │ 603.337 │ 590.793 │ 603.014 │ 592.754 │ 600.761 │ 598.986 │ 603.304 │
│ 94  │ 606.847 │ 600.5   │ 599.646 │ 602.013 │ 603.085 │ 604.451 │ 607.767 │
│ 95  │ 601.857 │ 603.694 │ 601.155 │ 609.234 │ 598.313 │ 593.84  │ 604.078 │
│ 96  │ 601.497 │ 612.724 │ 602.648 │ 599.876 │ 603.636 │ 598.606 │ 596.051 │
│ 97  │ 587.651 │ 597.113 │ 611.405 │ 608.394 │ 601.602 │ 593.162 │ 599.186 │
│ 98  │ 600.314 │ 592.158 │ 598.192 │ 596.135 │ 594.07  │ 612.595 │ 606.035 │
│ 99  │ 603.8   │ 600.477 │ 601.18  │ 601.254 │ 603.464 │ 591.172 │ 605.914 │
│ 100 │ 602.12  │ 599.617 │ 600.363 │ 591.685 │ 607.037 │ 599.461 │ 608.207 │

│ Row │ x8      │ x9      │ x10     │
├─────┼─────────┼─────────┼─────────┤
│ 1   │ 597.964 │ 604.5   │ 606.66  │
│ 2   │ 591.599 │ 599.966 │ 602.308 │
│ 3   │ 590.3   │ 593.76  │ 600.803 │
│ 4   │ 601.73  │ 600.084 │ 601.51  │
│ 5   │ 592.919 │ 600.804 │ 606.705 │
│ 6   │ 602.157 │ 596.47  │ 608.817 │
│ 7   │ 594.768 │ 599.574 │ 604.912 │
│ 8   │ 590.703 │ 599.676 │ 598.265 │
⋮
│ 92  │ 599.965 │ 599.058 │ 603.945 │
│ 93  │ 601.147 │ 604.591 │ 594.569 │
│ 94  │ 597.442 │ 603.474 │ 593.651 │
│ 95  │ 600.611 │ 596.317 │ 598.212 │
│ 96  │ 604.025 │ 599.2   │ 597.129 │
│ 97  │ 595.265 │ 604.271 │ 593.711 │
│ 98  │ 594.281 │ 602.019 │ 603.592 │
│ 99  │ 603.784 │ 592.89  │ 599.684 │
│ 100 │ 592.422 │ 599.386 │ 601.215 │

现在要使用循环绘制数据的每个变量或列并将其以 PDF 格式保存到桌面,您只需运行以下命令:

for i in names(df)
  p1 = plot(x = 1:length(df[i]), y = df[i])
  draw(PDF(joinpath(homedir(), "Desktop/" * string(i) * ".pdf"), 10cm, 10cm), p1)
end
于 2017-04-29T05:12:09.333 回答