wolfram-mathematica - 数学 : Labels and absolute positioning

标签 wolfram-mathematica

您如何在 mathematica 中将文本放置在绘图之外?一个快速的谷歌搜索将
带你到
http://reference.wolfram.com/mathematica/howto/AddTextOutsideThePlotArea.html
这还不够,因为您想用代码实现这一点。在 mathematica 中放置文本的简单示例如下:

    Show[ 
     Plot[x^3, {x, -1, 1},
      Frame -> True, 
      ImageSize -> Medium, 
      FrameLabel -> {"x", "y"},
      PlotRange -> {{-1, 1}, {-1, 1}}
      ],
     Graphics[
      Text[Style["A", Bold, 14, Red], {.5, .5}]]
     ]
这会将字母 A 放置在相对于绘图的点 (.5, .5) 处。有没有办法根据图像的大小放置文本?据我所知,一切都是在情节坐标中完成的。我的临时解决方案是设置选项 PlotRangeClippingFalse并通过给出正确的坐标来设置文本。
Show[
    Plot[
        x^3, {x, -1, 1}, 
        Frame -> True, 
        ImageSize -> Medium, 
        FrameLabel -> {"x", "y"}, 
        PlotRange -> {{-1, 1}, {-1, 1}}
    ],
    Graphics[
        Text[
            Style["A", Bold, 14, Red], 
            {-1.2, 1}
        ]
    ],
    PlotRangeClipping -> False
]
currentsolution
这种方法的一个缺点是,如果我们改变了绘图的范围,那么我们需要重新计算文本的坐标,以将其保持在我们想要的位置(相对于整个图像)。
编辑:
尝试定位 Text A外剧情。
Framed[
    Show[
        Graphics[
            {Orange, Disk[{0, 0}, 3.5]}, 
            Frame -> True, 
            PlotRange -> {{-3, 3}, {-3, 3}}, 
            PlotRangeClipping -> True, 
            FrameLabel -> {"x", "y"}
        ], 
        Graphics[
            Text[
                Style["A", Bold, 14], 
                ImageScaled[{.1, .95}]
            ]
        ]
    ]
]
enter image description here
编辑:
为了找到这个问题的另一个解决方案,我开始写另一篇文章,它给了我克服 belisarius 解决方案存在的问题的想法:将最终图形导出为 pdf 是图形的光栅化版本。查看我的另一个帖子 here为解决方案。
最终编辑?
由于图像链接消失了并且之前编辑中的链接已被修改,我决定
更新图像并包含 Simon 的修改解决方案 answer .
这个想法是创建一个蒙版并在绘制标签之前包含蒙版。通过这种方式
我们正在创建我们自己的 plotRangeClipping .
mask2D = Graphics[{Gray,
    Polygon[{
        ImageScaled[{-0.5, -0.5}],
        ImageScaled[{-0.5, 1.5}],
        ImageScaled[{1.5, 1.5}],
        ImageScaled[{1.5, -0.5}],
        ImageScaled[{-0.5, -0.5}],
        Scaled[{0, 0}],
        Scaled[{1, 0}],
        Scaled[{1, 1}],
        Scaled[{0, 1}],
        Scaled[{0, 0}],
        ImageScaled[{-0.5, -0.5}]
    }]
}];
在某些情况下使用 ImageScaled{1,1}不足以剪辑主图像。为此原因
我通过使用 1.5 提供了更多的报道和 -0.5 .现在我们可以绘制带有标签的图像,如下所示:
Framed@Show[
    Graphics[
        {
            Orange,
            Disk[{0, 0}, 3.5]
        },
        Frame -> True,
        PlotRange -> {{-3, 3}, {-3, 3}},
        FrameLabel -> {"x", "y"}
    ],
    mask2D,
    Graphics[
        Text[
            Style["A", Bold, 14],
            ImageScaled[{0, 1}],
            {-1, 1}
        ]
    ],
    Background -> Red
]
这是所需的图像:
enter image description here
请注意,我已将图像的背景更改为红色。这可以通过更改 Background 轻松修改。属性和掩码只需更改 Gray例如,您喜欢的任何颜色(白色)。

最佳答案

Plot[x^3, {x, -1, 1},
 Frame -> True,
 ImageSize -> Medium,
 FrameLabel -> {"x", "y"},
 PlotRange -> {{-1, 1}, {-1, 1}}],
 PlotRangeClipping -> False,
 Epilog ->
     Text[Style["A", Bold, 14, Red], ImageScaled@{.05, .98}]

enter image description here

编辑

回答你的橙盘部分,问题是Show连接图形选项,因此 PlotRangeClipping 不能有多个值在 Show[ ] 命令中。
克服这种情况的一种方法是:
InsertLabels[g_Graphics, legend__] := 
  Show[Rasterize[g], 
   Graphics[{legend}, 
    Cases[AbsoluteOptions[g], Except[PlotRangeClipping -> True]]]];

g = Graphics[
   {Gray, Disk[{0, 0}, 3.5]},
   Frame -> True,
   PlotRange -> {{-3, 3}, {-3, 3}},
   FrameLabel -> {"x", "y"},
   PlotRangeClipping -> True];

Framed@InsertLabels[g,
 Text[Style["B", Red, Bold, 18], ImageScaled[{0.95, .05}]], 
 Text[Style["A", Red, Bold, 18], ImageScaled[{0.05, .95}]]]

enter image description here

关于wolfram-mathematica - 数学 : Labels and absolute positioning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6274440/

相关文章:

wolfram-mathematica - 如何在Mathematica中打印以n个前导零开头的整数?

wolfram-mathematica - 在嵌套列表中如何测试给定的索引序列在 mathematica 中是否有效

wolfram-mathematica - 如何加速动态显示带有自定义刻度的图形?

wolfram-mathematica - 是什么导致了这个奇怪的 Mathematica 结果?

wolfram-mathematica - 如何修改OUTPUT字体类型?

wolfram-mathematica - 在 Mathematica 中读取 UTF-8 编码的文本文件

wolfram-mathematica - 如何修复 'Equations may not give solutions for all "解决“变量”错误

wolfram-mathematica - 如何在保持标签清晰的同时减小导出图的文件大小

image - ListAnimate 带有像幻灯片一样的控件

java - 为什么 MappedByteBuffer 的 array() 方法不起作用?