繁体   English   中英

将 facet_col 与 plotly.express 一起使用时如何使用各自的颜色条?

[英]How to use respective colorbar when using facet_col with plotly.express?

我有一个数据,其中包含三个大小不同的变量。

我正在尝试应用animation_framefacet_col使它们同时具有动画效果。

这是代码:

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()

默认情况下,它们共享相同的颜色条,如下所示。 是否可以使用手动 zmin/zmax 值制作三个颜色条(甚至不同的 cmap)?

图片

我找到了类似的问题并修改了它。

我觉得不错。 只有标题顺序错了。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM