简体   繁体   English

Plotly.js不一致地向图表添加上边距,如何防止它

[英]Plotly.js adds top-margin to graphs inconsistently, how to prevent it

With Plotly.js I'm getting a top-margin added sometimes (not consistently), where the total height of the graph is 300px, but the graph itself is only 150px high. 使用Plotly.js我有时会添加一个上边距(不一致),其中图形的总高度为300px,但图形本身只有150px高。 The SVG container then is stretched and the actual graph is smaller. 然后拉伸SVG容器并且实际图形更小。 What can I do to prevent this white-space, and why does it only show up selectively? 我该怎么做才能防止这个空白区域,为什么它只是有选择地出现?

图表上方的保证金

Plotly Matlab syntax that results in 300px div instead of a 300px graph: Plotly Matlab语法导致300px div而不是300px图:

`% PLOT MEAN MOVEMENT
    data = {...
      struct(...
        'x', nScan, ...
        'y',fastmotion, ...
         'type', 'scatter')...
         };
    if max(fastmotion) < 0.3
        yminval = 0.3;
    else
        yminval = round(max(fastmotion) + 1);
    end
    layout = struct(...
    'yaxis', struct(...
    'title', 'Movement (mm)', ...
    'range', [0, yminval]));
    header{3} = 'Absolute Movement';
    layout.width = 800;
    layout.height = 300;
    p = plotlyfig; 
    p.data = data;
    p.layout = layout;
    p.PlotOptions.FileName = 'plot_5';
    html_file = plotlyoffline(p);
    html_file;`

You can check this answer on a different question, and here is the fiddle . 你可以在另一个问题上查看这个答案 ,这里是小提琴

The quick code you can use: 您可以使用的快速代码:

var layout = {
  margin: {
    l: 20,
    r: 20,
    b: 20,
    t: 20,
    pad: 5
  }, title: false
};

As mfedoten says on his answer: But be careful if you have tick labels, if you set margins to zero the labels will be cropped 正如mfedoten在答案中所说: But be careful if you have tick labels, if you set margins to zero the labels will be cropped

Probably line 68 in plotlyfig.m just set default margins. 可能只是在plotlyfig.m中的第68行设置了默认边距。

You can set the margins manually like that: 您可以手动设置边距:

p.layout.margin = struct('b', 15,'l', 30, 'r', 0, 't', 0);

You can find documentation on margins here . 您可以在此处找到有关边距的文档。

But be careful if you have tick labels, if you set margins to zero the labels will be cropped. 但是如果你有刻度标签要小心,如果你设置边距为零,标签将被裁剪。

So apparently line 68 in plotlyfig.m caused the issue: 显然,plotlyfig.m中的第68行导致了这个问题:

obj.PlotlyDefaults.MinTitleMargin = 80;

Even though I had no title, it sometimes added 80 px on top of the graph, maxing out at the specified graph height. 即使我没有标题,它有时会在图表顶部添加80 px,最大值在指定的图形高度。 Setting this value to 0 solves the problem. 将此值设置为0可解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM