pandasplotlyplotly-pythonx-axis

X-axis tick values not matching data


I'm a new user of plotly. My plot's x-axis (label/tick) values do not match the data specified for the x axis.

import pandas as pd
import psycopg2 as odb
import plotly.express as px
from urllib.parse import quote_plus
from sqlalchemy import create_engine

connection = odb.connect(...)

q = """select SOMEATTR, OTHERATTR, SOMETYPE from SOMETABLE...."""

uri = f"postgresql+psycopg2://{quote_plus(user)}:{quote_plus(pw)}@{host}:{port}/{db}"
alchemyEngine = create_engine(uri)

df = pd.read_sql(q, connection)

fig = px.bar(df, x="SOMEATTR", y="OTHERATTR", color="SOMETYPE", orientation='h')
fig.update_xaxes(rangemode="normal")
fig.write_html('first_figure.html', auto_open=True)

SOMEATTR values range from 0 to 7.2808 in my example. The plot shows a label of 900 near the end of the bar chart.

enter image description here

enter image description here

I would expect axis labels/ticks ranging from 0 to 7 or 8, not 0-900.

Can someone help me get my x-axis values to represent the actual "x" data value range (0, 1, 2...8 etc.)? Is there a way to add a secondary axis to achieve this?


Solution

  • I was able to solve this with the help of the commenters here. One showed me I needed to use a "change in attribute" SOMEATTR (in my case, a length) instead of the value of SOMEATTR (mileage). Then I employed the x-axis scale suggestion posted in the comments.