如何在情节中增加子图的 yticklabel 和主 yticklabel 之间的 space?
How to increase the space between yticklabel of subplot and main yticklabel in plotly?
我正在学习情节并尝试自定义一个子情节。
我需要做以下事情:
- 仅使子 yticklabels 彩色而不是 yticks。
- 例如,将“成功”设为绿色,但按原样勾选“0.40% 和 0.20%”。
- 在
subplot ylabel
和 main ylabel
之间增加 space。
- 例如,在子图 ylabel
Status Quo
. 之外制作主要 ylabel Error Rate (%)
剧情
代码
import numpy as np
import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go
df = pd.DataFrame({'Month': ['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01', '2020-05-01', '2020-06-01'],
'Site A': [0.0006171, 0.0007480000000000001, 0.00041139999999999997, 0.0005422999999999999, 9.35e-05, 0.0011407],
'Site B': [0.0003927000000000001, 0.0026, 0.0008041000000000001, 0.0005797, 0.0008789000000000001, 0.0004301000000000001],
'Site C': [0.0075548, 0.0045815000000000005, 0.0033473, 0.0016455999999999999, 0.0023375, 0.00229],
'Site D': [0.0007854000000000001, 0.0003927000000000001, 0.0013277, 0.0005235999999999999, 0.0008227999999999999, 0.0016082000000000002],
'Site E': [0.0, 0.0007480000000000001, 0.0, 0.0015520999999999998, 0.0005984000000000001, 0.00014],
'Site F': [0.0, 0.0007292999999999999, 0.0, 0.0002431, 0.0, 0.0],
'Site G': [0.0006919000000000001, 0.0008976000000000001, 0.0005422999999999999, 0.0007667, 0.0008414999999999999, 0.0008],
'Site H': [0.00257, 0.00324, 0.00512, 0.00197, 0.0009199999999999999, 0.0004301000000000001],
'Site I': [0.0013277, 0.0, 0.0, 0.0, 0.0, 0.0013277]})
df['Month'] = pd.to_datetime(df['Month'])
df = df.set_index('Month')
fig = make_subplots(rows=3,cols=3,
start_cell='top-left',
column_widths = [1200]*3,
x_title = 'Month',
y_title = 'Error Rate (%)',
subplot_titles=("Site H", "Site E", "Site B",
"Site C", "Site G", "Site F",
"Site D", "Site I", "Site A",
)
)
fig.add_scatter(x=df.index, y=df['Site H'], row=1, col=1, showlegend=False, line=dict(color='darkgreen'), mode='lines+markers', name='Site H')
fig.add_scatter(x=df.index, y=df['Site E'], row=1, col=2, showlegend=False, line=dict(color='limegreen'))
fig.add_scatter(x=df.index, y=df['Site B'], row=1, col=3, showlegend=False, line=dict(color='lightgreen'))
fig.add_scatter(x=df.index, y=df['Site C'], row=2, col=1, showlegend=False, line=dict(color='black'))
fig.add_scatter(x=df.index, y=df['Site G'], row=2, col=2, showlegend=False, line=dict(color='gray'))
fig.add_scatter(x=df.index, y=df['Site F'], row=2, col=3, showlegend=False, line=dict(color='silver'))
fig.add_scatter(x=df.index, y=df['Site D'], row=3, col=1, showlegend=False, line=dict(color='darkred'))
fig.add_scatter(x=df.index, y=df['Site I'], row=3, col=2, showlegend=False, line=dict(color='tomato'))
fig.add_scatter(x=df.index, y=df['Site A'], row=3, col=3, showlegend=False, line=dict(color='lightsalmon'))
fig.update_xaxes(tickangle=90, tickformat="%b")
fig.update_yaxes(tickformat=".2%")
fig.update_yaxes(row=1, col=1, title='Success', color='darkgreen' )
fig.update_yaxes(row=2, col=1, title='Status Quo', color='black')
fig.update_yaxes(row=3, col=1, title=dict(text='Watch', standoff=10), color='darkred')
fig.update_layout(
title='2020 Monthy Error Rate by Site',
title_x=0.5,
autosize=False,
width=800,
height=800,
margin=dict(
l=80,
r=30,
b=80,
t=80,
pad=0
),
paper_bgcolor="LightSteelBlue",
)
fig.show()
1。 Y-axis标题颜色
为了设置y-axis标题颜色,更改
fig.update_yaxes(row=1, col=1, title = dict(text = 'Success', color='darkgreen'))
对此:
fig.update_yaxes(row=1, col=1, title='Success')
fig.update_yaxes(row=1, col=1, title_font_color="darkgreen")
您的原始方法将与该特定子图关联的 所有 文本属性的颜色设置为 "darkgreen"
。建议的方法只改变坐标轴标题的颜色,其余不变。
2。在主要 y-tick 标签和子图 y-tick 标签之间增加 space。
似乎最好的方法是删除 make_subplots
中 y_title
的定义,因为该特定属性似乎是 a bit rigid。然后使用 margin=dict(l=120...)
在子图左侧留出更多空间,并在适当的位置使用 fig.add_annotation
包含注释。如果你实际上想显示 'Erro Rate %'
outside 'Status Quo'
你可以使用:
fig.add_annotation(dict(font=dict(color="black",size=14),
x=-0.16,
y=0.5,
showarrow=False,
text='Error Rate (%)',
textangle=-90,
xref="paper",
yref="paper"
)
)
情节
完整代码
import numpy as np
import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go
df = pd.DataFrame({'Month': ['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01', '2020-05-01', '2020-06-01'],
'Site A': [0.0006171, 0.0007480000000000001, 0.00041139999999999997, 0.0005422999999999999, 9.35e-05, 0.0011407],
'Site B': [0.0003927000000000001, 0.0026, 0.0008041000000000001, 0.0005797, 0.0008789000000000001, 0.0004301000000000001],
'Site C': [0.0075548, 0.0045815000000000005, 0.0033473, 0.0016455999999999999, 0.0023375, 0.00229],
'Site D': [0.0007854000000000001, 0.0003927000000000001, 0.0013277, 0.0005235999999999999, 0.0008227999999999999, 0.0016082000000000002],
'Site E': [0.0, 0.0007480000000000001, 0.0, 0.0015520999999999998, 0.0005984000000000001, 0.00014],
'Site F': [0.0, 0.0007292999999999999, 0.0, 0.0002431, 0.0, 0.0],
'Site G': [0.0006919000000000001, 0.0008976000000000001, 0.0005422999999999999, 0.0007667, 0.0008414999999999999, 0.0008],
'Site H': [0.00257, 0.00324, 0.00512, 0.00197, 0.0009199999999999999, 0.0004301000000000001],
'Site I': [0.0013277, 0.0, 0.0, 0.0, 0.0, 0.0013277]})
df['Month'] = pd.to_datetime(df['Month'])
df = df.set_index('Month')
fig = make_subplots(rows=3,cols=3,
start_cell='top-left',
column_widths = [1200]*3,
x_title = 'Month',
#y_title = 'Error Rate (%)',
subplot_titles=("Site H", "Site E", "Site B",
"Site C", "Site G", "Site F",
"Site D", "Site I", "Site A",
)
)
fig.add_scatter(x=df.index, y=df['Site H'], row=1, col=1, showlegend=False, line=dict(color='darkgreen'), mode='lines+markers', name='Site H')
fig.add_scatter(x=df.index, y=df['Site E'], row=1, col=2, showlegend=False, line=dict(color='limegreen'))
fig.add_scatter(x=df.index, y=df['Site B'], row=1, col=3, showlegend=False, line=dict(color='lightgreen'))
fig.add_scatter(x=df.index, y=df['Site C'], row=2, col=1, showlegend=False, line=dict(color='black'))
fig.add_scatter(x=df.index, y=df['Site G'], row=2, col=2, showlegend=False, line=dict(color='gray'))
fig.add_scatter(x=df.index, y=df['Site F'], row=2, col=3, showlegend=False, line=dict(color='silver'))
fig.add_scatter(x=df.index, y=df['Site D'], row=3, col=1, showlegend=False, line=dict(color='darkred'))
fig.add_scatter(x=df.index, y=df['Site I'], row=3, col=2, showlegend=False, line=dict(color='tomato'))
fig.add_scatter(x=df.index, y=df['Site A'], row=3, col=3, showlegend=False, line=dict(color='lightsalmon'))
fig.update_xaxes(tickangle=90, tickformat="%b")
fig.update_yaxes(tickformat=".2%")
fig.update_yaxes(row=1, col=1, title='Success')
fig.update_yaxes(row=1, col=1, title_font_color="darkgreen", autorange = True)
##fig.update_yaxes(row=1, col=1, title = dict(text = 'Success', color='darkgreen'))
fig.update_yaxes(row=2, col=1, title=dict(text='Status Quo',standoff=10), color='black', autorange = True)
fig.update_yaxes(row=3, col=1, title=dict(text='Watch', standoff=10), color='darkred', autorange = True)
fig.update_layout(
title='2020 Monthy Error Rate by Site',
title_x=0.5,
autosize=False,
width=800,
height=800,
margin=dict(
l=120,
r=30,
b=80,
t=80,
pad=0
),
paper_bgcolor="LightSteelBlue",
)
fig.add_annotation(dict(font=dict(color="black",size=14),
x=-0.16,
y=0.5,
showarrow=False,
text='Error Rate (%)',
textangle=-90,
xref="paper",
yref="paper"
)
)
fig.show()
我正在学习情节并尝试自定义一个子情节。 我需要做以下事情:
- 仅使子 yticklabels 彩色而不是 yticks。
- 例如,将“成功”设为绿色,但按原样勾选“0.40% 和 0.20%”。
- 在
subplot ylabel
和main ylabel
之间增加 space。- 例如,在子图 ylabel
Status Quo
. 之外制作主要 ylabel
Error Rate (%)
- 例如,在子图 ylabel
剧情
代码
import numpy as np
import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go
df = pd.DataFrame({'Month': ['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01', '2020-05-01', '2020-06-01'],
'Site A': [0.0006171, 0.0007480000000000001, 0.00041139999999999997, 0.0005422999999999999, 9.35e-05, 0.0011407],
'Site B': [0.0003927000000000001, 0.0026, 0.0008041000000000001, 0.0005797, 0.0008789000000000001, 0.0004301000000000001],
'Site C': [0.0075548, 0.0045815000000000005, 0.0033473, 0.0016455999999999999, 0.0023375, 0.00229],
'Site D': [0.0007854000000000001, 0.0003927000000000001, 0.0013277, 0.0005235999999999999, 0.0008227999999999999, 0.0016082000000000002],
'Site E': [0.0, 0.0007480000000000001, 0.0, 0.0015520999999999998, 0.0005984000000000001, 0.00014],
'Site F': [0.0, 0.0007292999999999999, 0.0, 0.0002431, 0.0, 0.0],
'Site G': [0.0006919000000000001, 0.0008976000000000001, 0.0005422999999999999, 0.0007667, 0.0008414999999999999, 0.0008],
'Site H': [0.00257, 0.00324, 0.00512, 0.00197, 0.0009199999999999999, 0.0004301000000000001],
'Site I': [0.0013277, 0.0, 0.0, 0.0, 0.0, 0.0013277]})
df['Month'] = pd.to_datetime(df['Month'])
df = df.set_index('Month')
fig = make_subplots(rows=3,cols=3,
start_cell='top-left',
column_widths = [1200]*3,
x_title = 'Month',
y_title = 'Error Rate (%)',
subplot_titles=("Site H", "Site E", "Site B",
"Site C", "Site G", "Site F",
"Site D", "Site I", "Site A",
)
)
fig.add_scatter(x=df.index, y=df['Site H'], row=1, col=1, showlegend=False, line=dict(color='darkgreen'), mode='lines+markers', name='Site H')
fig.add_scatter(x=df.index, y=df['Site E'], row=1, col=2, showlegend=False, line=dict(color='limegreen'))
fig.add_scatter(x=df.index, y=df['Site B'], row=1, col=3, showlegend=False, line=dict(color='lightgreen'))
fig.add_scatter(x=df.index, y=df['Site C'], row=2, col=1, showlegend=False, line=dict(color='black'))
fig.add_scatter(x=df.index, y=df['Site G'], row=2, col=2, showlegend=False, line=dict(color='gray'))
fig.add_scatter(x=df.index, y=df['Site F'], row=2, col=3, showlegend=False, line=dict(color='silver'))
fig.add_scatter(x=df.index, y=df['Site D'], row=3, col=1, showlegend=False, line=dict(color='darkred'))
fig.add_scatter(x=df.index, y=df['Site I'], row=3, col=2, showlegend=False, line=dict(color='tomato'))
fig.add_scatter(x=df.index, y=df['Site A'], row=3, col=3, showlegend=False, line=dict(color='lightsalmon'))
fig.update_xaxes(tickangle=90, tickformat="%b")
fig.update_yaxes(tickformat=".2%")
fig.update_yaxes(row=1, col=1, title='Success', color='darkgreen' )
fig.update_yaxes(row=2, col=1, title='Status Quo', color='black')
fig.update_yaxes(row=3, col=1, title=dict(text='Watch', standoff=10), color='darkred')
fig.update_layout(
title='2020 Monthy Error Rate by Site',
title_x=0.5,
autosize=False,
width=800,
height=800,
margin=dict(
l=80,
r=30,
b=80,
t=80,
pad=0
),
paper_bgcolor="LightSteelBlue",
)
fig.show()
1。 Y-axis标题颜色
为了设置y-axis标题颜色,更改
fig.update_yaxes(row=1, col=1, title = dict(text = 'Success', color='darkgreen'))
对此:
fig.update_yaxes(row=1, col=1, title='Success')
fig.update_yaxes(row=1, col=1, title_font_color="darkgreen")
您的原始方法将与该特定子图关联的 所有 文本属性的颜色设置为 "darkgreen"
。建议的方法只改变坐标轴标题的颜色,其余不变。
2。在主要 y-tick 标签和子图 y-tick 标签之间增加 space。
似乎最好的方法是删除 make_subplots
中 y_title
的定义,因为该特定属性似乎是 a bit rigid。然后使用 margin=dict(l=120...)
在子图左侧留出更多空间,并在适当的位置使用 fig.add_annotation
包含注释。如果你实际上想显示 'Erro Rate %'
outside 'Status Quo'
你可以使用:
fig.add_annotation(dict(font=dict(color="black",size=14),
x=-0.16,
y=0.5,
showarrow=False,
text='Error Rate (%)',
textangle=-90,
xref="paper",
yref="paper"
)
)
情节
完整代码
import numpy as np
import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go
df = pd.DataFrame({'Month': ['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01', '2020-05-01', '2020-06-01'],
'Site A': [0.0006171, 0.0007480000000000001, 0.00041139999999999997, 0.0005422999999999999, 9.35e-05, 0.0011407],
'Site B': [0.0003927000000000001, 0.0026, 0.0008041000000000001, 0.0005797, 0.0008789000000000001, 0.0004301000000000001],
'Site C': [0.0075548, 0.0045815000000000005, 0.0033473, 0.0016455999999999999, 0.0023375, 0.00229],
'Site D': [0.0007854000000000001, 0.0003927000000000001, 0.0013277, 0.0005235999999999999, 0.0008227999999999999, 0.0016082000000000002],
'Site E': [0.0, 0.0007480000000000001, 0.0, 0.0015520999999999998, 0.0005984000000000001, 0.00014],
'Site F': [0.0, 0.0007292999999999999, 0.0, 0.0002431, 0.0, 0.0],
'Site G': [0.0006919000000000001, 0.0008976000000000001, 0.0005422999999999999, 0.0007667, 0.0008414999999999999, 0.0008],
'Site H': [0.00257, 0.00324, 0.00512, 0.00197, 0.0009199999999999999, 0.0004301000000000001],
'Site I': [0.0013277, 0.0, 0.0, 0.0, 0.0, 0.0013277]})
df['Month'] = pd.to_datetime(df['Month'])
df = df.set_index('Month')
fig = make_subplots(rows=3,cols=3,
start_cell='top-left',
column_widths = [1200]*3,
x_title = 'Month',
#y_title = 'Error Rate (%)',
subplot_titles=("Site H", "Site E", "Site B",
"Site C", "Site G", "Site F",
"Site D", "Site I", "Site A",
)
)
fig.add_scatter(x=df.index, y=df['Site H'], row=1, col=1, showlegend=False, line=dict(color='darkgreen'), mode='lines+markers', name='Site H')
fig.add_scatter(x=df.index, y=df['Site E'], row=1, col=2, showlegend=False, line=dict(color='limegreen'))
fig.add_scatter(x=df.index, y=df['Site B'], row=1, col=3, showlegend=False, line=dict(color='lightgreen'))
fig.add_scatter(x=df.index, y=df['Site C'], row=2, col=1, showlegend=False, line=dict(color='black'))
fig.add_scatter(x=df.index, y=df['Site G'], row=2, col=2, showlegend=False, line=dict(color='gray'))
fig.add_scatter(x=df.index, y=df['Site F'], row=2, col=3, showlegend=False, line=dict(color='silver'))
fig.add_scatter(x=df.index, y=df['Site D'], row=3, col=1, showlegend=False, line=dict(color='darkred'))
fig.add_scatter(x=df.index, y=df['Site I'], row=3, col=2, showlegend=False, line=dict(color='tomato'))
fig.add_scatter(x=df.index, y=df['Site A'], row=3, col=3, showlegend=False, line=dict(color='lightsalmon'))
fig.update_xaxes(tickangle=90, tickformat="%b")
fig.update_yaxes(tickformat=".2%")
fig.update_yaxes(row=1, col=1, title='Success')
fig.update_yaxes(row=1, col=1, title_font_color="darkgreen", autorange = True)
##fig.update_yaxes(row=1, col=1, title = dict(text = 'Success', color='darkgreen'))
fig.update_yaxes(row=2, col=1, title=dict(text='Status Quo',standoff=10), color='black', autorange = True)
fig.update_yaxes(row=3, col=1, title=dict(text='Watch', standoff=10), color='darkred', autorange = True)
fig.update_layout(
title='2020 Monthy Error Rate by Site',
title_x=0.5,
autosize=False,
width=800,
height=800,
margin=dict(
l=120,
r=30,
b=80,
t=80,
pad=0
),
paper_bgcolor="LightSteelBlue",
)
fig.add_annotation(dict(font=dict(color="black",size=14),
x=-0.16,
y=0.5,
showarrow=False,
text='Error Rate (%)',
textangle=-90,
xref="paper",
yref="paper"
)
)
fig.show()