【问题标题】:How to make binwidths wider in plotly?如何在情节中使 binwidths 更宽?
【发布时间】:2022-01-01 14:31:04
【问题描述】:

我正在尝试弄清楚如何为绘图仪表板填充 bin 宽度。就目前而言,我在数据集中有一堆股票数据可以改变图表的外观。

当有宽 X 轴(行情)的股票代码时,数据几乎变得不可见。

放大后,这些垃圾箱似乎只占用了大约 10% 的可用空间。我假设这是由于它们被抵消了,但一次一步。

如何设置 binwidths 以占据整个区域?

我尝试过使用

fig.update_traces(xbins=dict(
            start = 0.0,
            end = 60,
            size = 2))

和其他变体,但无法使其工作。

下面的情节/破折号代码以及数据框示例。

import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output
import plotly.express as px


tickers = GEX['ticker'].unique()
pos_neg = GEX['pos_neg'].unique()

app = dash.Dash(__name__)

app.layout = html.Div([
    dcc.Dropdown(
        id="dropdown",
        options=[{"label": x, "value": x} for x in tickers],
        value=tickers[0],
        clearable=False,
    ),
    dcc.Graph(id="bar-chart", style = {'width': '90vh', 'height': '90vh'}),
])

@app.callback(
    Output("bar-chart", "figure"), 
    [Input("dropdown", "value")])
def gex_per_strike(ticker):
    mask = GEX['ticker'] == ticker
    fig = px.bar(GEX[mask], x="strike", y="net_GEX", 
                 color= 'pos_neg', 
                 barmode="group", 
                 title = ('Gamma Exposure for ' + ticker),
                 color_discrete_sequence = ['#DC143C','#7FFF00'])
    '''fig.update_traces(xbins=dict(
            start = 0.0,
            end = 60,
            size = 2))'''

    return fig

app.run_server(debug=True, use_reloader = False)

数据框:

import numpy as np
import pandas as pd
import random

GEX = pd.DataFrame()

GEX['strike'] = range(0,200)
GEX['pos_GEX'] = np.random.randint(0, 1000000, GEX.shape[0])
GEX['neg_GEX'] = np.random.randint(-1000000, 0, GEX.shape[0])
GEX['net_GEX'] = GEX['pos_GEX'] + GEX['neg_GEX']
GEX['pos_neg'] = GEX['net_GEX'].apply(lambda x: 1 if x > 0 else 0)
GEX['ticker'] = 'AAPL'

【问题讨论】:

    标签: python-3.x pandas plotly plotly-dash plotly-python


    【解决方案1】:

    您可以使用fig.update_layout 更改bargap 参数:

    import numpy as np
    import pandas as pd
    import random
    
    GEX = pd.DataFrame()
    
    GEX['strike'] = range(0,200)
    GEX['pos_GEX'] = np.random.randint(0, 1000000, GEX.shape[0])
    GEX['neg_GEX'] = np.random.randint(-1000000, 0, GEX.shape[0])
    GEX['net_GEX'] = GEX['pos_GEX'] + GEX['neg_GEX']
    GEX['pos_neg'] = GEX['net_GEX'].apply(lambda x: 1 if x > 0 else 0)
    GEX['ticker'] = 'AAPL'
    
    import dash
    from dash import dcc
    from dash import html
    from dash.dependencies import Input, Output
    import plotly.express as px
    
    
    tickers = GEX['ticker'].unique()
    pos_neg = GEX['pos_neg'].unique()
    
    app = dash.Dash(__name__)
    
    app.layout = html.Div([
        dcc.Dropdown(
            id="dropdown",
            options=[{"label": x, "value": x} for x in tickers],
            value=tickers[0],
            clearable=False,
        ),
        dcc.Graph(id="bar-chart", style = {'width': '90vb', 'height': '90vh'}),
    ])
    
    @app.callback(
        Output("bar-chart", "figure"),
        [Input("dropdown", "value")])
    def gex_per_strike(ticker):
        mask = GEX['ticker'] == ticker
        fig = px.bar(GEX[mask], x="strike", y="net_GEX",
                     color= 'pos_neg',
                     barmode="group",
                     # width=1,
                     title = ('Gamma Exposure for ' + ticker),
                     color_discrete_sequence = ['#DC143C','#7FFF00'])
        fig.update_layout(
            bargap=0
        )
        '''fig.update_traces(xbins=dict(
                start = 0.0,
                end = 60,
                size = 2))'''
    
        return fig
    
    app.run_server(debug=True, use_reloader = False)
    

    条形图也有一个width argument - 但我想这不是这里的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-07
      • 2021-05-08
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 2017-04-09
      相关资源
      最近更新 更多