删除差距,避免重叠的Xaxis和Yaxis ticklabels

发布于 2025-01-25 03:48:59 字数 2493 浏览 3 评论 0原文

我正在开发一个可视化图表:

但如您所见: 1-在图表的开始和结尾处有差距 2- tick标签是重叠的,

我尝试了很多修复它,但是我找不到参考,我对我应该尝试的内容不了解更多...有人知道如何正确修复此问题吗?

以下是可重复的代码,任何帮助都将不胜感激:

import pandas as pd
import plotly.express as px

data_dict = [{'variable': 'opt1', 'time_seconds': 62.42, 'values': 1.2506616294386024},
        {'variable': 'opt1', 'time_seconds': 368.64, 'values': 1.026396270065788},
        {'variable': 'opt1', 'time_seconds': 672.04, 'values': 0.9193268790432114},
        {'variable': 'opt1', 'time_seconds': 967.76, 'values': 1.0040146519632747},
        {'variable': 'opt1', 'time_seconds': 1319.24, 'values': 0.9758039569410012},
        {'variable': 'opt1', 'time_seconds': 1621.84, 'values': 0.9608018775714326},
        {'variable': 'opt2', 'time_seconds': 62.42, 'values': 53.669690262026634},
        {'variable': 'opt2', 'time_seconds': 368.64, 'values': 67.29353920559024},
        {'variable': 'opt2', 'time_seconds': 672.04, 'values': 82.30782533848364},
        {'variable': 'opt2', 'time_seconds': 1017.26, 'values': 64.92250125677477},
        {'variable': 'opt2', 'time_seconds': 1319.24, 'values': 61.70492445574225},
        {'variable': 'opt2', 'time_seconds': 1621.84, 'values': 66.73124984237081},
        {'variable': 'opt3', 'time_seconds': 62.34, 'values': 67.07091129789107},
        {'variable': 'opt3', 'time_seconds': 364.74, 'values': 60.39192699523444},
        {'variable': 'opt3', 'time_seconds': 666.68, 'values': 57.13104540996532},
        {'variable': 'opt3', 'time_seconds': 967.76, 'values': 50.293945860615096},
        {'variable': 'opt3', 'time_seconds': 1317.33, 'values': 73.49109300734065},
        {'variable': 'opt3', 'time_seconds': 1619.03, 'values': 80.53859104682748}]

table = pd.DataFrame.from_dict(data_dict)

px.scatter(
    table,
    x=pd.to_datetime(table['time_seconds'],unit='s'),
    y="values",
    color="variable",
    hover_data=["time_seconds"],
    color_discrete_map={
        "opt1": "#008aff",
        "opt2": "#8c2eff",
        "opt3": "#56cb32",
    },
).update_traces(mode="lines+markers").for_each_trace(
    lambda t: t.update(yaxis="y2") if t.name == "opt1" else t
).update_layout(
    yaxis2={"overlaying": "y", "side": "right", "dtick":1},
    xaxis_tickformat="%H:%M:%S",
    showlegend=False,
    title_text=None,
)

问候,
伦纳多

I'm developing a visualization chart:
enter image description here

But as you can see:
1 - It has a gap at the start and end of the chart
2 - The tick labels are overlapping

I tried a lot to fix it but I can't find references and I don't know more about what I should try... Does someone know how to fix this properly?

Below is a reproducible code, any help will be really appreciated:

import pandas as pd
import plotly.express as px

data_dict = [{'variable': 'opt1', 'time_seconds': 62.42, 'values': 1.2506616294386024},
        {'variable': 'opt1', 'time_seconds': 368.64, 'values': 1.026396270065788},
        {'variable': 'opt1', 'time_seconds': 672.04, 'values': 0.9193268790432114},
        {'variable': 'opt1', 'time_seconds': 967.76, 'values': 1.0040146519632747},
        {'variable': 'opt1', 'time_seconds': 1319.24, 'values': 0.9758039569410012},
        {'variable': 'opt1', 'time_seconds': 1621.84, 'values': 0.9608018775714326},
        {'variable': 'opt2', 'time_seconds': 62.42, 'values': 53.669690262026634},
        {'variable': 'opt2', 'time_seconds': 368.64, 'values': 67.29353920559024},
        {'variable': 'opt2', 'time_seconds': 672.04, 'values': 82.30782533848364},
        {'variable': 'opt2', 'time_seconds': 1017.26, 'values': 64.92250125677477},
        {'variable': 'opt2', 'time_seconds': 1319.24, 'values': 61.70492445574225},
        {'variable': 'opt2', 'time_seconds': 1621.84, 'values': 66.73124984237081},
        {'variable': 'opt3', 'time_seconds': 62.34, 'values': 67.07091129789107},
        {'variable': 'opt3', 'time_seconds': 364.74, 'values': 60.39192699523444},
        {'variable': 'opt3', 'time_seconds': 666.68, 'values': 57.13104540996532},
        {'variable': 'opt3', 'time_seconds': 967.76, 'values': 50.293945860615096},
        {'variable': 'opt3', 'time_seconds': 1317.33, 'values': 73.49109300734065},
        {'variable': 'opt3', 'time_seconds': 1619.03, 'values': 80.53859104682748}]

table = pd.DataFrame.from_dict(data_dict)

px.scatter(
    table,
    x=pd.to_datetime(table['time_seconds'],unit='s'),
    y="values",
    color="variable",
    hover_data=["time_seconds"],
    color_discrete_map={
        "opt1": "#008aff",
        "opt2": "#8c2eff",
        "opt3": "#56cb32",
    },
).update_traces(mode="lines+markers").for_each_trace(
    lambda t: t.update(yaxis="y2") if t.name == "opt1" else t
).update_layout(
    yaxis2={"overlaying": "y", "side": "right", "dtick":1},
    xaxis_tickformat="%H:%M:%S",
    showlegend=False,
    title_text=None,
)

Regards,
Leonardo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间不值得 2025-02-01 03:48:59

明确设置 range_x

    range_x=[
        pd.to_datetime(table["time_seconds"], unit="s").min(),
        pd.to_datetime(table["time_seconds"], unit="s").max(),
    ],

完整代码

import pandas as pd
import plotly.express as px

data_dict = [
    {"variable": "opt1", "time_seconds": 62.42, "values": 1.2506616294386024},
    {"variable": "opt1", "time_seconds": 368.64, "values": 1.026396270065788},
    {"variable": "opt1", "time_seconds": 672.04, "values": 0.9193268790432114},
    {"variable": "opt1", "time_seconds": 967.76, "values": 1.0040146519632747},
    {"variable": "opt1", "time_seconds": 1319.24, "values": 0.9758039569410012},
    {"variable": "opt1", "time_seconds": 1621.84, "values": 0.9608018775714326},
    {"variable": "opt2", "time_seconds": 62.42, "values": 53.669690262026634},
    {"variable": "opt2", "time_seconds": 368.64, "values": 67.29353920559024},
    {"variable": "opt2", "time_seconds": 672.04, "values": 82.30782533848364},
    {"variable": "opt2", "time_seconds": 1017.26, "values": 64.92250125677477},
    {"variable": "opt2", "time_seconds": 1319.24, "values": 61.70492445574225},
    {"variable": "opt2", "time_seconds": 1621.84, "values": 66.73124984237081},
    {"variable": "opt3", "time_seconds": 62.34, "values": 67.07091129789107},
    {"variable": "opt3", "time_seconds": 364.74, "values": 60.39192699523444},
    {"variable": "opt3", "time_seconds": 666.68, "values": 57.13104540996532},
    {"variable": "opt3", "time_seconds": 967.76, "values": 50.293945860615096},
    {"variable": "opt3", "time_seconds": 1317.33, "values": 73.49109300734065},
    {"variable": "opt3", "time_seconds": 1619.03, "values": 80.53859104682748},
]

table = pd.DataFrame.from_dict(data_dict)

px.scatter(
    table,
    x=pd.to_datetime(table["time_seconds"], unit="s"),
    y="values",
    color="variable",
    hover_data=["time_seconds"],
    range_x=[
        pd.to_datetime(table["time_seconds"], unit="s").min(),
        pd.to_datetime(table["time_seconds"], unit="s").max(),
    ],
    color_discrete_map={
        "opt1": "#008aff",
        "opt2": "#8c2eff",
        "opt3": "#56cb32",
    },
).update_traces(mode="lines+markers").for_each_trace(
    lambda t: t.update(yaxis="y2") if t.name == "opt1" else t
).update_layout(
    yaxis2={"overlaying": "y", "side": "right", "dtick": 1},
    xaxis_tickformat="%H:%M:%S",
    showlegend=False,
    title_text=None,
)

”在此处输入图像描述”

Explicitly set the range_x

    range_x=[
        pd.to_datetime(table["time_seconds"], unit="s").min(),
        pd.to_datetime(table["time_seconds"], unit="s").max(),
    ],

full code

import pandas as pd
import plotly.express as px

data_dict = [
    {"variable": "opt1", "time_seconds": 62.42, "values": 1.2506616294386024},
    {"variable": "opt1", "time_seconds": 368.64, "values": 1.026396270065788},
    {"variable": "opt1", "time_seconds": 672.04, "values": 0.9193268790432114},
    {"variable": "opt1", "time_seconds": 967.76, "values": 1.0040146519632747},
    {"variable": "opt1", "time_seconds": 1319.24, "values": 0.9758039569410012},
    {"variable": "opt1", "time_seconds": 1621.84, "values": 0.9608018775714326},
    {"variable": "opt2", "time_seconds": 62.42, "values": 53.669690262026634},
    {"variable": "opt2", "time_seconds": 368.64, "values": 67.29353920559024},
    {"variable": "opt2", "time_seconds": 672.04, "values": 82.30782533848364},
    {"variable": "opt2", "time_seconds": 1017.26, "values": 64.92250125677477},
    {"variable": "opt2", "time_seconds": 1319.24, "values": 61.70492445574225},
    {"variable": "opt2", "time_seconds": 1621.84, "values": 66.73124984237081},
    {"variable": "opt3", "time_seconds": 62.34, "values": 67.07091129789107},
    {"variable": "opt3", "time_seconds": 364.74, "values": 60.39192699523444},
    {"variable": "opt3", "time_seconds": 666.68, "values": 57.13104540996532},
    {"variable": "opt3", "time_seconds": 967.76, "values": 50.293945860615096},
    {"variable": "opt3", "time_seconds": 1317.33, "values": 73.49109300734065},
    {"variable": "opt3", "time_seconds": 1619.03, "values": 80.53859104682748},
]

table = pd.DataFrame.from_dict(data_dict)

px.scatter(
    table,
    x=pd.to_datetime(table["time_seconds"], unit="s"),
    y="values",
    color="variable",
    hover_data=["time_seconds"],
    range_x=[
        pd.to_datetime(table["time_seconds"], unit="s").min(),
        pd.to_datetime(table["time_seconds"], unit="s").max(),
    ],
    color_discrete_map={
        "opt1": "#008aff",
        "opt2": "#8c2eff",
        "opt3": "#56cb32",
    },
).update_traces(mode="lines+markers").for_each_trace(
    lambda t: t.update(yaxis="y2") if t.name == "opt1" else t
).update_layout(
    yaxis2={"overlaying": "y", "side": "right", "dtick": 1},
    xaxis_tickformat="%H:%M:%S",
    showlegend=False,
    title_text=None,
)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文