Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly horizontal bar - display ALL y axis labels

Tags:

plotly

I have a graph that works fine most of the time, unless there are many bars contained in it and then plotly defaultly decides to show only any 2nd y axis label (I can hover on the bars with missing label to view the label but that's not the desired behaviour):

enter image description here

What can I do to enforce plotly to display all labels? and how do I control gapping between them? height should be dynamic...

like image 385
shaharsol Avatar asked Feb 12 '17 11:02

shaharsol


1 Answers

You could set dtick: 1 for your yaxis.

From the documentation:

dtick (number or categorical coordinate string)
Sets the step in-between ticks on this axis.

var data = [{
  "x": [2, 0, 3, 2, 6, 2, 0, 1, 0, 0, 1, 0, 1, 0, 1, 7, 1, 1, 1, 0, 0],
  "y": ["department name 1", "department name 2", "department name 3", "department name 4", "department name 5", "department name 6", "department name 7", "department name 8", "department name 9", "department name 10", "department name 11", "department name 12", "department name 13", "department name 14", "department name 15", "department name 16", "department name 17", "department name 18", "department name 19", "department name 20", "department name 21"],
  "name": "Done",
  "orientation": "h",
  "marker": {
    "color": "grey",
    "width": 1
  },
  "type": "bar"
}, {
  "x": [1, 0, 1, 5, 1, 1, 0, 7, 0, 1, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0],
  "y": ["department name 1", "department name 2", "department name 3", "department name 4", "department name 5", "department name 6", "department name 7", "department name 8", "department name 9", "department name 10", "department name 11", "department name 12", "department name 13", "department name 14", "department name 15", "department name 16", "department name 17", "department name 18", "department name 19", "department name 20", "department name 21"],
  "name": "Active",
  "orientation": "h",
  "marker": {
    "color": "green",
    "width": 1
  },
  "type": "bar"
}, {
  "x": [2, 7, 7, 1, 13, 2, 15, 8, 5, 2, 12, 17, 2, 1, 0, 4, 6, 8, 2, 2, 14],
  "y": ["department name 1", "department name 2", "department name 3", "department name 4", "department name 5", "department name 6", "department name 7", "department name 8", "department name 9", "department name 10", "department name 11", "department name 12", "department name 13", "department name 14", "department name 15", "department name 16", "department name 17", "department name 18", "department name 19", "department name 20", "department name 21"],
  "name": "Remaining",
  "orientation": "h",
  "marker": {
    "color": "yellow",
    "width": 1
  },
  "type": "bar"
}]

Plotly.newPlot('plot', data, {
  barmode: "stack",
  title: 'Department stats',
  xaxis: {
    title: 'Sales'
  },
  yaxis: {
    title: 'Departments',
    dtick: 1
  },
  margin: {
    l: 180
  }
})
<div id="plot"></div>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
like image 132
Maximilian Peters Avatar answered Sep 22 '22 18:09

Maximilian Peters