如何隐藏绘图yaxis标题(在Python中)? [英] how to hide plotly yaxis title (in python)?

查看:532
本文介绍了如何隐藏绘图yaxis标题(在Python中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:
以下示例来自Plotly以供参考:

 导入.express as px 

df = px.data.gapminder()。query( continent =='Europe'and year == 2007 and pop> 2.e6)
图= px.bar(df,y ='pop',x ='country',text ='pop')
fig.update_traces(texttemplate ='%{text:.2s}',textposition ='outside' )
fig.update_layout(uniformtext_minsize = 8,uniformtext_mode ='hide')
fig.show()

如何删除 pop一词。






我要隐藏y- 值的轴标题。




以下语法无效。

  fig.update_yaxes(showticklabels = False)

谢谢。

解决方案

解决方案



您需要使用 visible = False fig.update_yaxes() fig.update_layout()如下。有关更多详细信息,请参见



Plotly的有趣特征:隐藏的速记



事实证明,Plotly具有方便的速记符号,允许 dict-flattening 可用于以下输入参数:

  ##以下所有三种方法都等效

#没有字典拼合
#布局=字典,以yaxis为键
布局= {'yaxis':{'title':'y-label',
'visible':False,
'showticklabels':False}
}

#部分字典整平
#layout_yaxis =带有键名的字典
#标题,可见,显示标记标签
layout_yaxis = {'title':' y-label',
'visible':错误,
'showticklabels':False}

#完成dict扁平化
#layout_yaxis_key-name键名
layout_yaxis_title ='y-label'
layout_yaxis_visible = False
layout_yaxis_showticklabels = False

现在尝试运行以下所有三个命令并比较输出。

 导入plotly.graph_objects as go 

#方法1:最短(l详细信息)
fig = go.Figure(
data = [go.Bar(y = [2,1,3])]],
layout_title_text =显示自己的图形,
layout_yaxis_visible = False,
layout_xaxis_title ='x-label'

fig.show()

#方法2:字典和下划线分隔的语法
fig = go.Figure(
data = [go.Bar(y = [2,1,3])],
layout_title_text =显示自己的图形 ,
layout_xaxis_title ='x-label',
layout_yaxis = {'title':'y-label',
'visible':False,
'showticklabels':False}

fig.show()

#方法3:完整的dict语法
fig = go.Figure(
data = [go。 Bar(y = [2,1,3])],
layout_title_text =显示自己的图形,
layout = {'xaxis':{'title':'x-label',
'visible':真,
'showticklabels':真},
'yaxis':{'title':'y-label',
'visible':错误,
'showticklabels':False}
}

fig.show()


Editing: The following example from Plotly for reference:

import plotly.express as px

df = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
fig = px.bar(df, y='pop', x='country', text='pop')
fig.update_traces(texttemplate='%{text:.2s}', textposition='outside')
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')
fig.show()

How to remove the word 'pop'.


What I want to hide the y-axis title of'value'.

The following syntax doesn't work.

fig.update_yaxes(showticklabels=False)

Thanks.

解决方案

Solution

You need to use visible=False inside fig.update_yaxes() or fig.update_layout() as follows. For more details see the documentation for plotly.graph_objects.Figure.

# Option-1:  using fig.update_yaxes()
fig.update_yaxes(visible=False, showticklabels=False)

# Option-2: using fig.update_layout()
fig.update_layout(yaxis={'visible': False, 'showticklabels': False})

# Option-3: using fig.update_layout() + dict-flattening shorthand
fig.update_layout(yaxis_visible=False, yaxis_showticklabels=False)

Try doing the following to test this:

# Set the visibility ON
fig.update_yaxes(title='y', visible=True, showticklabels=False)
# Set the visibility OFF
fig.update_yaxes(title='y', visible=False, showticklabels=False)

How to create the figure directly with hidden-yaxis label and tickmarks

You can do this directly by using the layout keyword and supplying a dict to go.Figure() constructor.

import plotly.graph_objects as go
fig = go.Figure(
    data=[go.Bar(y=[2, 1, 3])],
    layout_title_text="A Figure Displaying Itself", 
    layout = {'xaxis': {'title': 'x-label', 
                        'visible': True, 
                        'showticklabels': True}, 
              'yaxis': {'title': 'y-label', 
                        'visible': False, 
                        'showticklabels': False}
              }
)
fig

An Interesting Feature of Plotly: A hidden shorthand

It turns out that Plotly has a convenient shorthand notation allowing dict-flattening available for input arguments such as this:

## ALL THREE METHODS BELOW ARE EQUIVALENT

# No dict-flattening
# layout = dict with yaxis as key
layout = {'yaxis': {'title': 'y-label', 
                    'visible': False, 
                    'showticklabels': False}
}

# Partial dict-flattening
# layout_yaxis = dict with key-names 
#     title, visible, showticklabels
layout_yaxis = {'title': 'y-label', 
                'visible': False, 
                'showticklabels': False}

# Complete dict-flattening
# layout_yaxis_key-name for each of the key-names
layout_yaxis_title = 'y-label'
layout_yaxis_visible = False
layout_yaxis_showticklabels = False

Now try running all three of the following and compare the outputs.

import plotly.graph_objects as go

# Method-1: Shortest (less detailed)
fig = go.Figure(
    data=[go.Bar(y=[2, 1, 3])],
    layout_title_text="A Figure Displaying Itself", 
    layout_yaxis_visible = False, 
    layout_xaxis_title = 'x-label'
)
fig.show()

# Method-2: A hibrid of dicts and underscore-separated-syntax
fig = go.Figure(
    data=[go.Bar(y=[2, 1, 3])],
    layout_title_text="A Figure Displaying Itself", 
    layout_xaxis_title = 'x-label', 
    layout_yaxis = {'title': 'y-label', 
                        'visible': False, 
                        'showticklabels': False}
)
fig.show()

# Method-3: A complete dict syntax
fig = go.Figure(
    data=[go.Bar(y=[2, 1, 3])],
    layout_title_text="A Figure Displaying Itself", 
    layout = {'xaxis': {'title': 'x-label', 
                        'visible': True, 
                        'showticklabels': True}, 
              'yaxis': {'title': 'y-label', 
                        'visible': False, 
                        'showticklabels': False}
              }
)
fig.show()

这篇关于如何隐藏绘图yaxis标题(在Python中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆