0

我正在使用 plotly.figure_factory 制作子弹图。我得到了输出,但我想在轴上方而不是在轴上提供标记。我什么都试过了。谁能帮我吗?

import os

def main():
        
    data = [
    {"label": "<span style='color: orange'>revenue</span>",
    "sublabel": "us$, in thousands",
    "range": [150, 225, 300],
    "performance": [220,270],
    "point": [220]},
        
    {"label": "<span style='color: orange'>Profit</span>",
    "sublabel": "%",
    "range": [20, 25, 30],
    "performance": [21, 23],
    "point": [21]}, 

    {"label": "<span style='color: orange'>Order Size</span>",
    "sublabel":"US$, average",
    "range": [350, 500, 600],
    "performance": [100,320],
    "point": [100]},
        
    {"label": "<span style='color: orange'><b>New</b><br> Customers</span>",
    "sublabel": "count",
    "range": [1400, 2000, 2500],
    "performance": [1000, 1650],
    "point": [1000]},

    {"label": "<span style='color: orange'>Satisfaction</span>",
    "sublabel": "out of 5",
    "range": [3.5, 4.25, 5],
    "performance": [3.2, 4.7],
    "point": [3.2]}
    ]

    fig = ff.create_bullet(
        data, titles='label',
        subtitles='sublabel',
        markers='point',
        measures='performance',
        ranges='range',
        orientation='h',
        title='my simple bullet chart',
        scatter_options= {
            'cliponaxis': False,
            'marker': {
                'symbol': 'triangle-down',
                'size': 20,
                'color': 'royalblue',
                'x': 20
                # 'opacity': 0.7,
            },
        }
    )

    # update_xaxes
    fig.update_yaxes(layer="below traces")
    fig.update_xaxes(ticks="outside")

    if not os.path.exists('pic'):
        os.mkdir('pic')
    fig.write_image('pic/size.png')

if __name__ == '__main__':
    main()

我想要实现的目标:https ://i.stack.imgur.com/XI4Ar.png

4

1 回答 1

0
  • you can modify scatter traces to place marker higher
  • y-axes to allow for larger value of y
  • layout to ensure background color is meaningful
fig.update_yaxes(range=[0, 1.8]).for_each_trace(
    lambda t: t.update(y=[1.8]) if t.type == "scatter" else t
).update_layout(plot_bgcolor="rgba(0,0,0,0)")

full code

import os
import plotly.figure_factory as ff


def main():

    data = [
        {
            "label": "<span style='color: orange'>revenue</span>",
            "sublabel": "us$, in thousands",
            "range": [150, 225, 300],
            "performance": [220, 270],
            "point": [220],
        },
        {
            "label": "<span style='color: orange'>Profit</span>",
            "sublabel": "%",
            "range": [20, 25, 30],
            "performance": [21, 23],
            "point": [21],
        },
        {
            "label": "<span style='color: orange'>Order Size</span>",
            "sublabel": "US$, average",
            "range": [350, 500, 600],
            "performance": [100, 320],
            "point": [100],
        },
        {
            "label": "<span style='color: orange'><b>New</b><br> Customers</span>",
            "sublabel": "count",
            "range": [1400, 2000, 2500],
            "performance": [1000, 1650],
            "point": [1000],
        },
        {
            "label": "<span style='color: orange'>Satisfaction</span>",
            "sublabel": "out of 5",
            "range": [3.5, 4.25, 5],
            "performance": [3.2, 4.7],
            "point": [3.2],
        },
    ]

    fig = ff.create_bullet(
        data,
        titles="label",
        subtitles="sublabel",
        markers="point",
        measures="performance",
        ranges="range",
        orientation="h",
        title="my simple bullet chart",
        scatter_options={
            "cliponaxis": False,
            "marker": {
                "symbol": "triangle-down",
                "size": 15,
                "color": "royalblue",
                # 'x': 20
                # 'opacity': 0.7,
            },
        },
    )

    # update_xaxes
    fig.update_yaxes(layer="below traces")
    fig.update_xaxes(ticks="outside")

    # if not os.path.exists('pic'):
    #     os.mkdir('pic')
    # fig.write_image('pic/size.png')
    return fig


main().update_yaxes(range=[0, 1.8]).for_each_trace(
    lambda t: t.update(y=[1.8]) if t.type == "scatter" else t
).update_layout(plot_bgcolor="rgba(0,0,0,0)")

enter image description here

于 2021-12-11T12:24:15.020 回答