如何在条形图上设置箱尺寸

时间:2018-08-06 17:20:50

标签: python plotly

我尝试为条形图定义容器大小,以更改绘图中每个条形图的宽度。我可以使用Marker的边框宽度来做到这一点。但是,当边框的大小增加时,其高度也会改变。因此,问题是如何在plotly中设置条形图中每个条的宽度?

第一个技巧的代码如下(每​​个小节的每个对象):

data = [go.Bar(
x = [1],
y = [100], 
marker=go.Marker(
        color = 'blue',
        line=go.Line(
            color='blue',  # set bar border color
            width= 2     # set bar border width
        )
    )
),
go.Bar(
x = [2],
y = [200], 
marker=go.Marker(
        color = 'red',
        line=go.Line(
            color='red',  # set bar border color
            width= 440,      # set bar border width
        )
    )
)]

1 个答案:

答案 0 :(得分:0)

每个条形图的width属性将帮助您设置各个宽度。请记住,如果宽度为greater than 1,则相邻的条形图之间会有重叠,因此请尝试约束1 and 0之间的值。请尝试以下示例。

import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
from pandas_datareader import data as web
from datetime import datetime as dt
import plotly.graph_objs as go

app = dash.Dash()
data = [go.Bar(
x = [1],
y = [100],
width=1,
marker=go.Marker(
        color = 'blue'
    )
)
,
go.Bar(
x = [2],
y = [200], 
width=0.6,
marker=go.Marker(
        color = 'red'
    )
)
]
app.layout = html.Div(children=[
    html.H1(children='Hello Dash'),

    html.Div(children='''
        Dash: A web application framework for Python.
    '''),

    dcc.Graph(
        id='example-graph',
        figure={
            'data': data,
            'layout': {
                'title': 'Dash Data Visualization',
            'xaxis': { 'range': [0,4], 'nticks':20, 'dticks': 0.5}
            }
        }
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)