An example to explain
import numpy as np
import plotly.express as px
fig = px.imshow(
np.random.uniform(0, 1, size = (18, 28, 28)),
facet_col = 0, facet_col_wrap = 6,
binary_string = True,
width=3200, height=1000)
fig.update_layout(yaxis = dict(showticklabels=False),
xaxis = dict(showticklabels=False))
for ann in fig.layout.annotations:
ann.text = ''
fig.write_image('test.png')
The figure is supposed to show the evolution of three specimens over time. How can I get plotly to to set the axis labels like in the attached image? Also, how can I get plotly to remove the ugly vertical gap between rows?
Hi @bjourne ,
Welcome to the forum!
You can change the last six facet_col
text annotations( which is the top row) to the values that you want. For y title (N=β¦) annotations, you can add another annotations using fig.add_annotation
.
And for set manual vertical gap betweens rows, you need set your best distance by combining facel_row_spacing
and height
.
Here the code from your example.
import numpy as np
import plotly.express as px
xlabels = [0,20,40,60,80,100]
ylabels = [5,99,34]
col_wrap = 6
fig = px.imshow(
np.random.uniform(0, 1, size = (18, 28, 28)),
facet_col = 0, facet_col_wrap = col_wrap,
facet_col_spacing=0,
facet_row_spacing=0,
binary_string = True,
width=2000, height=1000)
fig.update_xaxes(showticklabels=False).update_yaxes(showticklabels=False)
# Change the facet_col only for 6 top rows
x_annotations = fig.layout.annotations
for idx,x_ann in enumerate(x_annotations):
if idx >= len(x_annotations) - (len(xlabels) ):
x_ann["text"] = '{}%'.format(xlabels[idx-len(x_annotations)])
x_ann["font"] = {"color": "red"}
else:
x_ann["text"] = ""
# Add another 3 annotations for every rows
for idx, ylabel in enumerate(ylabels):
y_ann = {
'font': {'color':'red'},
'showarrow': False,
'text': 'N={}'.format(ylabel),
'x': 0.0,
'xanchor': 'right',
'xref': 'paper',
'y': 0.15 + (idx*0.35) ,
'yanchor': 'middle',
'yref': 'paper'}
fig.add_annotation(y_ann)
# fig.show()
fig.write_image('test.png')
Hope this help.
1 Like
Hi @farispriadi and thanks for the kind welcoming. Your code works except for the spacing between the images which I canβt seem to get right. It is as if the labels are there, just not visible.
1 Like