简体   繁体   English

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

[英]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_framefacet_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.

相关问题 如何使用 plotly.express 在 pyhthon 中 plot 多个 facet_col - How to plot multiple facet_col in pyhthon using plotly.express 如何在 imshow 中更改标题(facet_col)(情节) - How to change titles (facet_col )in imshow (plotly) Python Plotly:使用 facet_col 时为所有子图设置单独的固定高度和宽度 - Python Plotly : Set individual fixed height and width for all subplots when using facet_col Plotly:如何设置 plotly.express 图表的 position 与 facet? - Plotly: How to set position of plotly.express chart with facet? 如何使用 plotly.express 创建用于映射的 ID - How to Create id for Mapping with plotly.express 如何在 Python(plotly.express,px)中使用 Plotly Expess 仅显示图例的子集? - How to show only a subset of a legend using Plotly Expess in Python (plotly.express, px)? Plotly:如何使用 plotly.express 饼图向 hover_data 添加元素? - Plotly: How to add elements to hover_data using plotly.express piechart? 如何使用 plotly.express 在顶部创建带有共享 x 轴的条形 plot? - How to create a bar plot with shared x-axis using plotly.express with sub-titles on top? 将固定比率轴与 plotly.express facetrow / facetcol 一起使用 - Use fiexed ratio axes with plotly.express facetrow / facetcol Python 如何 plot 使用 plotly.express 的一列频率饼图 - Python how to plot a frequency pie chart with one column using plotly.express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM