首页 > 解决方案 > 绘图悬停标签'y统一'不显示相同'y'的值

问题描述

我正在开发的 plotly/dash 应用程序中的悬停标签有问题。出于某种原因,我在使用 hovermode='y unity' 时遇到了两个问题

1:由于某种原因,它并不总是显示相同深度的值,请参见下图 1。对于蓝色曲线,它选择 y=1,979.2,对于红色曲线,它选择 1,979.3,对于两条黑色曲线,它选择 1,979.0。我绘制的曲线都共享相同的 pandas 数据帧索引,因此它们都在相同的 y 点处具有值。

图1:不在同一深度显示曲线

2:放大,我显示所有曲线在 y=1979 附近确实是连续的。但图 2 显示了第二个问题。有时它不会显示所有曲线的值。在这种情况下,不显示蓝色的(可能是因为它离光标最远,这对于“y 统一”应该无关紧要吗?)

图2:不显示所有曲线的值

我是否可能误解了悬停模式“y统一”的期望行为?任何帮助,将不胜感激。谢谢!

def create_fig(curve_df, stored_yaxis_range_min, stored_yaxis_range_max, zeroline_x=False):
        #Plot all the curves in the dataframe using the column title
        #Make sure the column title is what you want the label to be (otherwise rename column)        
        
        curve_names = curve_df.columns
        color_list, curve_names_out = make_color_list(curve_names)
        
        #rename columns with new names
        for inname, outname in zip(curve_names, curve_names_out):
            curve_df = curve_df.rename(columns={inname: outname})
        
        fig = px.line(curve_df, x=curve_df.columns, y=curve_df.index, labels={'x':curve_names, 'y':curve_df.index.name}, color_discrete_sequence=color_list) 
        
        fig.update_xaxes(fixedrange=True)
        if not stored_yaxis_range_min or not stored_yaxis_range_max:        #If not set (could be a None, or could be False which is the value i give when loading a new .las)            
            fig.update_yaxes(autorange="reversed")                          #I don't think we can end up here anymore now that loading an .las would update max and min depth, which then triggers update_yaxis_range_store
        else: #min and max zoom level are already determined. Set them for the figure (this can trigger if toggling another property)
            fig['layout']['yaxis']['autorange'] = False
            fig['layout']['yaxis']['range'] = [stored_yaxis_range_max,stored_yaxis_range_min]  

        fig.update_xaxes(zeroline=zeroline_x, zerolinecolor = 'black')
                    
        #set other layout
        fig.update_layout(
            showlegend=False,
            plot_bgcolor="white",
            margin=dict(t=0,l=0,b=0,r=0),
            font_color = 'black',
            font_size  = 10,
            hovermode='y unified',
            hoverlabel=dict(font_size=10),
            xaxis_title="Value",
        )    
        
        fig_style = {'display': 'block', "height": 700, "width": "100%"}  #The display is  needed to get figure to appear?
        return fig, fig_style

标签: pythonplotplotlydata-visualizationplotly-dash

解决方案


推荐阅读