匹配图例和情节大小 [英] Match legend and Plot size

查看:105
本文介绍了匹配图例和情节大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑:

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.这就是为什么我在网格中重复.

While Row adjust the size I need to use Grid. This is why I duplicated in grid.

推荐答案

使用图像大小. (*<-*)标记了对代码的重要修改,其余的主要是字体大小的东西:

Working with Image Sizes. The (* <- *) marks the important modifications to your code, the rest are mainly font size thingies:

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]

出于美学目的,我在这里反转了强度列.

Where I reversed the intensities column for aesthetic purposes.

修改

如果未为绘图"明确指定ImageSize选项,则会令人失望地发现AbsoluteOptions[Plot, "ImageSize"]返回"Automatic"

If you don't explicitly specify the ImageSize option for the Plot, you'll disappointingly find that AbsoluteOptions[Plot, "ImageSize"] returns "Automatic" !

编辑回答以下@ 500的评论

Edit Answering the @500's comment bellow

表达式:

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

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

is really a working replacement for something that should work but doesn't to get the image size of a Plot:

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

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

So, what the IntegerPart[...] thing is doing is getting the vertical size of the plot image, multiplying imgSize by the AspectRatio of the Plot.
To understand how it works, run the code and then type:

AbsoluteOptions[FindD1] 

,您将在此处看到绘图"选项.然后,Cases []函数只是提取AspectRatio选项.

and you will see the Plot options there. Then the Cases[] function is just extracting the AspectRatio option.

实际上,有一种更干净的方法来执行Cases []的操作.它是:

In fact there is a cleaner way to do what the Cases[] does. It is:

AbsoluteOptions[FindD1,"AspectRatio"] 

但是AbsoluteOptions函数中还有一个错误使我们无法以这种方式使用它.

but there is another bug in the AbsoluteOptions function that prevents us to use it this way.

这篇关于匹配图例和情节大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆