You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Maxima的plot2d()中用数值列表生成图表图例标签?

问题描述

假设某微分方程的显式解为:

(%i1) x(t):=((x[0]*%e^(k*t))/(x[0]*%e^(k*t)-x[0]+1))

显然参数k会影响解曲线的斜率,我们可以绘制不同k值(本例中k从0.25到1.75,步长0.25)的曲线来观察。

尝试用以下代码绘图:

(%i6) plot2d(
        makelist(subst([k=d*0.25,x[0]=0.1],x(t)), d,1,7),
        [t,0,15],
        cons(legend, makelist("k=d*0.25", d,1,7)),
        [gnuplot_preamble,"set key right bottom"]
      )$

但生成的图表图例标签是静态的“k=d*0.25”,无法区分不同k值。

之后修改代码:

(%i7) makelist(subst([k=d*0.25,x[0]=0.1],x(t)), d,1,7);
          plot2d(%, [t,0,15],
          [gnuplot_preamble,"set key nobox spacing 1.1 bottom right"]
      )$

这比之前有所改进,但图例显示的是完整函数表达式,可读性较差。

我们希望图例仅显示对应的k值列表:

(%i8) kV: makelist(k=d*0.25, d,1,7);
(%o8) [k=0.25,k=0.5,k=0.75,k=1.0,k=1.25,k=1.5,k=1.75]

尝试以下代码时出现错误:

(%i11) plot2d(
           makelist(subst([k=d*0.25,x[0]=0.1],x(t)), d,1,7),
           [t,0,15],
           cons(legend, makelist(kV)),
           [gnuplot_preamble,"set key right bottom"]
       )$

Value of option legend. should be a string or false, not "
                [k = 0.25,k = 0.5,k = 0.75,k = 1.0,k = 1.25,k = 1.5,k = 1.75]
                ".
-- an error. To debug this try: debugmode(true);

请问如何将这些k值正确设置为图例标签?

问题解决

使用以下代码可生成正确的图表:

plot2d(
    makelist(
      subst([k=d*0.25,x[0]=0.1],x(t)), d,1,7),
    [t,0,15],
    cons(legend, map(string, kV)),
    [gnuplot_preamble,"set key right bottom"]
)$

使用Maxima的printf()函数可优化图例格式:

plot2d(
  makelist(subst([k=d*0.25,x[0]=0.1],x(t)), d,1,7),
  [t,0,15],
  cons(legend, makelist(printf(false,"k = ~,2f", 0.25*d), d,1,7)),
  [gnuplot_preamble, "set key nobox spacing 1.1 right bottom"]
)$

若要排版规范的标题,可在TeXmacs中使用标题作为替代方案。

内容的提问来源于stack exchange,提问作者tilda-a-steiner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 01:31:03