いくつかのデータをサブプロットにプロットします。各サブプロットは、デフォルトで自動スケーリングされます。
簡単に比較できるように、すべてのサブプロットで同じスケールにしたい場合があります。
https://plotly.com/python/custom-buttons/のスタイルでボタンを使用してこれを行うことは可能ですか?
ボタン付きのサンプルコード:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
# Load dataset
df = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
df.columns = [col.replace("AAPL.", "") for col in df.columns]
# Initialize figure
fig = make_subplots(rows=1, cols=2)
# Add Traces
fig.add_trace(
go.Scatter(x=list(df.index),
y=list(df.High*2),
name="High",
line=dict(color="#33CFA5")),
row = 1, col = 1
)
fig.add_trace(
go.Scatter(x=list(df.index),
y=list(df.Low),
name="Low",
line=dict(color="#F06A6A")),
row = 1, col = 2
)
# Add Buttons
fig.update_layout(
updatemenus=[
dict(
type="buttons",
direction="right",
active=0,
x=0.57,
y=1.2,
buttons=list([
dict(label="Autoscale for each",
method="update",
args=[ # set autoscale for each subplot
{"title": "Autoscale"}]),
dict(label="Same scale",
method="update",
args=[ # set same scale for all. how?
{"title": "Same scale for all"}]),
]),
)
])
# Set title
fig.update_layout(
title_text="Yahoo",
)
fig.show()
PS私はこれを手動で行う方法を知っています:
fig.update_yaxes(range=[ymin, ymax])
この場合、レイアウトを変更するrelayout
代わりにを使用する必要がありupdate
ます。次に、両方のボタンで、autorange: True
またはrange: [y_min, y_max]
foryaxis
とを定義する必要がありyaxis2
ます。
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
# Load dataset
df = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
df.columns = [col.replace("AAPL.", "") for col in df.columns]
# you need to define yaxis range
y_max = max(df["High"].max()*2, df["Low"].max())
y_min = min(df["High"].min()*2, df["Low"].min())
# Initialize figure
fig = make_subplots(rows=1, cols=2)
# Add Traces
fig.add_trace(
go.Scatter(x=df.index,
y=df["High"]*2,
name="High",
line=dict(color="#33CFA5")),
row = 1, col = 1
)
fig.add_trace(
go.Scatter(x=df.index,
y=df["Low"],
name="Low",
line=dict(color="#F06A6A")),
row = 1, col = 2
)
# Add Buttons
fig.update_layout(
updatemenus=[
dict(
type="buttons",
direction="right",
active=0,
x=0.57,
y=1.2,
buttons=list([
dict(label="Autoscale for each",
method="relayout",
args=[{'yaxis.autorange': True,
'yaxis2.autorange': True},
]),
dict(label="Same scale",
method="relayout",
args=[{'yaxis.range': [y_min, y_max],
'yaxis2.range': [y_min, y_max]}]),
]),
)
])
# Set title
fig.update_layout(
title_text="Yahoo"
)
fig.show()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加