4

I want to use Plotly to generate a line chart with a range slider. the range slider shows the displayed line again. this code is just an example. in my case, I have a lot of subplots and everything is shown twice. is it possible to show nothing or only the date in the range slider?

import plotly.express as px
import yfinance as yf
yf.pdr_override()



df = yf.download(tickers='aapl' ,period='1d',interval='1m')

fig = px.line(df, x = df.index, y = 'Close',
              title='Apple Stock Price')

fig.update_layout(
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1,
                     label="1m",
                     step="month",
                     stepmode="backward"),
                dict(step="all")
            ])
        ),
        rangeslider=dict(
            visible=True
        ),
        type="date"
    )
)

fig.show()
Derek O
  • 16,770
  • 4
  • 24
  • 43
Alex
  • 999
  • 1
  • 14
  • 31

1 Answers1

3

Looking through the rangeslider documentation, there aren't any arguments that can directly impact the rangeslider line because I believe that line will have all of the same properties as the line in the figure (including color, visibility, thickness).

The best workaround I can come up with is to change the background color of the rangeslider to be the same color as the line (and this only works if all traces in the figure are the same color). I would probably also make the rangeslider have a relatively smaller thickness so it's less obtrusive, but that's a matter of personal taste.

Here is an example:

fig.update_layout(
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1,
                     label="1m",
                     step="month",
                     stepmode="backward"),
                dict(step="all")
            ])
        ),
        rangeslider=dict(
            visible=True,
            bgcolor="#636EFA",
            thickness=0.05
        ),
        type="date"
    )
)

fig.show()

enter image description here

Derek O
  • 16,770
  • 4
  • 24
  • 43
  • 1
    thank you. a workaround that works. i have a lot of different colors, so changing the background color is not possible. i made the thickness far thinner. now it is better than many different colors – Alex Feb 08 '23 at 19:03
  • 1
    glad to hear this helped! it would definitely be nice to have something like `rangeslider=dict(showtraces=False)` – Derek O Feb 08 '23 at 20:06
  • 1
    Agreed. However, if you set the `rangeslider` height to something small, such as 0.02 in my case, the background isn't seen anymore. While still being high enough to be easy to grab. With 0.05 it's still seen. Adding it as a comment as I didn't figure that out until after I had tried quite some heights. – Jakob May 22 '23 at 15:18