如何以公制或英制单位显示绘图图,以及有关第 3 个 y 轴位置的小问题

How to display a plotly graph in metric or imperial units plus a minor question regarding to position 3rd y-axis

提问人:PeterPauken 提问时间:11/11/2023 最后编辑:JonasPeterPauken 更新时间:11/16/2023 访问量:34

问:

我想使用绘图创建类似于所附图片的图表。我当前的代码正在工作,但我还没有弄清楚两件事:

1:第三个 y 轴应位于右侧 y 轴内(里面有标签、刻度等)。无论出于何种原因,我都无法做到这一点。问题出在哪里?

2:压力、速度和距离三个图表提供的数据以公制单位为单位。如何使用 plotly 的功能创建一个单选按钮,允许用户将结果更改为英制单位(Psi 而不是 bar,fps 而不是 m/s,inch 而不是 mm)。当然,我的代码可以轻松管理转换。但是,在我看来,使用 plotly 的功能似乎更有意义。

请找到我当前的代码附件,包括结果和所需结果的图片。

谢谢 - 亚瑟 附言: 对不起,我的笔迹并不是地球上最漂亮的。 如果你们中的任何人有更好的主意在同一张图中显示三个单位,我很高兴看到......

现在

期望

    def output_plot(self, plot_data):
        # extract from the original data
        time_data = [0.001, 0.101, 0.2, 0.3, 0.4]
        velocity_data = [0, 0.1, 5.3, 19.3, 47.7]
        distance_data = [0, 0, 0, 1, 2]
        pressure_data = [26.1, 41.6, 66.1, 105.5, 168.2]
        cartridge_pmax = [3000]
          
        # Find index of maximum pressure and velocity for annotation
        max_pressure_index = pressure_data.index(max(pressure_data))
        max_velocity_index = velocity_data.index(max(velocity_data))

        # Create traces
        self.plot_output.data = [
            # Pressure
            go.Scatter(
                x=time_data,
                y=pressure_data,
                mode="lines",
                name="Pressure",
            ),
            # Velocity
            go.Scatter(
                x=time_data, y=velocity_data, mode="lines", name="Velocity", yaxis="y2"
            ),
            # Distance
            go.Scatter(
                x=time_data, y=distance_data, mode="lines", name="Distance", yaxis="y3"
            ),          
        ]


        # Create layout with synchronized axes
        self.plot_output.config['displayModeBar'] = False
        self.plot_output.layout = {
            "margin": dict(t=50, b=50),
            "legend": {
                "orientation": "h",
                "yanchor": "bottom",
                "xanchor": "center",
                "x": 0.5,
                "y": 1.05,
            },
            "xaxis": {
                "title": "Time [ms]",
                "rangemode": "tozero",
                "showgrid": False,
                "ticklen": 5,
            },
            "yaxis": {
                "title": "Pressure [bar]",
                "rangemode": "tozero",
                "showline": True,
                "showgrid": False,
                "ticklen": 5,
            },
            "yaxis2": {
                "title": "Velocity [m/s]",
                "rangemode": "tozero",
                "side": "right",
                "overlaying": "y",
                #"tickmode": "sync",
                "anchor": "x",
                "showline": True,
                "showgrid": False,
                "ticklen": 5,
            },
            "yaxis3": {
            "title": "Distance [mm]",
            "rangemode": "tozero",
            "side": "right",
            "overlaying": "y",
            "anchor": "free",
            "showline": True,
            "showgrid": False,
            "ticklen": 5,
            #"autoshift": True,
            "yaxis_title_standoff": 100,
            "ticks": "inside",
            "ticklabelposition": "inside",
            },      
            "annotations": [
                {
                    "text": "P<sub>CIP, max</sub>-20%",
                    "x": 0,
                    "y": cartridge_pmax * 0.8,
                    "showarrow": False,
                    "font": {
                        "color": "orange",
                    },
                    "xanchor": "left",
                    "yanchor": "top",
                },
                {
                    "text": f"P<sub>CIP, max</sub>= {cartridge_pmax:.0f} bar ({(cartridge_pmax*14.503773773):.0f} psi)",  # "P<sub>max</sub>",
                    "x": 0,
                    "y": cartridge_pmax,
                    "showarrow": False,
                    "font": {
                        "color": "red",
                    },
                    "xanchor": "left",
                    "yanchor": "top",
                },
                {
                    "text": f"P<sub>max</sub> = {max(pressure_data):.0f} bar",
                    "x": time_data[max_pressure_index],
                    "y": max(pressure_data),
                    "ax": 20,
                    "ay": 30,
                },
                {
                    "text": f"v<sub>max</sub> = {max(velocity_data):.0f} m/s",
                    "yref": "y2",
                    "x": time_data[max_velocity_index],
                    "y": max(velocity_data),
                    "ax": -20,
                    "ay": 30,
                },
                {
                    "text": "Watermark",
                    "textangle": -30,
                    "opacity": 0.2,
                     "font": {
                       "color": "black",
                       "size": "20"
                     },
                    "xref": "paper",
                    "yref": "paper",
                    "x": 0.5,
                    "y": 0.5,
                    "showarrow": False,
                },
            ],        
        }
python plotly 单选按钮

评论

0赞 r-beginners 11/13/2023
你能应用你正在绘制的数据来重现你的数据吗?如果你有数据,我会有更好的机会得到答案。
0赞 PeterPauken 11/13/2023
当然,我添加了前几个数据点。

答: 暂无答案