36

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

enter image description here The following syntax doesn't work.

fig.update_yaxes(showticklabels=False)

Thanks.

CypherX
  • 7,019
  • 3
  • 25
  • 37
ZKK
  • 627
  • 1
  • 10
  • 16
  • this question already ans here https://stackoverflow.com/questions/40705614/hide-axis-label-only-not-entire-axis-in-pandas-plot – Welcome_back May 09 '20 at 07:11
  • @JaiMahesh Your answer seems to be about `pandas`, this question is about `plotly`. Does your solution work for `plotly` too? – Peter Pesch May 09 '20 at 07:15
  • hi all, the solution both does not solve the problem.It is Plotly by the way guys, the plt does not work. – ZKK May 09 '20 at 07:17
  • please put some code content . – Welcome_back May 09 '20 at 07:17
  • example code added – ZKK May 09 '20 at 07:20
  • 3
    Hi Patrick, the text = '' will still remain the space there. The reason why I want to remove it is that the screen size for mobile is limited and I want to save some space to remove it literally. ' – ZKK May 09 '20 at 07:24
  • _How to remove the word 'pop'._ vs _`What I want to hide the y-axis title of'value'._ ? Both should go? Where is pop on the image? – Patrick Artner May 09 '20 at 07:25
  • Hi Patrick, I am sorry that it is misleading in my question. I mean remove it. – ZKK May 09 '20 at 07:27

2 Answers2

71

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

enter image description here

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:

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

plotly-figure-with-no-axis-titles-and-reduced-margins

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()
CypherX
  • 7,019
  • 3
  • 25
  • 37
  • While this does hide the label, it does not fix the issue on mobile devices. The graphs still end up being the same width with or without the label visible. Is there a known way to remove it instead of hiding the visibility to make the graphs wider? – DarkHark Jun 05 '21 at 18:44
  • Just an idea: try setting the axes labels as empty strings. May be that will work. For example, `layout_xaxis_title = ''`. – CypherX Jun 07 '21 at 01:51
  • Yeah, I tried that too but nothing. I'm looking into modifying the graphs in JS instead of Python now. – DarkHark Jun 14 '21 at 00:45
  • 1
    @DarkHark Perhaps what you are looking for is adjusting the margin around the figure. That 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. – CypherX Jun 15 '21 at 20:00
  • 1
    @DarkHark I have updated the answer now with the margin-manipulation. Please take a look at the newly added section ***B. How to create the figure without the margin space around***. – CypherX Jun 15 '21 at 20:33
  • yes! I didn't think to look at the margins. Great solution, thank you! – DarkHark Jun 16 '21 at 17:41
  • When you set 'visible': False to update_xaxes, you hide not only the title, but also thick labels and other axis configurations. @saaj's answer below in fact hides the title, only. – bohnpessatti Nov 18 '22 at 16:54
26

How to remove the word 'pop'?

Just pass yaxis_title=None to fig.update_layout to hide default title of Y axis (similarly for xaxis_title=None for X axis).

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', yaxis_title=None)
fig.show()

hide title of Y axis

saaj
  • 23,253
  • 3
  • 104
  • 105