Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotly have trace without data in legend

Tags:

plotly

As a follow on to a previous question

Suppose I want to show a set of bars, where each bar could be one of the choices: A, B, or C, each which has a different color. I would like to show the bars, but also if none the bars fall into a particular category still have that category show up in the legend. Unfortunately the categories without colors seem to get dropped. Notice in this example how the category labeled 'C' which should be blue is dropped from the legend:

data = [
{
x: [1, 3, 4],
y: [20, 14, 23],
type: 'bar',
name: 'A',
marker: {color: '#00FFFF'}},
{
x: [2, 5, 6],
y: [8, 6, 2],
type: 'bar',
name: 'B',
marker: {color: '#FF00FF'}},
{
x: [],
y: [],
type: 'bar',
name: 'C'
marker: {color: '#FF0000'}}]

Category C is not shown

How would I be able to make sure C (or any color without data) always shows up?

like image 290
Jacqueline Nolis Avatar asked Oct 13 '15 21:10

Jacqueline Nolis


People also ask

How do you hide a trace in legend Plotly?

Users may show or hide traces by clicking or double-clicking on their associated legend item.

What are traces in Plotly?

A plotly. graph_objects. Image trace is a graph object in the figure's data list with any of the named arguments or attributes listed below. Display an image, i.e. data on a 2D regular raster. By default, when an image is displayed in a subplot, its y axis will be reversed (ie.

What does Add_trace do in Plotly?

Adding TracesNew traces can be added to a graph object figure using the add_trace() method. This method accepts a graph object trace (an instance of go. Scatter , go. Bar , etc.)

How do you show legends in Plotly?

Plotly legends are interactive. Click on the legend entries to hide and show traces. The legendgroup key groups legend entries so that clicking on one legend entry will hide or show all of the traces in that group.


1 Answers

Traces with empty data arrays are assumed to be not visible. This is equivalent to setting visible: false in the trace object.

You can trick plotly by entering null values in the data arrays:

data = [{
  x: [1, 3, 4],
  y: [20, 14, 23],
  type: 'bar',
  name: 'A',
  marker: {color: '#00FFFF'}
}, {
  x: [2, 5, 6],
  y: [8, 6, 2],
  type: 'bar',
  name: 'B',
  marker: {color: '#FF00FF'}
}, {
  x: [null],
  y: [null],
  type: 'bar',
  name: 'C'
  marker: {color: '#FF0000'}
}]

which gives

enter image description here

like image 167
etpinard Avatar answered Oct 11 '22 16:10

etpinard