簡體   English   中英

設置使用 Plotly 創建的 3D 表面 plot 的顏色條范圍

[英]Set the range for colorbar of a 3D surface plot created using Plotly

如何手動設置使用 Plotly 創建的 3D 表面 plot 的顏色條范圍?

可以使用cmincmax設置顏色條范圍,如下所示:

fig = go.Figure(go.Surface(x=x_values,y=y_values,z=z_values,colorscale="Plotly3",cmin=-5,cmax=5))

我正在分享一個基本的 class 我用來更改 Plotly 3D 散點圖的一些基本圖形屬性,例如帶有cmincmax的顏色條范圍。 我希望它對你有用。

class Scatter3D(object):
    def __init__(self,
                 x: np.ndarray,
                 y: np.ndarray,
                 z: np.ndarray, ):
        """
        :param x: np.ndarray
        :param y: np.ndarray
        :param z: np.ndarray

        Create a custom 3D scatter plot with plotly
        """
        self.x = x
        self.y = y
        self.z = z

    def plot(self,
             color: np.ndarray = None,
             title: str = '',
             xlabel: str = '',
             ylabel: str = '',
             legend_title: str = '',
             size: tuple = (800, 600),
             marker_size: int = 5,
             x_range=None,
             y_range=None,
             z_range=None,
             n_ticks: int = 10,
             margin=None,
             alpha: float = 0.8,
             show: bool = False,
             cmin: float = 0.,
             cmax: float = 1.,
             colorscale: str = 'Turbo'):

        """
        :param colorscale: color scale
        :param cmax: maximum value of the colorbar
        :param cmin: minimum value of the colorbar
        :param alpha: alpha
        :param margin: scene margins
        :param n_ticks: number of ticks on every axis
        :param x_range: x range
        :param z_range: z range
        :param y_range: y range
        :param marker_size: marker size
        :param color: nodal values for the color scale
        :param title: figure title
        :param xlabel: xlabel
        :param ylabel: ylabel
        :param legend_title: legend_title
        :param size: size of the figure
        :param show: show (or not) the figure
        :return fig: figure instance

        Create the 3d scatter plot
        """
        # color = color.reshape(-1, 1) if color is not None else color
        if margin is None:
            margin = dict(r=10, l=10, b=10, t=10)
        if z_range is None:
            z_range = [-1, 1]
        if y_range is None:
            y_range = [-1, 1]
        if x_range is None:
            x_range = [-1, 1]
        fig = go.Figure(data=[go.Scatter3d(x=self.x, y=self.y, z=self.z,
                                           mode='markers',
                                           marker=dict(size=marker_size,
                                                       color=color,
                                                       colorscale=colorscale,
                                                       opacity=alpha,
                                                       colorbar=dict(thickness=20),
                                                       cmin=cmin,
                                                       cmax=cmax,
                                                       # line=dict(width=0.5,
                                                       #          color='black')
                                                       ))],
                        layout=go.Layout(
                            width=size[0],
                            height=size[1],
                        ))

        fig.update_layout(
            title=title,
            xaxis_title=xlabel,
            yaxis_title=ylabel,
            legend_title=legend_title,
            scene=dict(
                xaxis=dict(nticks=n_ticks, range=x_range, ),
                yaxis=dict(nticks=n_ticks, range=y_range, ),
                zaxis=dict(nticks=n_ticks, range=z_range, ), ),

            margin=margin
        )

        fig.show() if show else None
        return fig

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM