#plotly-python #bullet-chart
#сюжетно-питон #маркированная диаграмма
Вопрос:
Я использую заговор.figure_factory для создания маркированных диаграмм. Я получаю вывод, но я хочу указать маркеры над осью, а не на оси. Я перепробовал все. Кто-нибудь может мне помочь?
import os def main(): data = [ {"label": "lt;span style='color: orange'gt;revenuelt;/spangt;", "sublabel": "us$, in thousands", "range": [150, 225, 300], "performance": [220,270], "point": [220]}, {"label": "lt;span style='color: orange'gt;Profitlt;/spangt;", "sublabel": "%", "range": [20, 25, 30], "performance": [21, 23], "point": [21]}, {"label": "lt;span style='color: orange'gt;Order Sizelt;/spangt;", "sublabel":"US$, average", "range": [350, 500, 600], "performance": [100,320], "point": [100]}, {"label": "lt;span style='color: orange'gt;lt;bgt;Newlt;/bgt;lt;brgt; Customerslt;/spangt;", "sublabel": "count", "range": [1400, 2000, 2500], "performance": [1000, 1650], "point": [1000]}, {"label": "lt;span style='color: orange'gt;Satisfactionlt;/spangt;", "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
Ответ №1:
- вы можете изменить трассировки рассеяния, чтобы разместить маркер выше
- оси y для увеличения значения y
- макет для обеспечения значимого цвета фона
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)")
полный код
import os import plotly.figure_factory as ff def main(): data = [ { "label": "lt;span style='color: orange'gt;revenuelt;/spangt;", "sublabel": "us$, in thousands", "range": [150, 225, 300], "performance": [220, 270], "point": [220], }, { "label": "lt;span style='color: orange'gt;Profitlt;/spangt;", "sublabel": "%", "range": [20, 25, 30], "performance": [21, 23], "point": [21], }, { "label": "lt;span style='color: orange'gt;Order Sizelt;/spangt;", "sublabel": "US$, average", "range": [350, 500, 600], "performance": [100, 320], "point": [100], }, { "label": "lt;span style='color: orange'gt;lt;bgt;Newlt;/bgt;lt;brgt; Customerslt;/spangt;", "sublabel": "count", "range": [1400, 2000, 2500], "performance": [1000, 1650], "point": [1000], }, { "label": "lt;span style='color: orange'gt;Satisfactionlt;/spangt;", "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)")