0

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?

5
  • 1
    Looks like a stacked bar chart. What is the expected result, if you don't want the sum of multiple SOMEATTR corresponding to a particular instance of OTHERATTR? Commented Apr 18, 2024 at 17:54
  • @BigBen, I suppose I don't want the x-axis to represent a 'running total' of the SOMEATTR values, but rather the x-axis labels/ticks should better represent the SOMEATTR values i.e. the 900 should be something smaller that 7.2808, more like 7. Perhaps I need a different type of chart besides 'stacked'. The SOMEATTR data is 'miles', maybe one that better represents linear elements? Commented Apr 18, 2024 at 18:09
  • 1
    I'm not sure what the desired result is. Maybe you can draw it manually to figure out? Commented Apr 18, 2024 at 18:14
  • @BigBen, Added another picture to the question. Commented Apr 18, 2024 at 18:57
  • 1
    To set an arbitrary scale, set a list of interval values and a list of strings for them. Try the following. fig.update_xaxes(tickvals=[x for x in range(0,1000,100)], ticktext=[str(x) for x in range(0,10,1)]) Commented Apr 19, 2024 at 0:35

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.