Plotly:曲面颜色如何使最小颜色值透明?

2024-04-20 02:40:11 发布

您现在位置:Python中文网/ 问答频道 /正文

这是Plotly Volume from的示例代码

https://plotly.com/python/visualizing-mri-volume-slices/

因为一个框架包含很多深色,所以看起来不太好看

我想知道的是,如何使颜色的最小值透明

使每个框架的黑色透明

或者有什么可以替代的方法来实现可视化

enter image description here

# Import data
import time
import numpy as np
from skimage import io

vol = io.imread("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/attention-mri.tif")
volume = vol.T
r, c = volume[0].shape

# Define frames
import plotly.graph_objects as go
nb_frames = 68

fig = go.Figure(frames=[go.Frame(data=go.Surface(
   z=(6.7 - k * 0.1) * np.ones((r, c)),
surfacecolor=np.flipud(volume[67 - k]),
cmin=0, cmax=200
),
name=str(k) # you need to name the frame for the animation to behave properly
)
for k in range(nb_frames)])

# Add data to be displayed before animation starts
fig.add_trace(go.Surface(
z=6.7 * np.ones((r, c)),
surfacecolor=np.flipud(volume[67]),
colorscale='Gray',
cmin=0, cmax=200,
colorbar=dict(thickness=20, ticklen=4)
))


def frame_args(duration):
    return {
        "frame": {"duration": duration},
        "mode": "immediate",
        "fromcurrent": True,
        "transition": {"duration": duration, "easing": "linear"},
    }

sliders = [
        {
            "pad": {"b": 10, "t": 60},
            "len": 0.9,
            "x": 0.1,
            "y": 0,
            "steps": [
                {
                    "args": [[f.name], frame_args(0)],
                    "label": str(k),
                    "method": "animate",
                }
                for k, f in enumerate(fig.frames)
            ],
        }
    ]

# Layout
fig.update_layout(
     title='Slices in volumetric data',
     width=600,
     height=600,
     scene=dict(
                zaxis=dict(range=[-0.1, 6.8], autorange=False),
                aspectratio=dict(x=1, y=1, z=1),
                ),
     updatemenus = [
        {
            "buttons": [
                {
                    "args": [None, frame_args(50)],
                    "label": "▶", # play symbol
                    "method": "animate",
                },
                {
                    "args": [[None], frame_args(0)],
                    "label": "◼", # pause symbol
                    "method": "animate",
                },
            ],
            "direction": "left",
            "pad": {"r": 10, "t": 70},
            "type": "buttons",
            "x": 0.1,
            "y": 0,
        }
     ],
     sliders=sliders
)

fig.show()

Tags: tonameimportcomgofordataframes
1条回答
网友
1楼 · 发布于 2024-04-20 02:40:11

回答:

曲面图似乎不接受像'rgba(255,255,255,0.8'这样的透明颜色。因此,您最好的选择似乎是将最低值设置为绘图背景色的值。结果如下:

图1:

enter image description here

片段1:

# retrieve all colours from the fig.data[0].colorscale tuple
# and store them in a list
scale_lst = [[*row] for row in fig.data[0].colorscale]

# edit the color for the lowest value
scale_lst[0][1] = '#E5ECF6'

# turn edited list back into a tuple
scale_tup = tuple(tuple(i) for i in scale_lst)

# replace the original tuple
fig.data[0].colorscale = scale_tup
fig.show()

相关问题 更多 >