gpt4 book ai didi

python - Plotly:如何在带有刻面的图形表达图形中隐藏轴标题?

转载 作者:行者123 更新时间:2023-12-03 13:32:37 25 4
gpt4 key购买 nike

有没有一种简单的方法可以使用 plotly express 在分面图中隐藏重复的轴标题?我试过设置

visible=True
在下面的代码中,但这也隐藏了 y 轴刻度标签(值)。理想情况下,我想将隐藏重复轴标题设置为一般多面图的默认值(或者甚至更好,只是默认为整个多面图显示单个 x 和 y 轴标题。
下面是测试代码:
import pandas as pd
import numpy as np
import plotly.express as px
import string

# create a dataframe
cols = list(string.ascii_letters)
n = 50

df = pd.DataFrame({'Date': pd.date_range('2021-01-01', periods=n)})

# create data with vastly different ranges
for col in cols:
start = np.random.choice([1, 10, 100, 1000, 100000])
s = np.random.normal(loc=0, scale=0.01*start, size=n)
df[col] = start + s.cumsum()

# melt data columns from wide to long
dfm = df.melt("Date")

fig = px.line(
data_frame=dfm,
x = 'Date',
y = 'value',
facet_col = 'variable',
facet_col_wrap=6,
facet_col_spacing=0.05,
facet_row_spacing=0.035,
height = 1000,
width = 1000,
title = 'Value vs. Date'
)

fig.update_yaxes(matches=None, showticklabels=True, visible=True)
fig.update_annotations(font=dict(size=16))
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
enter image description here
最终代码(接受的答案)。注意 plotly >= 4.9
import pandas as pd
import numpy as np
import plotly.express as px
import string
import plotly.graph_objects as go

# create a dataframe
cols = list(string.ascii_letters)
n = 50

df = pd.DataFrame({'Date': pd.date_range('2021-01-01', periods=n)})

# create data with vastly different ranges
for col in cols:
start = np.random.choice([1, 10, 100, 1000, 100000])
s = np.random.normal(loc=0, scale=0.01*start, size=n)
df[col] = start + s.cumsum()

# melt data columns from wide to long
dfm = df.melt("Date")

fig = px.line(
data_frame=dfm,
x = 'Date',
y = 'value',
facet_col = 'variable',
facet_col_wrap=6,
facet_col_spacing=0.05,
facet_row_spacing=0.035,
height = 1000,
width = 1000,
title = 'Value vs. Date'
)

fig.update_yaxes(matches=None, showticklabels=True, visible=True)
fig.update_annotations(font=dict(size=16))
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))

# hide subplot y-axis titles and x-axis titles
for axis in fig.layout:
if type(fig.layout[axis]) == go.layout.YAxis:
fig.layout[axis].title.text = ''
if type(fig.layout[axis]) == go.layout.XAxis:
fig.layout[axis].title.text = ''

# keep all other annotations and add single y-axis and x-axis title:
fig.update_layout(
# keep the original annotations and add a list of new annotations:
annotations = list(fig.layout.annotations) +
[go.layout.Annotation(
x=-0.07,
y=0.5,
font=dict(
size=16, color = 'blue'
),
showarrow=False,
text="single y-axis title",
textangle=-90,
xref="paper",
yref="paper"
)
] +
[go.layout.Annotation(
x=0.5,
y=-0.08,
font=dict(
size=16, color = 'blue'
),
showarrow=False,
text="Dates",
textangle=-0,
xref="paper",
yref="paper"
)
]
)

fig.show()

最佳答案

这个答案有五个部分:

  • 隐藏子 plotly 标题(虽然不是 100% 确定你想这样做......)
  • 使用 fig.layout[axis].tickfont = dict(color = 'rgba(0,0,0,0)') 隐藏 y 轴刻度值
  • 使用 go.layout.Annotation(xref="paper", yref="paper") 设置单轴标签
  • plotly 图
  • 最后完整的代码片段

  • 这里非常重要的一点是,您可以编辑使用 px 生成的任何元素。函数使用 plotly.graph_object引用文献,例如 go.layout.XAxis .

    1.隐藏子 plotly 标题
    如果您对设置 fig 的方式感到满意,你可以只包括
    for anno in fig['layout']['annotations']:
    anno['text']=''

    fig.show()
    2.隐藏yaxis文本
    您可以在循环中使用以下内容将 yaxis tickfont 设置为透明
    fig.layout[axis].tickfont = dict(color = 'rgba(0,0,0,0)')
    该确切的行包含在下面的代码段中,它还删除了每个子图的 y 轴标题。
    3. 单轴标签
    删除轴标签和包含单个标签需要做更多的工作,但这里有一个非常灵活的设置,可以完全满足您的需要,如果您想以任何方式编辑新标签,则可以做更多:
    # hide subplot y-axis titles and x-axis titles
    for axis in fig.layout:
    if type(fig.layout[axis]) == go.layout.YAxis:
    fig.layout[axis].title.text = ''
    if type(fig.layout[axis]) == go.layout.XAxis:
    fig.layout[axis].title.text = ''

    # keep all other annotations and add single y-axis and x-axis title:
    fig.update_layout(
    # keep the original annotations and add a list of new annotations:
    annotations = list(fig.layout.annotations) +
    [go.layout.Annotation(
    x=-0.07,
    y=0.5,
    font=dict(
    size=16, color = 'blue'
    ),
    showarrow=False,
    text="single y-axis title",
    textangle=-90,
    xref="paper",
    yref="paper"
    )
    ] +
    [go.layout.Annotation(
    x=0.5,
    y=-0.08,
    font=dict(
    size=16, color = 'blue'
    ),
    showarrow=False,
    text="Dates",
    textangle=-0,
    xref="paper",
    yref="paper"
    )
    ]
    )

    fig.show()
    4. plotly
    enter image description here
    5.完整代码:
    import pandas as pd
    import numpy as np
    import plotly.express as px
    import string
    import plotly.graph_objects as go

    # create a dataframe
    cols = list(string.ascii_letters)
    cols[0]='zzz'
    n = 50

    df = pd.DataFrame({'Date': pd.date_range('2021-01-01', periods=n)})

    # create data with vastly different ranges
    for col in cols:
    start = np.random.choice([1, 10, 100, 1000, 100000])
    s = np.random.normal(loc=0, scale=0.01*start, size=n)
    df[col] = start + s.cumsum()

    # melt data columns from wide to long
    dfm = df.melt("Date")

    fig = px.line(
    data_frame=dfm,
    x = 'Date',
    y = 'value',
    facet_col = 'variable',
    facet_col_wrap=6,
    #facet_col_spacing=0.05,
    #facet_row_spacing=0.035,
    height = 1000,
    width = 1000,
    title = 'Value vs. Date'
    )

    fig.update_yaxes(matches=None, showticklabels=True, visible=True)
    fig.update_annotations(font=dict(size=16))
    fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))

    # subplot titles
    for anno in fig['layout']['annotations']:
    anno['text']=''

    # hide subplot y-axis titles and x-axis titles
    for axis in fig.layout:
    if type(fig.layout[axis]) == go.layout.YAxis:
    fig.layout[axis].title.text = ''
    if type(fig.layout[axis]) == go.layout.XAxis:
    fig.layout[axis].title.text = ''

    # keep all other annotations and add single y-axis and x-axis title:
    fig.update_layout(
    # keep the original annotations and add a list of new annotations:
    annotations = list(fig.layout.annotations) +
    [go.layout.Annotation(
    x=-0.07,
    y=0.5,
    font=dict(
    size=16, color = 'blue'
    ),
    showarrow=False,
    text="single y-axis title",
    textangle=-90,
    xref="paper",
    yref="paper"
    )
    ] +
    [go.layout.Annotation(
    x=0.5,
    y=-0.08,
    font=dict(
    size=16, color = 'blue'
    ),
    showarrow=False,
    text="Dates",
    textangle=-0,
    xref="paper",
    yref="paper"
    )
    ]
    )


    fig.show()

    关于python - Plotly:如何在带有刻面的图形表达图形中隐藏轴标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63386812/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com