Insert space between ticks on y-Axsis / Control height of ticks on Python Plotly

Question:

I have a linegraph with twolines it in. On the x-Axis there is the time, on the y-Axes the values. On the y-Axis I want to set the space between the ticks manually.

  • My data is very dense in the range from 0 – 5, so I want the ticks in this range to be far away from each other in order distinguish between the two lines plotted in the graph.
  • Between 5 – 10 my data has more or less the same values, so the ticks can be closer together here.
  • From 10 – 15 need to spread the ticks again because my data is dense here.

I tried setting the values of the ticks manually, but that does not add/remove space/height of the ticks

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
df['Date']=pd.to_datetime(df['Date'])    
dfg = df.groupby([pd.Grouper(key='Date', freq='M'), 'direction']).size().to_frame('counts')
dfg.reset_index(inplace=True)

layout = Layout(
    title='Foo',
    plot_bgcolor='rgba(0,0,0,0)', 
    yaxis = dict(
        tickmode = 'array',
        tickvals = [0, 0.5, 1, 1.5, ..., 5, 6, 7, ..., 10.5, 11, 11.5, 12, ...],
    )
)

fig = go.Figure()

for d,c in zip(dfg['direction'].unique(), ['red','green']):
    dfs = dfg.query('direction == @d')
    fig.add_trace(
        go.Scatter(
            x=dfs['Date'],
            y=dfs['counts'],
            mode='lines',
            line=dict(
                color=c,
                width=3
            ),
            name=d
        )
    )

fig.show()

Note: The data I added here is not my real data. It’s just some data to work with.

Asked By: four-eyes

||

Answers:

You need to scale your y data as you want, and then provide the ticks values and tick text

Assume:

y=[1,1.1,1.2,1.3,5,10,11,11.1,11.2,11.3]
y_trans=custom_trans(y) -> [1,2,3,4,5,6,7,8,9,10] # index
----------
go.Scatter(,y=y_trans,)
----------
fig.update_yaxes(
    ticktext=["1", "1.1", "1.2", "5","10", ...],
    tickvals=[1,2,3,5,6,..] #index position from y_trans
)

This is a very weird request though, and I would rather plot the difference in the two lines if it makes sense, or choose another method of visualizing, try log y axis?

Answered By: Igna
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.