I would like to plot subplots of Heatmaps. Is there any way that I can set a global width for all cells? By default, all subplots have the same size, and the width of each cell changes accordingly to how many columns each subplot has. I need it the other way around. I would like to set a fixed cell width that needs to be automatically determined using the entire width of the whole figure and the overall number of columns.
Hereโs an example:
import numpy as np
from numpy.random import default_rng
import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go
rng = default_rng(42)
# create example dataframe
n_variates = 2
variables = ['apple','pear', 'orange','mandarin','grapefruit']
sources = ['pome','pome','citrus','citrus','citrus']
variates = np.repeat(list(range(n_variates)),len(variables))
values = rng.random(n_variates * len(variables))
df = pd.DataFrame({'variable':np.tile(variables,n_variates),
'source':np.tile(sources,n_variates),
'variate':variates,
'value':values})
# prepare for plotting
z_list = []
source_names = []
for source_name,source in df.groupby('source'):
z = source.pivot(columns=['source','variable'],index='variate',values='value')
z_list.append(z)
source_names.append(source_name)
# plot
fig = make_subplots(rows=1,cols=len(z_list),
subplot_titles=source_names,
horizontal_spacing=0,
shared_yaxes=True)
for col,z in enumerate(z_list):
fig.add_trace(go.Heatmap(z=z,x=z.columns.get_level_values(1),coloraxis = "coloraxis"),1,col+1)
fig.show()
Which gives you: