Plotting multiple values with the same x-value

Hello guys, I am sry for the question I read multiple questions in this forum but didn’t understand how to fix my problem. My problem is that I got an csv file from which I collect data for my x-axis and y-axis. The Data looks like this:

141954,23.750
14200,23.750
14206,23.750
142012,23.750
142018,23.750
142023,23.750

The values iafter the ‘,’ are the x-values. So you see all have the same value. When I want to plot this with the following code:

from dash import Dash, html, dcc
from os.path import isfile, join
from dash.dependencies import Input, Output
import os
import plotly.graph_objs as go

mypath = '/Users/theo/Documents/Privat/Arbeiten/HiWi-Stelle/Modul-Vorbereitung/TCPServer/'

onlyfiles = [f for f in os.listdir(mypath) if isfile(join(mypath, f))]
onlyfiles.remove("TCPServer.py")
onlyfiles.remove(".DS_Store")

formatted_files = []

for file in onlyfiles:
    year = file[0:4]
    month = file[4:6]
    day = file[6:8]
    formatted_files.append(year + "." + month + "." + day)

app = Dash(__name__)

app.layout = html.Div([
    html.Div(children='Temperature Plot'),
    dcc.Dropdown(id='file-dropdown', options=formatted_files, placeholder="Select a file"),
    dcc.Graph(id='graph')
])

@app.callback(
    Output('graph', 'figure', allow_duplicate=True),
    Input('file-dropdown', 'value'),
    prevent_initial_call=True
)
def update_graph(selected_file):
    row1_list = []
    row2_list = []
    if selected_file:
        file = str(selected_file).replace(".", "") + ".csv"
        file_path = os.path.join(mypath, file)

        with open(file_path, 'r') as f:
            for line in f:
                if line != "\n":
                    split = line.split(",")
                    row1_list.append(split[0])
                    row2_list.append(split[1])

        col1_cleaned = [element.strip() for element in row1_list]
        col2_cleaned = [element.strip() for element in row2_list]

        integer_col1 = [int(x) for x in col1_cleaned]
        float_col2 = [float(x) for x in col2_cleaned]

        float_col2 = [y + i * 0.01 for i, y in enumerate(float_col2)]

        fig = {
            'data': [go.Scatter(
                    x=integer_col1,
                    y=float_col2,
                    mode='markers',  # Modus auf 'markers' setzen, um ein Punkt-Diagramm zu erstellen
                    marker=dict(size=10)  # Größe der Punkte festlegen
            )
            ],
            'layout': {'title': 'Graph of ' + selected_file}
        }

        return fig
    else:

        return {}

if __name__ == '__main__':
    app.run(debug=True)

Question
then all works finde except that no graph is shown. So dash got a problem when some values have the same x-value? Why this?

My Solution
an ugly solutionn from my side was to edit the values like a little bit with:

float_col2 = [y + i * 0.01 for i, y in enumerate(float_col2)]

Hey @zahntheo welcome to the forums.

The issue you encounter is not related to plotly. Take this example using your data. Empty figures are usually related to wrong data input, for example single values instead of a list as x- and y- arguments.

import plotly.graph_objects as go

data = [
    (141954,23.750),
    (14200,23.750),
    (14206,23.750),
    (142012,23.750),
    (142018,23.750),
    (142023,23.750)
]

x = [tup[1] for tup in data]
y = [tup[0] for tup in data]

# x = [23.75 for _ in range(10)]
# y = [*range(10)]

fig = go.Figure(go.Scatter(x=x, y=y, mode='markers'))
fig.show()

The figure renders just fine. There is a marker for each data ponit, due to the y- values you have to zoom in quite a bit to see them all. If you comment in the generic values for x and y you’ll see all markers.

Your data:

Generic data:

1 Like

First of all I am sry for posting wrong I am new on this forum. And thanks to you for your time and answer. Yeah you right, the one data point is messing with me hahaha. I didn’t saw the others because of the big difference between them.

1 Like

Hey @zahntheo

There is no reason for being sorry. We’re happy you found your way to the forums and you have been able to solve your problem.

When I said that your issue is not related to plotly I meant that you can plot various data points with the same x coordinates and the the problem lies somewhere else.