Engee 文档
Notebook

准备图表以供出版

我们所有人都曾经面临过对GOST进行报告并通过标准检查的需要,即使是第十次。 请记住word模板的价值以及在Excel中制作图形的不便。 在这篇文章中,我想展示Engee如何帮助使图表适合报告。 有三种方法可以做到这一点!

图表要求

让我们的标准控制对时间表的设计有以下要求:

  1. 轴签名和图形-Times New Roman字体,第12大小
  2. X轴签名位于轴的右侧
  3. Y轴签名位于轴的上侧
  4. 图例-没有框架,在轴的右上角
  5. 网格是黑色的
  6. 线条是灰色的阴影
  7. 线厚-1.5

图形的Engee特性概述

情节。jl库提供了三种设置图形的方法:

  1. 在绘图期间指定属性,作为命令的参数 plot
  2. 使用主题
  3. 图表的"食谱"

此外,该库还提供了几个用于绘制图形的后端。 Engee默认使用plotlyjs后端。 但是,这个后端不支持某些属性,所以我们将使用经典的GR后端。

使用plot()命令

最简单的方法是在调用plot命令时设置属性。 让我们尝试构建我们的图表。 为此,我们将首先生成数据:

In [ ]:
x = 0:0.01:10;
ys = hcat(
    sin.(x),
    cos.(x),
);

如何制作灰度线? 您可以更改图形的调色板。:

In [ ]:
using Plots
gr()
plot(x,ys,palette=:grays)
Out[0]:
No description has been provided for this image

但是,我们看到一些线条丢失了-它们的颜色太亮了! 但是图形库可以创建自己的调色板。 让我们创建它考虑到我们不需要太轻的线条。 调色板中会有与我们绘制的行完全相同数量的颜色。 为了创建一个调色板,我写了这个辅助函数:

In [ ]:
function gen_gs_pallete(data)
    nseries = data isa AbstractVector ? 1 : size(data, 2)
    if nseries > 1
        GS_Pallete = [ RGB(t, t, t) for t in range(0.0, 0.75; length=nseries)]
    else 
        GS_Pallete = RGB(0.0,0.0,0.0);
    end
    GS_Pallete
end

gspalette = gen_gs_pallete(ys)
Out[0]:
No description has been provided for this image

让我们尝试应用它:

In [ ]:
plot(x,ys,palette=gspalette)
Out[0]:
No description has been provided for this image

好多了。 现在让我们改变字体和大小。 您可以通过设置titlefont,guidefont,tickfont,legendfont等属性来执行此操作。 Font对象用于控制字体。:

In [ ]:
GOST_font = font(12, "times");
titlefont  = GOST_font;
guidefont  = GOST_font;
tickfont   = GOST_font;
legendfont = GOST_font;

应用设置:

In [ ]:
plot(x,ys,palette=gspalette,titlefont=titlefont,guidefont=guidefont,tickfont=tickfont,legendfont=legendfont)
Out[0]:
No description has been provided for this image

现在,让我们设置图例视图、其位置、网格和轴标签的位置。:

In [ ]:
plot(x,ys,
    palette=gspalette,
    titlefont=titlefont,
    guidefont=guidefont,
    tickfont=tickfont,
    legendfont=legendfont, 
    xguidefonthalign = :right,
    yguidefontvalign = :top,
    legend=(0.94,0.95),
    foreground_color_legend = nothing, 
    background_color_legend = nothing,
    gridalpha = 1
    )
xlabel!("t,c")
ylabel!("信号")
Out[0]:
No description has been provided for this image

请注意图例的位置是如何表示的。 我将其设置为相对于图形的右上角的位置。 它几乎工作,但让我们改变轴边界。 为此,我们将使用此函数计算新维度:

In [ ]:
function lim_helper(x,y)
    xlim = (x[1],x[end] + x[end]/4)
    yh = ceil(maximum(y) + maximum(y)/4)
    ylim = (-yh,yh)
    xlim,ylim
end

(xlim,ylim) = lim_helper(x,ys)
Out[0]:
((0.0, 12.5), (-2.0, 2.0))

最后的时间表将是这样的:

In [ ]:
plot(x,ys,
    palette=gspalette,
    titlefont=titlefont,
    guidefont=guidefont,
    tickfont=tickfont,
    legendfont=legendfont, 
    xguidefonthalign = :right,
    yguidefontvalign = :top,
    legend=(0.94,0.95),
    foreground_color_legend = nothing, 
    background_color_legend = nothing,
    gridalpha = 1,
    xlim = xlim,
    ylim = ylim
    )
xlabel!("t,c")
ylabel!("信号")
Out[0]:
No description has been provided for this image

应用主题

上图,剧情命令变得过于臃肿。 Plots库允许您创建自己的图形设计主题,它允许您一次配置所有属性并将其分发到后续图形。 主题是使用PlotTheme命令创建的,必要的属性充当其参数。:

In [ ]:
gsTheme = PlotTheme(
    linewidth = 1.5,
    titlefont  = GOST_font,
    guidefont  = GOST_font,
    tickfont   = GOST_font,
    legendfont = GOST_font,
    xguidefonthalign = :right,
    yguidefontvalign = :top,
    legend=(0.94,0.95),
    foreground_color_legend = nothing, 
    background_color_legend = nothing,
    gridalpha = 1
    );

创建主题后,我们需要注册它。:

In [ ]:
PlotThemes.add_theme(:gstheme, gsTheme);

现在你可以使用这个主题来构建图表。 在下面的代码中,主题将全局应用于后续图形。:

In [ ]:
theme(:gstheme,palette=gen_gs_pallete(ys))
(xlim,ylim) = lim_helper(x,ys)
plot(x,ys,xlim = xlim,ylim = ylim)
xlabel!("t,c")
ylabel!("信号")
Out[0]:
No description has been provided for this image

如果我们想返回默认主题,那么我们需要运行命令:

In [ ]:
theme(:default)

图表和自定义图表的配方

Plots库不仅允许您创建主题,还允许您根据"配方"创建自定义图形类型。

配方是一种允许您定义数据可视化规则的机制。 它们在按此顺序绘制图形之前应用:

  1. 用户食谱(@userplot):使用用户界面创建用于绘制图形的新功能(例如, andrewsplot).

  2. Type Recipes:定义特定Julia类型的方式(例如, DistributionSimResult)必须转换为数组进行渲染。

  3. Plot Recipes:描述在创建行(例如,创建复杂布局)之前数据的可视化。

  4. 系列配方:最低级别;定义如何绘制特定系列(例如,描述直方图转换为一组列)。

这4点包含在所谓的图表管道中。 管道在[文档]中有更详细的描述(https://engee.com/helpcenter/stable/ru/julia/juliaplots/pipeline.html )。 此外,这些食谱不依赖于图库! 广泛使用的StatsPlots包。jl只是使用配方引擎。 对于我们的图表,我们将创建一个新的图表类型,GOSTPlot。:

In [ ]:
@userplot GOSTPlot

@recipe function f(p::GOSTPlot)
    x = p.args[1] 
    y = p.args[2]
    
    # number of series
    nseries = y isa AbstractVector ? 1 : size(y, 2)
    if nseries == 1
        linecolor := :black
    else
        palette := [RGB(t, t, t) for t in range(0.0, 0.75; length=nseries)]
    end

    linewidth := 1.5
    seriestype := :path
    fontfamily := "Times"
    fontsize = 12;
    titlefont  := font(fontsize, "Times")
    guidefont  := font(fontsize, "Times")
    tickfont   := font(fontsize, "Times")
    legendfont := font(fontsize, "Times")
    foreground_color := :black
    background_color := :white

    legend := (0.94,0.95)
    xlabel := "t,与"
    ylabel := "信号"
    xlim := (x[1],x[end] + x[end]/4)
    yh = ceil(maximum(y) + maximum(y)/4)
    ylim := (-yh,yh)
    xguidefonthalign := :right
    yguidefontvalign := :top
    grid := true
    gridalpha := 1.0
    foreground_color_legend := nothing 
    background_color_legend := nothing
    x, y
end

请注意,我们的图表现在自动创建调色板并自行分配轴签名。 由于图形的配方是一个宏,我们可以调用函数内计算属性值的任何代码。 您还应该注意这样一个事实,即我们正在返回x和y-它们是随后绘制图形所需的。

让我们试试我们的新时间表!

In [ ]:
gostplot(x,ys)
Out[0]:
No description has been provided for this image

结论

在本出版物中,我考虑了绘制符合监管控制流行要求的图表的三种选择。 考虑了三种配置图的方法,并回顾了图库的特点.