Python 禁止打印子批次中的y轴标签,注释未对齐
我有两个热图,一个包含每周数据,另一个包含6W/YTD信息。我把它们分开,这样它们的颜色就不会歪斜 当它们放在一个子批次中时,右侧的yaxis标签是左侧的第一行标签 我想删除这个yaxis标签,并尝试通过Python 禁止打印子批次中的y轴标签,注释未对齐,python,plotly,Python,Plotly,我有两个热图,一个包含每周数据,另一个包含6W/YTD信息。我把它们分开,这样它们的颜色就不会歪斜 当它们放在一个子批次中时,右侧的yaxis标签是左侧的第一行标签 我想删除这个yaxis标签,并尝试通过fig.update\u yaxes(title=''/None/False),以及title\u text参数,我还尝试通过fig['layout']['scenes']['yaxis2']['title']=''/None/False来访问它。似乎有很多资源(包括官方文档)展示了如何更改单
fig.update\u yaxes(title=''/None/False)
,以及title\u text
参数,我还尝试通过fig['layout']['scenes']['yaxis2']['title']=''/None/False来访问它。似乎有很多资源(包括官方文档)展示了如何更改单个图形,我的代码通过
fig1['layout']['yaxis']['title']='This works with a single plot'
fig1.show()
不幸的是,当fig2
添加到子批次时,它不会继续。我不确定这项任务是怎么发生的。我已经查看了JSON结构,没有看到任何赋值,我也在他们的文档中挖掘了该结构,看看是否有我可以重写或设置的内容。有人能帮我找出如何隐藏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
数据准备
# 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()
尝试使用update\u layout方法的yaxis\u title参数:fig.update\u layout(annotations=wow\u annot+totals\u annot,xaxis=xaxis\u conf,xaxis2={'side':'top'},yaxis\u title=None)
以下是我的Jupyter笔记本的输出(忽略颜色贴图):
图更新布局(annotations=foo)有问题,我可以通过直接应用注释来解决这个问题,方法是重新排列
fig.update_layout(xaxis=xaxis_conf, xaxis2={'side':'top'})
fig['layout']['annotations'] = wow_annot+totals_annot
这使得所有字段都被正确注释,并且不再指定y_标签
至于.update\u layout
的具体问题是什么,我不确定,但如果yaxis\u title=foo
对您不起作用,您将如何解决这个问题。它仍然显示奇怪的“A”。它在我的电脑里显示得很好。您使用的是什么版本的Plotly?Plotly当前的版本是4.8.1,我会在有机会的时候再看一看,但如果您能弄明白,请随时联系我们
fig.update_layout(xaxis=xaxis_conf, xaxis2={'side':'top'})
fig['layout']['annotations'] = wow_annot+totals_annot