Home > other >  how to set scope of data displayed with simple plotly bar graph
how to set scope of data displayed with simple plotly bar graph

Time:09-06

working my through my understanding of plotly/dash. immediately have a problem I can't find an answer too. I broke my code down into its simplest form to isolate the problem.

There are 10 float values for x, and 10 for y

x = [1548.36, 1548.35, 1548.32, 1548.31, 1548.3, 1548.26, 1548.25, 1548.17, 1548.12, 
1548.03]
y = [36.9467, 2.7585, 4.5658, 7.5905, 18.9993, 3.6085, 4.3028, 0.02, 29.7094, 0.2]
fig = px.bar(x=x,y=y)
fig.show()

this yields : enter image description here

Code:

import plotly.express as px

x = [1548.36, 1548.35, 1548.32, 1548.31, 1548.3, 1548.26, 1548.25, 1548.17, 1548.12,  1548.03]
y = [36.9467, 2.7585, 4.5658, 7.5905, 18.9993, 3.6085, 4.3028, 0.02, 29.7094, 0.2]
fig = px.bar(x, y=y, text=x)

CodePudding user response:

Answer instead of comment to be able to add a plot and sorted data.

When scaling the x values to a reasonable range the plot looks as expected:

import plotly.express as px

x = [1548.36, 1548.35, 1548.32, 1548.31, 1548.3, 1548.26, 1548.25, 1548.17, 1548.12, 
1548.03]
y = [36.9467, 2.7585, 4.5658, 7.5905, 18.9993, 3.6085, 4.3028, 0.02, 29.7094, 0.2]
x_scaled = [(i - 1548)*100 for i in x]
print(f"scaled x:\n{x_scaled}")

fig = px.bar(x=x_scaled,y=y)
fig.show()

x_scaled.sort()
print(f"scaled and sorted x: \n{x_scaled}")
scaled x:
[35.999999999989996, 34.999999999990905, 31.999999999993634, 30.999999999994543,
 29.999999999995453, 25.99999999999909, 25.0, 17.000000000007276, 11.999999999989086,
 2.9999999999972715]

enter image description here

scaled and sorted x: 
[2.9999999999972715, 11.999999999989086, 17.000000000007276, 25.0, 25.99999999999909,
 29.999999999995453, 30.999999999994543, 31.999999999993634, 34.999999999990905,
 35.999999999989996]

To sort x aside with y using pandas dataframe:

import pandas as pd

zipped = list(zip(x, y))

df = pd.DataFrame(zipped, columns=['X', 'Y'])
df_sorted = df.sort_values(by='X')
df_sorted

             X        Y
    9  1548.03   0.2000
    8  1548.12  29.7094
    7  1548.17   0.0200
    6  1548.25   4.3028
    5  1548.26   3.6085
    4  1548.30  18.9993
    3  1548.31   7.5905
    2  1548.32   4.5658
    1  1548.35   2.7585
    0  1548.36  36.9467


If that's still not what you expect and the answer from u1234x1234 doesn't fit as well you may want to describe more what you expect.

  • Related