如何在 plotly 中使 binwidths 更宽?
How to make binwidths wider in plotly?
我正在尝试找出如何为 plotly dash 仪表板填充 bin 宽度。就目前而言,我在数据集中有一堆股票数据可以改变图表的外观。
当股票代码的 X 轴(行使价)较宽时,数据几乎变得不可见。
放大后,垃圾箱似乎只占可用空间的 ~ 10% space。我假设这是由于它们被抵消了,但一次一步。
如何设置 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'
您可以使用 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 - 但我想这不是问题所在。
我正在尝试找出如何为 plotly dash 仪表板填充 bin 宽度。就目前而言,我在数据集中有一堆股票数据可以改变图表的外观。
当股票代码的 X 轴(行使价)较宽时,数据几乎变得不可见。
放大后,垃圾箱似乎只占可用空间的 ~ 10% space。我假设这是由于它们被抵消了,但一次一步。
如何设置 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'
您可以使用 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 - 但我想这不是问题所在。