如何配置绘图以调整图表以拟合其背景图像?

发布于 2025-02-07 06:01:48 字数 2624 浏览 1 评论 0原文

我正在使用Plotly Dash显示覆盖图像的图。该图像很大,为7490x4906,但尺寸为尺寸。我遇到的问题是,情节似乎限制了图形的高度,这会导致图像显示的显示小于我所需的(在某些情况下,图表上没有什么,只是图像)。

因为我的网页将在任意数量的设备上使用,所以我需要autoSize是正确的,并且无法静态设置图像的大小。有什么方法可以根据图像大小尺寸?或者,更具体地说,在保持图像的纵横比的同时,调整了图表以适合图像的宽度?

这是我目前正在使用的内容:

# Load the image
image = cv2.imread("image.png")

# Initialize figure
figure = go.Figure(
    layout=go.Layout(
        autosize=True,
        font={"color": "black", "family": "Arial"},
        margin={"t": 40, "b": 10, "r": 5, "l": 10},
        paper_bgcolor="#ffffff",
        plot_bgcolor="#ffffff",
        xaxis={
            "showgrid": False,
            "tickfont": {"color": "rgba(0, 0, 0, 0)"},
            "visible": False,
        },
        yaxis={
            "showgrid": False,
            "tickfont": {"color": "rgba(0, 0, 0, 0)"},
            "visible": False,
        },
    ),
)

# Encode the image for Plotly
_, buffer = cv2.imencode(".png", image)
encoded_image = base64.b64encode(buffer)

# Populate the figure
figure.add_layout_image(
    go.layout.Image(
        source="data:image/png;base64,{image}".format(image=encoded_image.decode()),
        xref="paper",
        yref="paper",
        x=0.5,
        y=0.5,
        sizex=1,
        sizey=1,
        xanchor="center",
        yanchor="middle",
    ),
)

# Disable zoom
figure.update_xaxes(fixedrange=True)
figure.update_yaxes(fixedrange=True)

编辑:

上述代码产生的内容的屏幕截图:

更改sizey to 3go.layout.image.image配置中结果:

理想情况下,我想要类似第二张图像的东西;但是,没有切断。我的应用程序将图形包装在引导卡中。但是,我已经尝试提高卡的高度,而结果没有任何变化。该图没有更高的图像。

编辑2: 这是与我包装和显示图形有关的更多代码。这使用django-plotly-dash

from django_plotly_dash import DjangoDash

# Load the image
img = cv2.imread("image.png")

def build_figure(image):
   # Put the code from what I am currently using here and return figure
   ...

app = DjangoDash("circuits", suppress_callback_exceptions=True)
app.layout = html.Div(
    children=[
        dbc.Card(
            dbc.CardBody(
                dcc.Graph(
                    config={"displaylogo": False},
                    figure=build_figure(img),
                ),
            ),
        ),
    ],
)

I'm using Plotly Dash to display a graph that overlays an image. This image is quite large, being 7490x4906, but will be sized down. The issue I have is it seems like Plotly is restricting the height of the graph which causes the image to display smaller than I need (in some instances, there is nothing on the graph, it's just the image).

Because my webpage will be used on any number of devices, I need autosize to be true and cannot statically set the size of the image. Is there any way Plotly can size the graph based on the image? Or, more specifically, resize the graph to fit the image's width while maintaining the aspect ratio of the image?

Here is what I am currently using:

# Load the image
image = cv2.imread("image.png")

# Initialize figure
figure = go.Figure(
    layout=go.Layout(
        autosize=True,
        font={"color": "black", "family": "Arial"},
        margin={"t": 40, "b": 10, "r": 5, "l": 10},
        paper_bgcolor="#ffffff",
        plot_bgcolor="#ffffff",
        xaxis={
            "showgrid": False,
            "tickfont": {"color": "rgba(0, 0, 0, 0)"},
            "visible": False,
        },
        yaxis={
            "showgrid": False,
            "tickfont": {"color": "rgba(0, 0, 0, 0)"},
            "visible": False,
        },
    ),
)

# Encode the image for Plotly
_, buffer = cv2.imencode(".png", image)
encoded_image = base64.b64encode(buffer)

# Populate the figure
figure.add_layout_image(
    go.layout.Image(
        source="data:image/png;base64,{image}".format(image=encoded_image.decode()),
        xref="paper",
        yref="paper",
        x=0.5,
        y=0.5,
        sizex=1,
        sizey=1,
        xanchor="center",
        yanchor="middle",
    ),
)

# Disable zoom
figure.update_xaxes(fixedrange=True)
figure.update_yaxes(fixedrange=True)

EDIT:

A screenshot of what the above code produces:
enter image description here

Changing sizey to 3 in the go.layout.Image configuration results in this:
enter image description here

Ideally, I would like something like the second image; however, with there being no cut off. My application wraps the graph in a bootstrap card. However, I've already attempted to increase the height of the card with no change in the result. The graph doesn't get any taller to fit the picture.

EDIT 2:
Here is more code relating to how I wrap and display the figure. This uses django-plotly-dash:

from django_plotly_dash import DjangoDash

# Load the image
img = cv2.imread("image.png")

def build_figure(image):
   # Put the code from what I am currently using here and return figure
   ...

app = DjangoDash("circuits", suppress_callback_exceptions=True)
app.layout = html.Div(
    children=[
        dbc.Card(
            dbc.CardBody(
                dcc.Graph(
                    config={"displaylogo": False},
                    figure=build_figure(img),
                ),
            ),
        ),
    ],
)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文