Y-axis unordered on CSV import (Plotly Express)

Hello everyone. I’m an absolute beginner in coding, python, plotly … you name it.
As a personal peak performance I managed to install anaconda, install Plotly and pandas and copy some Plotly Express examples into a Jupyter Notebook in an attemp to plot a very simple 2 dimensional graph with several reverberation times over frequency.

A graph in Excel/Calc looks like this:

Simple enough. However, I am not succesful in replicating the graph in Plotly:

import pandas as pd
import plotly.express as px

df = pd.read_csv('C:\\Users\\cer\\plotly_tests\\20221022\\RT_GN6021_VGL.csv', sep=';')
fig = px.line(df, x = 'F', y = ['A_T20_T', 'B_T20_T', 'A_T30_T', 'B_T30_T', 'A_T20_O', 'B_T20_O', 'A_T30_O', 'B_T30_O'], log_x=True)

fig.show()

gives me:

Problem: The y-axis is not in order. If I check the CSV data with df.head() the data looks ok.
This is the content of the CSV file:

F;A_T20_T;B_T20_T;A_T30_T;B_T30_T;A_T20_O;B_T20_O;A_T30_O;B_T30_O
125;1,4;0,8;1,5;0,9;1,3;0,8;1,3;0,9
250;1,2;0,9;1,2;0,9;1,1;0,9;1,1;0,9
500;1,3;1;1,3;1;1,3;1;1,3;1
1000;1,3;0,9;1,3;0,9;1,3;0,9;1,3;0,9
2000;1,2;0,8;1,2;0,8;1,2;0,9;1,2;0,9
4000;1,1;0,8;1,1;0,8;1,1;0,8;1,1;0,8

I figured out that there is a problem with the CSV import. If I manually type in values into arrays the y-axis looks fine:

import pandas as pd
import plotly.express as px

x = [125, 250,500,1000,2000,4000]
y = [1.4, 1.2, 1.3, 1.3, 1.2, 1.1]
y1= [1.5, 1.2, 1.3, 1.3, 1.2, 1.1]


fig = px.line(x=x, y=[y,y1], log_x=True)

fig.show()

It would be important for me to use CSV (or other table) files. Can anyone explain to me what’s happening here with the data? Is there a problem with my data layout, does plotly need something like an extra row to define the y-axis?

Also: I have the feeling that Jupyter Notebook might not be the best tool for me. It was what the first tutorial was advising me to use. I assume that I’m looking for an IDE: Any other recommendations in this regard?

Many thanks for you time
Chris

HI @CEr , welcome to the forums.

EDIT:

The problem with your CSV file is, that the decimal point is a comma β€œ,” instead of a point β€œ.” As of now, the values are interpreted as objects instead of float. You can check this using df.info() in your jupyter Notebook. You have two options:

  • You could use the decimal parameter when importing your *.csv without having to change the *.csv itself: pd.read_csv('C:\\Users\\cer\\plotly_tests\\20221022\\RT_GN6021_VGL.csv', sep=';', decimal=',')

  • Second option would be to replace all commas with a point in your *.csv

As for IDE or not: I really like Jupyter Notebooks for trying stuff, but I prefer an IDE sucha as Pycharm for coding.

2 Likes

Hello AIMPED,

thank you very much for your clarification! I just found out that I needed to define the seperator on import and assumed that would magically resolve the decimal :slight_smile:

Could you enlighten me on what would be different compared to Jupyter Notebooks when using an IDE: I assume that I can try stuff there as well? Excuse my ignorance, just did some research, but the comparisons I found did not answer that clearly. I assume there are fudamentally different concepts behind each but for me it looks like β€œjust cars”…

Hello @CEr,

They are essentially avenues to get to the same outcome. However, PyCharm offers tools to help with writing the code. Different hints, tips and tricks on start up. As well as suggestions and shortcuts to perform different tasks.

1 Like