python - 抑制绘图子图中的y轴标签,注释未对齐
问题描述
我有 2 个要合并的热图,一个包含每周数据,另一个包含 6W/YTD 信息。我把它们分开,这样它们的颜色就不会歪斜。
当它们放在一个子图中时,右侧的 yaxis 标签是左侧的第一行标签
我想删除那个 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)
这行得通,但感觉很挑剔,我仍然想知道这应该如何通过情节来完成。这种方法感觉它可能会根据应用程序产生意想不到的影响。
经过仔细检查,我还意识到底行的前两列没有任何注释,尽管它们在原始图中有注释
(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)图 2 的 y 标题;我觉得他们必须是相关的,虽然我不知道这是怎么发生的
我在下面分离了我的代码,此处提供了粘贴。
进口
# 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
# 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
推荐阅读
- bioinformatics - 在 Nextflow 中使用 Mutect2 容器时减速
- javascript - 在 HTML 文本输入中验证美国邮政编码的 Javascript
- angular - Firestore valueChanges() 仅在使用 take(1) 时发出一次
- javascript - 如何使用 Knex.js 和 Bookshelf.js (ExpressJS/Postgress) 插入一对多
- powershell - 当输入是 system.array 时,无法访问选择字符串输出的成员
- python - python函数意外地改变数据
- python - 试图抓取具有多个数据表的网页,但只提取第一个表?
- javascript - OnClientClick 在我的 ASP.NET Web 解决方案中返回 true 时未传递给 OnClick 代码
- kendo-ui - Angular KendoUI 中 kendo-numerictextbox 中的文本颜色
- python - 将列表中的所有术语转换为标题大小写