最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - plotly.js reduce the margin between yAxis title and the axis numbers - Stack Overflow

programmeradmin3浏览0评论

i want to be able to reduce the margin between the yAxis title and the axis numbers, i cant find any property or function in plotly.js documentation mentioning it

an example of the code here

var trace1 = {
  x: [0, 1, 2, 3, 4, 5, 6, 7, 8],
  y: [0, 1, 2, 3, 4, 5, 6, 7, 8],
  name: 'Name of Trace 1',
  type: 'scatter'
};
var trace2 = {
  x: [0, 1, 2, 3, 4, 5, 6, 7, 8],
  y: [1, 0, 3, 2, 5, 4, 7, 6, 8],
  name: 'Name of Trace 2',
  type: 'scatter'
};
var data = [trace1, trace2];
var layout = {
  title: 'Plot Title',
  xaxis: {
    title: 'x Axis',
    titlefont: {
      family: 'Courier New, monospace',
      size: 18,
      color: '#7f7f7f'
    }
  },
  yaxis: {
    title: 'y Axis',
    titlefont: {
      family: 'Courier New, monospace',
      size: 18,
      color: '#7f7f7f'
    }
  }
};
Plotly.newPlot('myDiv', data, layout);

i want to be able to reduce the margin between the yAxis title and the axis numbers, i cant find any property or function in plotly.js documentation mentioning it

an example of the code here

var trace1 = {
  x: [0, 1, 2, 3, 4, 5, 6, 7, 8],
  y: [0, 1, 2, 3, 4, 5, 6, 7, 8],
  name: 'Name of Trace 1',
  type: 'scatter'
};
var trace2 = {
  x: [0, 1, 2, 3, 4, 5, 6, 7, 8],
  y: [1, 0, 3, 2, 5, 4, 7, 6, 8],
  name: 'Name of Trace 2',
  type: 'scatter'
};
var data = [trace1, trace2];
var layout = {
  title: 'Plot Title',
  xaxis: {
    title: 'x Axis',
    titlefont: {
      family: 'Courier New, monospace',
      size: 18,
      color: '#7f7f7f'
    }
  },
  yaxis: {
    title: 'y Axis',
    titlefont: {
      family: 'Courier New, monospace',
      size: 18,
      color: '#7f7f7f'
    }
  }
};
Plotly.newPlot('myDiv', data, layout);

also here is a codepen link codepen

Share Improve this question asked Mar 10, 2017 at 14:42 Ahmed FarahatAhmed Farahat 2,4432 gold badges16 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

If someone's searching for this solution , then it's available now , use the 'standoff' property .

Something like this :

 var layout = {
      margin: {t:0,r:0,b:0,l:20},
      xaxis: {
        automargin: true,
        tickangle: 90,
        title: {
          text: "Month",
          standoff: 20
        }},
      yaxis: {
        automargin: true,
        tickangle: 90,
        title: {
          text: "Temprature",
          standoff: 40
        }}}

As far as I know you cannot do it directly but you have at least two possibilities, see the discussion here.

Move the axis label directly

document.getElementsByClassName('ytitle')[0].y.baseVal[0].value *= 1.1

or add an annotation to layout and specify its position

annotations: [
  {
    x: 0.5,
    y: -0.15,
    xref: 'paper',
    yref: 'paper',
    text: 'I am not an axis label',
    showarrow: false,
  }
]

var trace1 = {
  x: [0, 1, 2, 3, 4, 5, 6, 7, 8],
  y: [0, 1, 2, 3, 4, 5, 6, 7, 8],
  name: 'Name of Trace 1',
  type: 'scatter'
};
var trace2 = {
  x: [0, 1, 2, 3, 4, 5, 6, 7, 8],
  y: [1, 0, 3, 2, 5, 4, 7, 6, 8],
  name: 'Name of Trace 2',
  type: 'scatter'
};
var data = [trace1, trace2];
var layout = {
  title: 'Plot Title',
  xaxis: {
    title: 'x Axis',
    titlefont: {
      family: 'Courier New, monospace',
      size: 18,
      color: '#7f7f7f'
    }
  },
  yaxis: {
    title: 'y Axis',
    titlefont: {
      family: 'Courier New, monospace',
      size: 18,
      color: '#7f7f7f'
    }
  },
  annotations: [
    {
      x: 0.5,
      y: -0.15,
      xref: 'paper',
      yref: 'paper',
      text: 'I am not an axis label',
      showarrow: false,
    }
  ]
};
Plotly.newPlot('myDiv', data, layout);
document.getElementsByClassName('ytitle')[0].y.baseVal[0].value *= 1.1
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="myDiv" style="width: 100%; height: 500px;"></div>

发布评论

评论列表(0)

  1. 暂无评论