Python Plotly Gauge 阈值标签

问题描述 投票:0回答:1

我有一个带有阈值的简单仪表,有没有办法向阈值添加标签?

下面是一些来自plotly的示例代码,用于显示我正在使用的仪表和阈值的类型。

import plotly.graph_objects as go

fig = go.Figure(go.Indicator(
    mode = "gauge+number+delta",
    value = 420,
    domain = {'x': [0, 1], 'y': [0, 1]},
    title = {'text': "Speed", 'font': {'size': 24}},
    delta = {'reference': 400, 'increasing': {'color': "RebeccaPurple"}},
    gauge = {
        'axis': {'range': [None, 500], 'tickwidth': 1, 'tickcolor': "darkblue"},
        'bar': {'color': "darkblue"},
        'bgcolor': "white",
        'borderwidth': 2,
        'bordercolor': "gray",
        'steps': [
            {'range': [0, 250], 'color': 'cyan'},
            {'range': [250, 400], 'color': 'royalblue'}],
        'threshold': {
            'line': {'color': "red", 'width': 4},
            'thickness': 0.75,
            'value': 490}}))

fig.update_layout(paper_bgcolor = "lavender", font = {'color': "darkblue", 'family': "Arial"})

fig.show()
python plotly plotly-python
1个回答
0
投票

这个解决方案并不完美,但是如果我们假设仪表在绘图的纸张坐标方面大致是一个以 (0.5, 0.1) 为中心的完美半圆,那么我们可以使用极坐标将注释大致放置在阈值的位置.

请注意,根据您调整浏览器窗口大小的方式,仪表可能会变得更椭圆,因此注释可能会稍微移动。

from numpy import pi, cos, sin
import plotly.graph_objects as go

fig = go.Figure(go.Indicator(
    mode = "gauge+number+delta",
    value = 420,
    domain = {'x': [0, 1], 'y': [0, 1]},
    title = {'text': "Speed", 'font': {'size': 24}},
    delta = {'reference': 400, 'increasing': {'color': "RebeccaPurple"}},
    gauge = {
        'axis': {'range': [None, 500], 'tickwidth': 1, 'tickcolor': "darkblue"},
        'bar': {'color': "darkblue"},
        'bgcolor': "white",
        'borderwidth': 2,
        'bordercolor': "gray",
        'steps': [
            {'range': [0, 250], 'color': 'cyan'},
            {'range': [250, 400], 'color': 'royalblue'}],
        'threshold': {
            'line': {'color': "red", 'width': 4},
            'thickness': 0.75,
            'value': 490}}))

## the center  centered at roughly (0.5,0.05)
## x = r*cos(theta), y = r*sin(theta)
threshold_radians = pi * (500-490) / 500
r = 0.35
dx = r*cos(threshold_radians)
dy = r*sin(threshold_radians)

fig.add_annotation(
    x=0.5+dx, y=0.08+dy,
    xref='paper',yref='paper',
    text="Label",
    font=dict(size=15),
    showarrow=False
)

fig.update_layout(paper_bgcolor = "lavender", font = {'color': "darkblue", 'family': "Arial"})

fig.show()

© www.soinside.com 2019 - 2024. All rights reserved.