簡體   English   中英

Aproximating a point to a region fill in Plotly 分散區域填充

[英]Aproximating a point to a region fill in Plotly Scatter Region Fill

我將 plotly 與 Python 一起使用,並且我正在考慮 x 軸或 y 軸,嘗試接近一個點到最近的區域邊界。

import pandas as pd
import numpy as np
import plotly.graph_objects as go

def plot_graph5(points_x, points_y):   
    fig = go.Figure()
    
    dx = np.array([[0,129,129,330,1594,1977,1977],[0.93,0.93,0.58,0.58,0.43,0.25,0]])
    df2 = pd.DataFrame(dx.T, columns=['x','y'])
    
    fig.add_trace(go.Scatter(x=df2.x, y=df2.y, fill='tozeroy')) # fill to trace0 y

    fig.add_trace(go.Scatter(x=points_x, y=points_y, mode='markers', marker=dict(size=8))) #fill to trace0 y
    
    fig.show()
    
points_y = np.array([0.65])
points_x = np.array([730])

plot_graph5(points_x, points_y)

結果圖

我試圖選擇 x 或 y 值並確定最接近的值。 例如,如果我選擇 x 點,讓它返回相應的 y 值,使該點與區域相交。 是否可以僅使用 plotly? 使用按鈕或類似的東西?

這實際上是一個幾何操作,將一個點捕捉到一條線。 因此已經使用shapely來執行幾何操作。 這是使用線性參考

import pandas as pd
import numpy as np
import plotly.graph_objects as go
import shapely.geometry


def plot_graph5(points_x, points_y):
    fig = go.Figure()

    dx = np.array(
        [[0, 129, 129, 330, 1594, 1977, 1977], [0.93, 0.93, 0.58, 0.58, 0.43, 0.25, 0]]
    )
    df2 = pd.DataFrame(dx.T, columns=["x", "y"])

    fig.add_trace(go.Scatter(x=df2.x, y=df2.y, fill="tozeroy"))  # fill to trace0 y

    snap_x = []
    snap_y = []
    ls = shapely.geometry.LineString(dx.T)

    for x, y in zip(points_x, points_y):
        p = ls.interpolate(ls.project(shapely.geometry.Point(x, y)))
        snap_x.append(p.x)
        snap_y.append(p.y)

    fig.add_trace(
        go.Scatter(
            x=points_x, y=points_y, name="raw", mode="markers", marker=dict(size=8)
        )
    )  # fill to trace0 y
    fig.add_trace(
        go.Scatter(
            x=snap_x, y=snap_y, name="snapped", mode="markers", marker=dict(size=8)
        )
    )  # fill to trace0 y

    fig.show()

points_y = np.array([0.65, 0.63])
points_x = np.array([730, 1100])

plot_graph5(points_x, points_y)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM