'How to hide text labels for rangeslider in Plotly?

I'm trying to plot a time-series plot with a range slider using Plotly in Python.

import plotly.express as px
import pandas as pd

data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

data = data.head(10)

fig = px.line(data, x='Date', y='AAPL.High', text='AAPL.High', title='Time Series with Range Slider and Selectors')

fig.update_xaxes(
    rangeslider_visible=True,
    rangeselector=dict(
        buttons=list([
            dict(count=1, label="1m", step="month", stepmode="backward"),
            dict(count=6, label="6m", step="month", stepmode="backward"),
            dict(count=1, label="YTD", step="year", stepmode="todate"),
            dict(count=1, label="1y", step="year", stepmode="backward"),
            dict(step="all")
        ])
    )
)

fig.update_traces(
    textposition="top center")

fig.show()

enter image description here

I want to show the text labels in the plot and hide them in the range slider. How can I hide the text labels in the range slider?



Solution 1:[1]

The graphs result from the large number of digits in the annotated values, making them difficult to read. Also, since the highs are cut off, you may wish to remove the annotations from the range slider as an improvement. I have looked into ways to work around this but have not found any. (Maybe I didn't do enough research.) It's not the best solution, but I will respond with an improvement. The two things I did (until a wise person responds) were to adjust the number of digits in the annotation value and adjust the height of the range slider.

import plotly.express as px
import pandas as pd

data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

data = data.head(10)
text = ['{:.2f}'.format(x) for x in data['AAPL.High']]

fig = px.line(data, x='Date', y='AAPL.High',  text=text, title='Time Series with Range Slider and Selectors')

fig.update_xaxes(
    rangeslider_thickness=0.3,
    rangeslider_visible=True,
    rangeselector=dict(
        buttons=list([
            dict(count=1, label="1m", step="month", stepmode="backward"),
            dict(count=6, label="6m", step="month", stepmode="backward"),
            dict(count=1, label="YTD", step="year", stepmode="todate"),
            dict(count=1, label="1y", step="year", stepmode="backward"),
            dict(step="all")
        ])
    )
)
fig.update_yaxes(range=[data['AAPL.High'].min()-5, data['AAPL.High'].max()+5])
fig.update_traces(textposition="top center")
#fig.update_layout(height=500)

fig.show()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 r-beginners