簡體   English   中英

在 Plotly Python 中更改子圖標題位置/方向

[英]Change subplots title position/orientation in Plotly Python

我需要更改 python plotly 中的子圖標題,即旋轉 90 度。 我努力嘗試但沒有成功。

這是我的代碼

import plotly.offline as pyo
import plotly.graph_objs as go
from plotly import tools

trace1 = go.Bar(
    x=[1, 2, 3],
    y=[10, 11, 12]
)
trace2 = go.Bar(
    x=[1, 2, 3],
    y=[100, 110, 120],
)
trace3 = go.Bar(
    x=[1, 2, 3],
    y=[1000, 1100, 1200],
)

fig = tools.make_subplots(rows=1, cols=3,
                          shared_xaxes=True, shared_yaxes=True,
                          vertical_spacing=0.001,
                          subplot_titles = ('first_title', 'second_title', 'third_title'))

fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)
fig.append_trace(trace3, 1, 3)

fig['layout'].update(height=600, width=600, title='main_title')

pyo.plot(fig, filename='file.html')

所以,我想將'first_title''second_title''third_title'旋轉 90 度,這樣它們就不會相互重疊。 有可能解決這個問題嗎?

您可以簡單地在pyo.plot(fig, filename='file.html')行之前添加此代碼:

for annotation in fig['layout']['annotations']: 
    annotation['textangle']=-90

但是,這會導致子情節標題與主標題重疊,因此我建議您將其刪除。 這是最終的代碼:

import plotly.offline as pyo
import plotly.graph_objs as go
from plotly import tools

trace1 = go.Bar(
    x=[1, 2, 3],
    y=[10, 11, 12]
)
trace2 = go.Bar(
    x=[1, 2, 3],
    y=[100, 110, 120],
)
trace3 = go.Bar(
    x=[1, 2, 3],
    y=[1000, 1100, 1200],
)

fig = tools.make_subplots(rows=1, cols=3,
                          shared_xaxes=True, shared_yaxes=True,
                          vertical_spacing=0.001,
                          subplot_titles = ('first_title', 'second_title', 'third_title'))

fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)
fig.append_trace(trace3, 1, 3)

fig['layout'].update(height=600, width=600, title='')

# rotate all the subtitles of 90 degrees
for annotation in fig['layout']['annotations']: 
        annotation['textangle']=-90

pyo.plot(fig, filename='file.html')

這就是你得到的: 在此處輸入圖片說明

這個答案鏈接到Can you alter a subplot title location in plotly? 它尋求正確對齊子圖塊的答案。

我在 2 x 2 網格上遇到了類似的子圖標題問題。 答案很簡單

fig.update_annotations(yshift=20)

您還可以在一行中更新文本角度

fig.update_annotations(textangle=-90)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM