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'}}]
How would I be able to make sure C (or any color without data) always shows up?
Users may show or hide traces by clicking or double-clicking on their associated legend item.
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.
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.)
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With