简体   繁体   English

Plotly.js中单个图的多个X轴

[英]Multiple X axis for a single plot in Plotly.js

I have a single trace, which I want to show two units of measurement for the X axis. 我只有一条轨迹,我想显示X轴的两个度量单位。

Imagine my plot has "Inches" as the normal X axis. 想象我的绘图以法向X轴为“英寸”。 I also want to show "Centimeter" at the same time as another X axis right underneath the first X axis. 我也想在第一个X轴的正下方同时显示“厘米”和另一个X轴。

Inches and Centimeter have a 1:1 relationship, so there shouldn't be any funny business related to warping the trace. 英寸和厘米之间存在1:1的关系,因此不应存在任何与弯曲迹线有关的有趣事情。

I tried something like this for my layout: 我尝试过这样的布局:

var layout = {
    title: '',
    xaxis: {
        autorange: true,
        rangeslider: {},
        type: 'linear',
        title: 'Inches'
    },
    xaxis2: {
        autorange: true,
        rangeslider: {},
        type: 'linear',
        title: 'Centimeter',
        overlaying: 'x'
    },
    yaxis: {
        autorange: true,
        type: 'linear',
        title: ''
    },
};

But this doesn't work, and for obvious reasons since I am not even including details about the Centimeter conversion. 但这是行不通的,并且出于明显的原因,因为我什至没有提供有关厘米转换的详细信息。

Do you all know how to achieve a view like this? 你们都知道如何实现这样的观点吗? Would I need to make a second trace with matching Y data? 我是否需要使用匹配的Y数据进行第二次跟踪?

Not sure that's really you want. 不确定这真的是您想要的。 But it's can help you to play with this. 但这可以帮助您解决这个问题。

Please visit the: example 请访问: 示例

var y = ['dot','pen','pencil','scale','stick'];
var x = [0,5,9,12,16];//inc
var tinc = []; //inc
var tcm = []; //cm
var incToCm ='';
for (var i = 0; i < x.length; i++) 
{
   incToCm = (x[i] * 2.54)+'cm';
   tcm.push(incToCm);
   tinc.push(x[i]+'inc');
}

var trace1 = {
  x:x,
  y:y,
  type:'bar',
  xaxis:'x',
  yaxis:'y'
}
var trace2 = {
  x:trace1.x,
  y:trace1.y,
  type:trace1.type,
  xaxis:'x2',
  yaxis:'y'
}
var data =[trace1,trace2];
var layout={
  xaxis:{
    tickvals:x,
    ticktext:tinc,
    ticksuffix:'inc',
    tickangle:45,
  }, 
  xaxis2:{
    tickvals:x,
    ticktext:tcm,
    ticklen:18,
    tickangle:45,
    tickwidth:0,
    tickcolor:'#eee',
  },
  showlegend:false
}
Plotly.plot('plotElement', data,layout);

Sample output 样品输出

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

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