在图例中有没有数据的情节 [英] plotly have trace without data in legend

查看:42
本文介绍了在图例中有没有数据的情节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何才能确保 C(或任何没有数据的颜色)始终显示?

解决方案

带有空数据数组的跟踪被假定为不可见.这相当于在跟踪对象中设置 visible: false.

您可以通过在数据数组中输入 null 值来欺骗情节:

data = [{x: [1, 3, 4],y: [20, 14, 23],类型:'酒吧',名称:'A',标记:{颜色:'#00FFFF'}}, {x: [2, 5, 6],y: [8, 6, 2],类型:'酒吧',名称:'B',标记:{颜色:'#FF00FF'}}, {x: [空],y:[空],类型:'酒吧',名称:'C'标记:{颜色:'#FF0000'}}]

给出

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?

解决方案

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

这篇关于在图例中有没有数据的情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆