在Mathematica中绘制传说 [英] plotting legends in Mathematica

查看:119
本文介绍了在Mathematica中绘制传说的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用PlotLegends软件包的情况下为功能绘制图例?

How do you plot legends for functions without using the PlotLegends package?

推荐答案

我也对使PlotLegend正常工作的困难感到失望.我编写了自己的简短函数来制作自己的自定义人物传说:

I, too, was disappointed by the difficulty of getting PlotLegend to work correctly. I wrote my own brief function to make my own custom figure legends:

makePlotLegend[names_, markers_, origin_, markerSize_, fontSize_, font_] := 
  Join @@ Table[{
    Text[
      Style[names[[i]], FontSize -> fontSize, font], 
      Offset[
        {1.5*markerSize, -(i - 0.5) * Max[markerSize,fontSize] * 1.25},
        Scaled[origin]
      ],
      {-1, 0}
    ],
    Inset[
      Show[markers[[i]], ImageSize -> markerSize],
      Offset[
        {0.5*markerSize, -(i - 0.5) * Max[markerSize,fontSize] * 1.25},
        Scaled[origin]
      ],
      {0, 0}, 
      Background -> Directive[Opacity[0], White]
    ]
  },
  {i, 1, Length[names]}
];

它很灵活,但使用起来并不容易. 名称"是要在图例中呈现的字符串的列表; 标记"是一个列表,其长度与表示绘图标记或要渲染的图形的图形"对象的名称"相同; "origin"是一个由两个元素组成的列表,具有图例左上角的绝对水平和垂直位置; "markerSize"是将标记缩放到的点数; "fontSize"是字体大小; 字体"是要使用的字体的名称.这是一个示例:

It is flexible, but not so easy to use. "names" is a list of strings to render in the legend; "markers" is a list with the same length as "names" of Graphics objects representing the plot markers or graphics to render; "origin" is a two-element list with the absolute horizontal and vertical position of the upper-left corner of the legend; "markerSize" is the number of points to scale the markers to; "fontSize" is the font size; "font" is the name of the font to use. Here is an example:

Plot[{x, x^2}, {x, 0, 2}, PlotStyle -> {Blue, Red},
  Epilog -> makePlotLegend[
    {x, x^2},
    (Graphics[{#, Line[{{-1, 0}, {1, 0}}]}]) & /@ {Blue, Red},
    {0.9, 0.3},
    12,
    12,
    "Arial"
  ]
]

这篇关于在Mathematica中绘制传说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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