How to break the connection between the first and last point in line

Here is my output

Here is my code for the above diagram

from curve_plotter import read_data, convert_data
import plotly.express as px
import pandas as pd

dat = read_data('test.csv')
dat2 = convert_data(dat)

def convert_to_array_of_arrays(input_array):
    result_array = [[item[1], item[0]] for item in input_array if len(item) >= 2]
    return result_array

result_array = convert_to_array_of_arrays(dat2)


def extract_xy(input_array):
    x = [item[1] for item in input_array]
    y = [item[0] for item in input_array]
    return x, y

x, y = extract_xy(result_array)

max_index = y.index(max(y))

# add a frame to the graph 
df = pd.DataFrame({'x': x,
                   'y': y})

fig = px.line(df, x='x', y='y', title='Your Title')

# Set the color to red
fig.update_traces(line_color='#F88416')

# Set the x-axis range to the min and max values of the x array
fig.update_layout(xaxis=dict(range=[max(x) + 100, min(x) - 100]))
fig.update_layout(yaxis=dict(range=[0, 100]))

fig.update_layout(plot_bgcolor='#1960e0')
fig.update_xaxes(gridcolor='#015BA0')
fig.update_yaxes(gridcolor='#015BA0')

# Add an arrow annotation for the highest point
fig.add_annotation(
    x=x[max_index],
    y=y[max_index],
    xref="x",
    yref="y",
    text=f'Highest Point <br> X: {x[max_index]:.2f} <br> Y: {y[max_index]:.2f}',
    showarrow=True,
    font=dict(
            family="Courier New, monospace",
            size=16,
            color="#ffffff"
        ),
     align="center",
    arrowhead=2,
    arrowsize=1,
    arrowwidth=2,
    arrowcolor="yellow",
    ax=50,
    ay= -90,
    bordercolor="#fff",
    borderwidth=2,
    borderpad=4,
    bgcolor="black",
    )
 
fig.update_layout(showlegend=False)

fig.show()

In the diagram, I’m looking for a way to prevent the connection between the first and last point. I wish to ask if it is possible for me to prevent that connection.

Hi @peter34 ,

This line that cross from last point to first point maybe caused by your last point that has smallest x value or your last point is pushed to first point.

Try to sort the array by x value before put it into plot. Maybe you could put it inside extract_xy function.

def extract_xy(input_array):
    input_array = sorted(input_array, key=lambda item: item[1]) # sort array by x value 
    x = [item[1] for item in input_array]
    y = [item[0] for item in input_array]
    return x, y

you can also refer to this similar post.

Hope this help.

1 Like

Thank you @farispriadi,
By sorting the x array, It fixes the issue as shown below
image

1 Like