python - 抑制绘图子图中的 y 轴标签,注释未对齐

标签 python plotly

我有 2 个热图,我试图将它们连接在一起,一个包含每周数据,另一个包含 6W/YTD 信息。我把它们分开,这样它们的颜色就不会歪斜。
当它们放在子图中时,右侧的 yaxis 标签是左侧的第一行标签
enter image description here
我想删除那个 yaxis 标签,并尝试通过 fig.update_yaxes(title=''/None/False) ,还有 title_text论点,我也尝试通过 fig['layout']['scenes']['yaxis2']['title'] = ''/None/False 访问它.似乎很多资源(包括官方文档)都展示了如何更改单个数字,这可以通过我的代码进行演示

fig1['layout']['yaxis']['title']='This works with a single plot'
fig1.show()
不幸的是,当 fig2 时不会结转被添加到子图中。我不确定这个任务是如何发生的。我已经查看了 JSON 结构并没有看到任何分配,我还在他们的文档中挖掘了结构以查看是否有我可以覆盖或设置的内容。有人可以帮我弄清楚如何隐藏 fig2 yaxis标签?询问如何手动设置它可能更准确,但仍然如此。

*编辑*
我仔细查看了注释。数字注释是用这个位设置的
annotations=wow_annot+totals_annot
这些是基于
wow['data_labels'] = int_to_str(wow['data'])
totals['data_labels'] = int_to_str(totals['data'])
它们只是二维整数数组。检查每个图中“A”的注释,它们作为子图添加到的图是唯一包含“A”的图
(Pdb) [i for i in fig1.layout.annotations if i.text == 'A']
[]
(Pdb) [i for i in fig2.layout.annotations if i.text == 'A']
[]
(Pdb) [i for i in fig.layout.annotations if i.text == 'A']
[layout.Annotation({
    'font': {'size': 16},
    'showarrow': False,
    'text': 'A',
    'textangle': 90,
    'x': 0.98,
    'xanchor': 'left',
    'xref': 'paper',
    'y': 0.5,
    'yanchor': 'middle',
    'yref': 'paper'
})]
基于此,这可以很容易地通过像这样覆盖注释来追溯“解决”
new_annot = []
for i in fig.layout.annotations:
  if i.text == wow['y_labels']['labels'][0]:
    i.text = ''
  new_annot.append(i)

fig.update_layout(annotations=new_annot)
这有效,但感觉非常挑剔,我仍然想知道这应该如何用 plotly 完成。这种方法感觉可能会产生意想不到的效果,具体取决于应用程序。

仔细检查后,我也意识到底行的前两列没有注释,尽管它们的原始图中有注释
(Pdb) fig1.layout.annotations[:2]
(layout.Annotation({
    'font': {'color': 'black'}, 'showarrow': False, 'text': '0', 'x': 'W21', 'xref': 'x', 'y': 'A', 'yref': 'y'
}), layout.Annotation({
    'font': {'color': 'black'}, 'showarrow': False, 'text': '0', 'x': 'W22', 'xref': 'x', 'y': 'A', 'yref': 'y'
}))
我不确定我是否遗漏了某些东西,或者我的方法在设置注释时不正确
Checking `wow_annot+totals_annot` for `W21:A` annotation
layout.Annotation({
    'font': {'color': 'black'}, 'showarrow': False, 'text': '0', 'x': 'W21', 'xref': 'x', 'y': 'A', 'yref': 'y'
})
Checking the final `fig` for `W21:A` annotation
> d:\test_subplots.py(178)<module>()
-> fig.show()
(Pdb) len([i for i in totals_annot if i.y == 'A'])
2
(Pdb) len([i for i in wow_annot if i.y == 'A'])
6
(Pdb) len([i for i in totals_annot+wow_annot if i.y == 'A'])
8
(Pdb) len([i for i in fig.layout.annotations if i.y == 'A'])
6
我将保持原样,因为这篇文章变得很麻烦,但有一个问题:1)注释和 2)fig2 的 y 标题;我觉得他们必须是相关的,虽然我不知道这是怎么发生的

我在下面分离了我的代码,粘贴可用 here .
进口
# Success Criteria for this exercise is a subplot containing 2 Heatmaps side by side in the same Figure
from pdb import set_trace
from covidDash.plot_handlers.colorscales import bone_r # this is a custom derived from matplotlib
from plotly.subplots import make_subplots
import plotly.figure_factory as ff
import plotly.graph_objects as go
数据准备
# DATA PREP SECTION
# wow Heatmap data
wow = {'x_labels' : {'name' : 'Week',
                     'labels' : ['W21', 'W22', 'W23', 'W24', 'W25', 'W26']
                    },
       'y_labels' : {'name' : 'Site',
                     'labels' : ['A', 'B', 'C', 'D', 'E', 'F', 'G']
                    },
       'data'     : [
                      [0, 0, 1, 0, 0, 0],
                      [0, 0, 3, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 1],
                      [0, 0, 0, 0, 0, 0],
                      [0, 0, 1, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0]
                    ],
       'data_labels' : []
      }


# 6w and YTD Heatmap data
totals = {'x_labels' : {'name' : 'Week',
                        'labels' :['6W', 'YTD' ]
         },
          'y_labels' : wow['y_labels'],
          'data'     : [
                         [1, 16],
                         [4, 8],
                         [0, 1],
                         [1, 12],
                         [0, 5],
                         [1, 17],
                         [0, 1]
                      ],
         'data_labels' : []
         }


# this function is simply a base func for now
def int_to_str(arr2d):
  """base function for handling data to label conversion
  Args:

    arr2d (list): a 2D array with numeric values

  Returns:

    r_data (list): a 2D array with values converted into strings

  """
  r_data = []
  for row in arr2d:
    new_row = []
    [new_row.append(str(n)) for n in row]
    r_data.append(new_row)
  return r_data


wow['data_labels'] = int_to_str(wow['data'])
totals['data_labels'] = int_to_str(totals['data'])
绘图准备
# PLOT PREP SECTION 
# colorbar placement
wow_cbar= {
      'x' : 1.0,
      'title' : {
        'text' : 'Wow',
        'side' : 'right'
      }
    }


total_cbar = {
        'x' : 1.05,
        'title' : {
          'text' : 'Totals',
          'side' : 'right'
         }
       }

# xaxis conf
xaxis_conf={'rangeslider': {'visible': True},
       'type' : 'category',
       'side' : 'top'
      }
创建和连接数字
# week over week figure
fig1 = ff.create_annotated_heatmap(x=wow['x_labels']['labels'],
                                  y=wow['y_labels']['labels'],
                                  z=wow['data'], 
                                  colorscale=bone_r,
                                  font_colors=['black','white'],
                                  showscale=True,
                                  annotation_text=wow['data_labels'],
                                  colorbar=wow_cbar,
                                  name='Wow'
                                  )


# 6W and YTD
fig2 =ff.create_annotated_heatmap(x=totals['x_labels']['labels'],
                                  y=totals['y_labels']['labels'],
                                  z=totals['data'], 
                                  colorscale=bone_r,
                                  font_colors=['black','white'],
                                  showscale=True,
                                  annotation_text=totals['data_labels'],
                                  colorbar=total_cbar,
                                  name='Totals',
                                  )
# SUBPLOT PREP SECTION
# base subplot
fig = make_subplots(
    rows=1, cols=2,
    shared_yaxes=True,
    horizontal_spacing=0,
    row_titles=wow['y_labels']['labels'],
#    y_title=[wow['y_labels']['name'],
    x_title=wow['x_labels']['name'],
    column_widths=[0.75, 0.25]
)

# add data 
fig.add_trace(fig1.data[0], 1, 1)
fig.add_trace(fig2.data[0], 1, 2)

# apply annotations
wow_annot = list(fig1.layout.annotations)
totals_annot = list(fig2.layout.annotations)
for k in range(len(totals_annot)):
  totals_annot[k]['xref'] = 'x2'
  totals_annot[k]['yref'] = 'y2'

fig.update_layout(annotations=wow_annot+totals_annot,xaxis=xaxis_conf, xaxis2={'side':'top'})
set_trace()

fig.show()

最佳答案

fig.update_layout(annotations=foo) 出了点问题,我能够通过重新排列直接应用注释来解决这个问题

fig.update_layout(xaxis=xaxis_conf, xaxis2={'side':'top'})
fig['layout']['annotations'] = wow_annot+totals_annot
这使得所有字段都被正确注释并且不再分配 y_label 。
至于.update_layout的具体问题是什么,我不确定,但如果 yaxis_title=foo,这就是解决此问题的方法没有为你工作。

关于python - 抑制绘图子图中的 y 轴标签,注释未对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62665912/

相关文章:

python - 如何将几千列插入到sqlite3中?

python - 运行 pip install --allow external 时 Mysql python 连接器不可用

Plotly box p 值显着注释

python - 使用layout.updatemenus[] Dropdown动态更改X轴和Y轴标题

r - plotly::sublot 不显示两个标题

r - 如何在不禁用 geom_smooth 的情况下构建悬停文本?

python - 与 Django 位于同一页面上的多个 FusionChart

python - 如何使用 Python 流式传输来自多个 IP 摄像机的视频

python - 如果值列表不在数据框列中(每个值),则返回 true

r - 如何调整标题空间并在 r 中绘图