Plotly + X-axis

24 Views Asked by At

Im having a problem in my plot_data function
im plotting subplots, im expecting to see values on the x-axis on the both plots , but for unknown reason im getting only on of them like this :

enter image description here

as i said im expecting see x values on both the plots not only one of them as im getting.

here is my code

def plot_data(data, filename):
    """
    Plotting the data
    param data: data to plot.
    param filename: file name to write the output in.
    return: None.
    """
    subplot_titles = []
    for segment in data:
        subplot_titles.append(
            f"{segment.get('problem_id')}      {segment.get('y_array_label')}({segment.get('x_array_label')})")
    subplot_titles_tuple = tuple(subplot_titles)
    fig = make_subplots(rows=len(data), cols=1, shared_xaxes=True, subplot_titles=subplot_titles_tuple)
    fig.update_layout(updatemenus = [
        dict(
            buttons=[
                dict(label="Linear",
                     method="relayout",
                     args=[{"yaxis.type": "linear"}]),
                dict(label="Log",
                     method="relayout",
                     args=[{"yaxis.type": "log"}]),
            ])])
    for i, segment in enumerate(data):
        trace = go.Scatter(x=segment['x'], y=segment['y'], mode='lines', name=f"Plot {i + 1}")
        print('im here',segment['x'] )
        fig.add_trace(trace,row=i+1,col=1)

        # Set subplot titles
        fig.update_layout(title=f"Plots for {filename}")

        # Set x-axis and y-axis titles according to segment data
        x_title = segment.get('x_array_label', 'RCM')  # Default to 'X-axis' if 'x_array_label' is not present
        y_title = segment.get('y_array_label', 'Y-axis')  # Default to 'Y-axis' if 'y_array_label' is not present
        fig.update_xaxes(title_text=x_title, row=i + 1, col=1)
        fig.update_yaxes(title_text=y_title, row=i + 1, col=1)

        # Add update menus to each subplot
        # change from Linear to Log
        fig.update_layout(
            updatemenus=[
                dict(
                    buttons=[
                        dict(label="Linear",
                             method="relayout",
                             args=[{"yaxis.type": "linear"}]),
                        dict(label="Log",
                             method="relayout",
                             args=[{"yaxis.type": "log"}]),
                    ],
                    direction="down",
                    showactive=True,
                    x=1,
                    xanchor="left",
                    y=0.9,
                    yanchor="top"
                )
            ]
        )


    # change xlim and ylim
    xlim_slider = widgets.FloatRangeSlider(
        value=[min(segment['x']), max(segment['x'])],  # Initial limits based on data
        min=min(segment['x']),
        max=max(segment['x']),
        step=0.1,
        description='xlim:',
        continuous_update=False
    )

    ylim_slider = widgets.FloatRangeSlider(
        value=[min(segment['y']), max(segment['y'])],  # Initial limits based on data
        min=min(segment['y']),
        max=max(segment['y']),
        step=0.1,
        description='ylim:',
        continuous_update=False
    )

    # Function to update xlim and ylim
    def update_plot(xlim, ylim):
        fig.update_xaxes(range=xlim)
        fig.update_yaxes(range=ylim)

    # Connect sliders to update function
    widgets.interactive(update_plot, xlim=xlim_slider, ylim=ylim_slider)

    # Show or save the plot
    plot_filename = f"{os.path.splitext(filename)[0]}_plots.html"
    plot_path = os.path.join(os.getcwd(), plot_filename)
    fig.write_html(plot_path)
    print(f"All plots saved in {plot_filename}")
0

There are 0 best solutions below