[英]How to use respective colorbar when using facet_col with plotly.express?
I have a data which has three variables whose magnitudes are different.我有一个数据,其中包含三个大小不同的变量。
I'm trying to apply animation_frame
and facet_col
to make them animate at the same time.我正在尝试应用animation_frame
和facet_col
使它们同时具有动画效果。
Here's the code:这是代码:
import plotly.express as px
import xarray as xr
# Load xarray from dataset included in the xarray tutorial
ds = xr.tutorial.open_dataset('eraint_uvz').sel(level=500)
# convert Dataset to DataArray for animation
data = ds.to_array().transpose('month', ...)
# fix the bug
# TypeError: %d format: a real number is required, not str
plot_data = data.assign_coords({'variable': range(len(data['variable']))})
fig = px.imshow(plot_data, animation_frame='month', facet_col='variable', color_continuous_scale='viridis')
# set variable back to string
# https://community.plotly.com/t/cant-set-strings-as-facet-col-in-px-imshow/60904
for k in range(len(data['variable'])):
fig.layout.annotations[k].update(text = data['variable'].values[k])
fig.show()
By default, they share the same colorbar like below.默认情况下,它们共享相同的颜色条,如下所示。 Is it possible to make three colorbars (even different cmaps) with manual zmin/zmax values?是否可以使用手动 zmin/zmax 值制作三个颜色条(甚至不同的 cmap)?
I found the similar question and modified it.我找到了类似的问题并修改了它。
It looks nice to me.我觉得不错。 Only the titles are in the wrong order.只有标题顺序错了。
fig = px.imshow(plot_data, animation_frame='month',
facet_col='variable', facet_col_wrap=1,
color_continuous_scale='viridis')
# set variable back to string
# https://community.plotly.com/t/cant-set-strings-as-facet-col-in-px-imshow/60904
for k in range(len(data['variable'])):
fig.layout.annotations[k].update(text = data['variable'].values[k])
# update traces to use different coloraxis
for i, t in enumerate(fig.data):
t.update(coloraxis=f"coloraxis{i+1}")
for fr in fig.frames:
# update each of the traces in each of the animation frames
for i, t in enumerate(fr.data):
t.update(coloraxis=f"coloraxis{i+1}")
# position / config all coloraxis
fig.update_layout(
coloraxis={"colorbar": {"x": 1, "len": 0.3, "y": 0.85}},
coloraxis2={
"colorbar": {
"x": 1,
"len": 0.3,
"y": 0.5,
},
"colorscale": 'Thermal',
},
coloraxis3={
"colorbar": {"x": 1, "len": 0.3, "y": 0.15},
"colorscale": 'Blues',
},
)
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.