Multiple y scales to show all data

Hi,

I’m a noob python, pandas and plotly programmer kindly asking for some support :wave:

I’m looking to write a program the reads csv files with various data types and produce a html file for us to send to our consumers.
The data in the csv files can contain boolean values, integers and floats.

I have been able to make the bare-bone sw to import boolean, integers and floats, and produce a line-chart.

The issue I am having is that the “finished” html file displays the y-scale to encompass the min/max values of the entire dataset.

This is not what I want as I want all data to be displayed as each data was having the y scale as 0-100% when viewing all the data at the same time.
This way costumer would be able to see the changes on lines with small data while at the same time comparing this to data that has huge changes.

I´ve been playing around with rangemode ,
but that does not seem to do the trick.

I am also planning to introduce automated generation of traces, depending on amount of columns in data set as this makes the program flexible.
If you have any input on this too, it would be greatly appreciated.

This is the sample csv data that I have been using.

sample.csv

year;col_test_sn;col_test_mn;col_test_ln;col_test_bool_value;col_test_float
2000;1;356;986462;True;0,1
2001;3;567;624964;true;0,2
2002;5;454;529573;False;0,3
2003;4;878;329486;FALSE;5,4
2004;6;343;572057;true;0,5
2005;9;385;824585;TRUE;120,6
2006;7;476;175424;False;0,7
2007;2;754;986386;False;3,8
2008;5;927;476890;true;0,9
2009;5;973;268423;true;1984,01
2010;2;123;723423;true;1,9
import pandas as pd
import plotly.graph_objects as go

csv_del = ';'
csv_dec = ','

df = pd.read_csv(
			'sample.csv', 
			delimiter = csv_del,
			decimal = csv_dec,
			true_values = ['TRUE','True','true'],
			false_values = ['FALSE', 'False', 'false']

df.replace({False: 0, True: 1}, inplace=True)

fig.add_trace(go.Scatter(x=df['year'], y=df['col_test_sn'],
                    mode='lines',
                    name='Small_Numbers',))

fig.add_trace(go.Scatter(x=df['year'], y=df['col_test_mn'],
                    mode='lines',
                    name='Medium_numbers',))

fig.add_trace(go.Scatter(x=df['year'], y=df['col_test_ln'],
                    mode='lines', 
                    name='Large_numbers',))

fig.add_trace(go.Scatter(x=df['year'], y=df['col_test_bool_value'],
                    mode='lines', 
                    name='Bools',))

fig.add_trace(go.Scatter(x=df['year'], y=df['col_test_float'],
                    mode='lines', 
                    name='Floats',))

fig.update_layout(title='Testbed for plotly',
                   plot_bgcolor='rgb(230, 230,230)',
                   showlegend=True)

fig.update_yaxes(rangemode='normal')

######################     SHOW PLOT IN BROWSER
fig.show()

Hi @erlens04,

Welcome to the community! :slightly_smiling_face:

A few ideas based on what you said:

I want all data to be displayed as each data was having the y scale as 0-100% when viewing all the data at the same time

  • You can add multiple y-axis by providing explicitly the yaxis parameter to go.Scatter. Here’s a link to the documentation:
  • You could normalize the data for each trace individually to put in a [0, 1] interval. It can be done in pandas via (s - s.min()) / (s.max() - s.min()) where s is a series (and something equivalent for the entire dataframe). This could be a bad choice if the magnitude becomes important and it is crucial to inform the readers that the y-axis has normalized quantities.

  • You could have traces of different scales in different subplots sharing the same x-axis:

I often find option 3 more readable than option 1, but it depends from case to case.

Hope this helps!

1 Like