【问题标题】:Match legend and Plot size匹配图例和绘图大小
【发布时间】:2011-10-16 19:39:09
【问题描述】:

请考虑:

intense = Reverse[Round[Rationalize /@ N[10^Range[0, 3, 1/3]]]];
values = Range[0, 9/10, 1/10];

intensityLegend = Column[Prepend[MapThread[
                         Function[{intensity, values},
                         Row[{Graphics[{(Lighter[Blue, values]),
                         Rectangle[{0, 0}, {4, 1}], Black, 
                         Text[Style[ToString[intensity], 16, Bold], {2, .5}]}]}]],
                        {intense, values}], Text[Style["Photons Number", Bold, 15]]]];

IntersectionDp1={{1., 588.377}, {2.15443, 580.306}, {4.64159, 573.466}, {10.,560.664}, 
                 {21.5443, 552.031}, {46.4159, 547.57}, {100.,545.051}, 
                 {215.443, 543.578}, {464.159, 542.281}, {1000., 541.346}}


FindD1=ListLogLinearPlot[Map[List, IntersectionDp1], 
                  Frame -> True, 
                  AxesOrigin -> {-1, 0}, 
                  PlotMarkers -> 
                  With[{markerSize = 0.04}, {Graphics[{Lighter[Blue, #], Disk[]}], 
                       markerSize} & /@Range[9/10, 0, -1/10]], Filling -> Axis, 
                  FillingStyle -> Opacity[0.8], 
                  PlotRange -> {{.5, 1100}, {540, 600}},
                  ImageSize->400];

Grid[{{intensityLegend, FindD1}, {intensityLegend, FindD1}}, 
      ItemSize -> {50, 20}, Frame -> True]

如何让图例列大小适合绘图区域的高度?

在 Row 调整大小时,我需要使用 Grid。这就是我在网格中复制的原因。

【问题讨论】:

    标签: wolfram-mathematica plot mathematica-8


    【解决方案1】:

    使用图像尺寸。 (*

    intense = Reverse[Round[Rationalize /@ N[10^Range[0, 3, 1/3]]]];
    values = Range[0, 9/10, 1/10];
    imgSize = 400;                                                          (* <- *) 
    Off[Ticks::ticks]
    
    IntersectionDp1 =  {{1., 588.377},     {2.15443, 580.306}, {4.64159, 573.466}, 
       {10., 560.664}, {21.5443, 552.031}, {46.4159, 547.57},  {100., 545.051},
       {215.443, 543.578}, {464.159, 542.281}, {1000., 541.346}}
    
    FindD1 = ListLogLinearPlot[Map[List, IntersectionDp1], Frame -> True, 
       AxesOrigin -> {-1, 0}, 
       PlotMarkers -> 
        With[{markerSize = 0.04}, 
         {Graphics[{Lighter[Blue, #], Disk[]}], markerSize} & 
           /@ Range[9/10, 0, -1/10]], Filling -> Axis, FillingStyle -> Opacity[0.8], 
           PlotRange -> {{.5, 1100}, {540, 600}}, ImageSize -> imgSize];    (* <- *) 
    
    intensityLegend =
      Rasterize[Column[
        Prepend[
         Reverse@MapThread[                                                 (* <- *) 
          Function[{intensity, values}, 
           Row[{Graphics[{(Lighter[Blue, values]), 
               Rectangle[{0, 0}, {4, 1}], Black, 
               Text[Style[ToString[intensity], 30, Bold], {2, .5}]}]}]],
          {intense, values}],
         Text[Style["Photons Number", Bold, 25]]]], 
       ImageSize -> {Automatic,                                             (* <- *) 
         IntegerPart@                            
          First[imgSize Cases[AbsoluteOptions[FindD1], 
             HoldPattern[AspectRatio -> x_] -> x]]}];
    
    Grid[{{intensityLegend, FindD1}, {intensityLegend, FindD1}}, Frame -> True]
    

    出于审美目的,我颠倒了强度列。

    编辑

    如果您没有为 Plot 明确指定 ImageSize 选项,您会失望地发现 AbsoluteOptions[Plot, "ImageSize"] 返回 "Automatic"

    编辑在下面回答@500的评论

    表达式:

       ImageSize -> {Automatic,                                             (* <- *) 
         IntegerPart@                            
          First[imgSize Cases[AbsoluteOptions[FindD1], 
             HoldPattern[AspectRatio -> x_] -> x]]}];
    

    确实可以替代应该工作但不能获得绘图的图像大小的东西:

       ImageSize -> {Automatic, Last@AbsoluteOptions[FindD1,"ImageSize"]}   
    

    所以,IntegerPart[...] 所做的是获取绘图图像的垂直大小,将 imgSize 乘以绘图的 AspectRatio
    要了解它的工作原理,请运行代码,然后键入:

    AbsoluteOptions[FindD1] 
    

    您将在那里看到绘图选项。那么 Cases[] 函数只是提取了AspectRatio 选项。

    事实上,有一种更简洁的方式来完成 Cases[] 所做的事情。它是:

    AbsoluteOptions[FindD1,"AspectRatio"] 
    

    但是 AbsoluteOptions 函数中还有另一个错误阻止我们以这种方式使用它。

    【讨论】:

    • 你能解释一下下面的部分吗?我认为它有条件地使图例的 y 大小适应情节本身,但我不明白这部分:IntegerPart@ First[imgSize Cases[AbsoluteOptions[FindD1], HoldPattern[AspectRatio -> x_] -> x]]跨度>
    【解决方案2】:

    让图例小一点怎么样?

    intensityLegend = 
      Column[Prepend[
        MapThread[
         Function[{intensity, values}, 
          Row[{Graphics[{(Lighter[Blue, values]), 
              Rectangle[{0, 0}, {4, 1}], Black, 
              Text[Style[ToString[intensity], 12, Bold], {2, .5}]}, 
             ImageSize -> 50]}]], {intense, values}], 
        Text[Style["Photons Number", Bold, 15]]]];
    

    【讨论】:

    • 非常感谢!但我必须承认我还不能掌握 Unit 或 Scaled :-) 所以它仍然很神奇。我希望我们总是在谈论像素!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 2021-08-01
    相关资源
    最近更新 更多