Can't set strings as 'facet_col' in px.imshow()

I have this list of lists. These data should be represented as 3 different heatmaps.

data_array = [[[1.0, 0.13790931989924432, -0.0919779252979285, 0.12174212725627881],
  [0.13790931989924432, 1.0, -0.0005118143819841338, 0.03747498635619429],
  [-0.0919779252979285, -0.0005118143819841338, 1.0, 0.06857971615041956],
  [0.12174212725627881, 0.03747498635619429, 0.06857971615041956, 1.0]],
 [[1.0, 0.3103448275862069, 0.08571428571428572, 0.11764705882352941],
  [0.3103448275862069, 1.0, 0.16923076923076924, 0.10144927536231885],
  [0.08571428571428572, 0.16923076923076924, 1.0, 0.013333333333333334],
  [0.11764705882352941, 0.10144927536231885, 0.013333333333333334, 1.0]],
 [[1.0, 0.10930636482092237, -0.047154899617311986, 0.13299499622264638],
  [0.1093063648209226, 1.0, 0.014284516205891629, 0.0912683000085494],
  [-0.0471548996173124, 0.014284516205891629, 1.0, 0.20172045383255874],
  [0.13299499622264643, 0.09126830000854978, 0.20172045383255963, 1.0]]]

So I transformed the data in Xarray

data_xarray = xarray.DataArray(
        data=data_array,
        dims=['Metrics', 'Parameter', 'Parameter'],
        coords={'Metrics': [1,2,3],
                'Parameter': ['Size', 'Depth', 'Diet', 'Taxon']
                }
    )

Now I want to show the 3 heatmaps

fig = px.imshow(data_xarray, facet_col='Metrics', color_continuous_scale=multicolor_scale, zmax=1)


As you can see the heatmaps are correctly subdivided according to the β€˜Metrics’.
The problem is that I need to change β€˜Metrics’ to a list of strings like β€˜Metrics’: [β€˜a’, β€˜b’, β€˜c,’] instead of [1,2,3].
If I do that an error pops up
**TypeError** : %d format: a number is required, not numpy.str_
The error depends on px.imshow and not on xarray.Datarray. For some reason it only accepts integers.

Does anyone have a solution for this?

@Mirk0_98
To change the facet title in any plot type, not only those that contain heatmaps, like in your example, just print:

print(fig.layout.annotations)

and identify the annotation index for each one that contains the facet titles.
In your case update the annotation text as follows:

new_titles =["Metrics: a", "Metrics: b", "Metrics: c"]
for k in range(3):
    fig.layout.annotations[k].update(text = new_titles[k])
3 Likes

Note that if we change facet_col_wrap to 1, the order of annotation should be different. See this post.