简体   繁体   English

设置使用 Plotly 创建的 3D 表面 plot 的颜色条范围

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

How to manually set the range of the colorbar of 3D surface plot created using Plotly?如何手动设置使用 Plotly 创建的 3D 表面 plot 的颜色条范围?

The color bar range can be set using cmin and cmax as follows:可以使用cmincmax设置颜色条范围,如下所示:

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

I'm sharing a basic class I made to change some basic graphics properties of Plotly 3D scatter plots, like the colorbar range with cmin and cmax .我正在分享一个基本的 class 我用来更改 Plotly 3D 散点图的一些基本图形属性,例如带有cmincmax的颜色条范围。 I hope it can be useful to you.我希望它对你有用。

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