<i id='jSG3T'><tr id='jSG3T'><dt id='jSG3T'><q id='jSG3T'><span id='jSG3T'><b id='jSG3T'><form id='jSG3T'><ins id='jSG3T'></ins><ul id='jSG3T'></ul><sub id='jSG3T'></sub></form><legend id='jSG3T'></legend><bdo id='jSG3T'><pre id='jSG3T'><center id='jSG3T'></center></pre></bdo></b><th id='jSG3T'></th></span></q></dt></tr></i><div id='jSG3T'><tfoot id='jSG3T'></tfoot><dl id='jSG3T'><fieldset id='jSG3T'></fieldset></dl></div>

    • <bdo id='jSG3T'></bdo><ul id='jSG3T'></ul>
    <tfoot id='jSG3T'></tfoot>

    1. <small id='jSG3T'></small><noframes id='jSG3T'>

      <legend id='jSG3T'><style id='jSG3T'><dir id='jSG3T'><q id='jSG3T'></q></dir></style></legend>
    2. 如何隐藏 plotly yaxis 标题(在 python 中)?

      时间:2023-09-28
        1. <tfoot id='lCxGu'></tfoot>
          <i id='lCxGu'><tr id='lCxGu'><dt id='lCxGu'><q id='lCxGu'><span id='lCxGu'><b id='lCxGu'><form id='lCxGu'><ins id='lCxGu'></ins><ul id='lCxGu'></ul><sub id='lCxGu'></sub></form><legend id='lCxGu'></legend><bdo id='lCxGu'><pre id='lCxGu'><center id='lCxGu'></center></pre></bdo></b><th id='lCxGu'></th></span></q></dt></tr></i><div id='lCxGu'><tfoot id='lCxGu'></tfoot><dl id='lCxGu'><fieldset id='lCxGu'></fieldset></dl></div>
            <tbody id='lCxGu'></tbody>
          <legend id='lCxGu'><style id='lCxGu'><dir id='lCxGu'><q id='lCxGu'></q></dir></style></legend>

          <small id='lCxGu'></small><noframes id='lCxGu'>

            <bdo id='lCxGu'></bdo><ul id='lCxGu'></ul>

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

                问题描述

                以下来自 Plotly 的示例供参考:

                将 plotly.express 导入为 pxdf = px.data.gapminder().query("大陆 == '欧洲' 和年份 == 2007 和流行 > 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')图.show()

                如何去掉pop"这个词.

                <小时>

                我想隐藏'value'的y轴标题.

                以下语法不起作用.

                fig.update_yaxes(showticklabels=False)

                谢谢.

                解决方案

                解决方案

                您需要在 fig.update_yaxes() 内使用 visible=Falsefig.update_layout() 如下.有关更多详细信息,请参阅

                B.如何在没有边距空间的情况下创建图形

                说,您隐藏了两个轴的标题.默认情况下 plotly仍然会在图形周围留下默认的空间量:这在 Plotly 的文档中称为 margin.

                <块引用>

                如果你想减少甚至完全去除边距怎么办?

                这可以使用 fig.update_layout(margin=dict(l = ..., r = ..., t = ..., b = ...)) 来完成在文档中:

                • C.Plotly 的一个有趣功能:隐藏的速记

                  事实证明,Plotly 有一个方便的速记符号允许 dict-flattening 可用于输入参数,例如:

                  ## 下面的所有三种方法都是等价的# 没有 dict-flattening# layout = 以 yaxis 为键的 dict布局 = {'yaxis': {'title': 'y-label',可见":错误,'showticklabels':错误}}# 部分字典扁平化# layout_yaxis = 带有键名的字典# 标题,可见,showticklabelslayout_yaxis = {'title': 'y-label',可见":错误,'showticklabels':错误}# 完成 dict-flattening# 每个键名的 layout_yaxis_key-namelayout_yaxis_title = 'y-标签'layout_yaxis_visible = Falselayout_yaxis_showticklabels = False

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

                  import plotly.graph_objects as go# 方法一:最短(不太详细)fig = go.Figure(数据=[go.Bar(y=[2, 1, 3])],layout_title_text=显示自身的图形",layout_yaxis_visible = False,layout_xaxis_title = 'x-标签')图.show()# Method-2: 混合字典和下划线分隔语法fig = go.Figure(数据=[go.Bar(y=[2, 1, 3])],layout_title_text=显示自身的图形",layout_xaxis_title = 'x-标签',layout_yaxis = {'title': 'y-label',可见":错误,'showticklabels':错误})图.show()# Method-3:完整的dict语法fig = go.Figure(数据=[go.Bar(y=[2, 1, 3])],layout_title_text=显示自身的图形",布局 = {'xaxis': {'title': 'x-label',可见":是的,'showticklabels': True},'yaxis': {'title': 'y-label',可见":错误,'showticklabels':错误}})图.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)
                  

                  A. 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
                  

                  B. How to create the figure without the margin space around

                  Say, you suppressed the titles for both the axes. By default plotly would still leave a default amount of space all around the figure: this is known as the margin in Plotly's documention.

                  What if you want to reduce or even completely remove the margin?

                  This can be done using fig.update_layout(margin=dict(l = ..., r = ..., t = ..., b = ...)) as mentioned in the documentation:

                  • https://plotly.com/python/reference/#layout-margin.

                  In the following example, I have reduced the left, right and bottom margins to 10 px and set the top margin to 50 px.

                  import plotly.graph_objects as go
                  
                  fig = go.Figure(
                      data=[go.Bar(y=[2, 1, 3])],
                      layout_title_text="A Figure with no axis-title and modified margins",
                      layout = {
                          'xaxis': {'title': 'x-label',
                                  'visible': False,
                                  'showticklabels': True},
                          'yaxis': {'title': 'y-label',
                                  'visible': False,
                                  'showticklabels': False},
                          # specify margins in px
                          'margin': dict(
                              l = 10,        # left
                              r = 10,        # right
                              t = 50,        # top
                              b = 10,        # bottom
                          ),
                      },
                  )
                  fig
                  

                  C. 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()
                  

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

                上一篇:Plotly:如何设置y轴的范围? 下一篇:如何在 Plotly for Python 中悬停时突出显示整个跟踪?

                相关文章

                <legend id='8YgAv'><style id='8YgAv'><dir id='8YgAv'><q id='8YgAv'></q></dir></style></legend>
              • <i id='8YgAv'><tr id='8YgAv'><dt id='8YgAv'><q id='8YgAv'><span id='8YgAv'><b id='8YgAv'><form id='8YgAv'><ins id='8YgAv'></ins><ul id='8YgAv'></ul><sub id='8YgAv'></sub></form><legend id='8YgAv'></legend><bdo id='8YgAv'><pre id='8YgAv'><center id='8YgAv'></center></pre></bdo></b><th id='8YgAv'></th></span></q></dt></tr></i><div id='8YgAv'><tfoot id='8YgAv'></tfoot><dl id='8YgAv'><fieldset id='8YgAv'></fieldset></dl></div>
                • <bdo id='8YgAv'></bdo><ul id='8YgAv'></ul>

                    <tfoot id='8YgAv'></tfoot>
                  1. <small id='8YgAv'></small><noframes id='8YgAv'>