Trying to put notes as text on the x-axis


#1

I am trying to put notes as text on specific dates on the x-axis in my plot while still leaving the dates when no notes are present. If found the following about tickvals and ticktext but they are not working in the same way I see presented in the documentation.

Enumerated Ticks with Tickvals and Ticktext

If I use this code with these lists, I get the output below:

traceA = Scatter(
x = new_df[‘Date’],
y = new_df[‘Daily_Viewability’],
name = ‘Viewability’,
yaxis = ‘y’,
mode=‘markers+lines’,
text=new_df[‘Notes’]
)
traceB = Scatter(
x = new_df[‘Date’],
y = new_df[‘TP_CTR’],
name = ‘CTR’,
yaxis = ‘y2’
)

data = [traceA, traceB]

layout = Layout(
title=‘TITLE’,
xaxis=dict(
range = [start_date, end_date],
showgrid=True,
zeroline=True,
showline=True,
mirror=‘ticks’,
gridcolor=’#bdbdbd’,
gridwidth=1,
zerolinecolor=’#969696’,
zerolinewidth=4,
linecolor=’#636363’,
linewidth=3,
title=‘Date’,
ticktext=notes_list,
tickvals=dates_list
),
yaxis=dict(
#range=[60,100],
showgrid=False,
zeroline=True,
showline=True,
mirror=‘ticks’,
gridcolor=’#bdbdbd’,
gridwidth=2,
zerolinecolor=’#969696’,
zerolinewidth=4,
linecolor=’#636363’,
linewidth=3,
title=“Viewability”
),
yaxis2=dict(
title=‘CTR’,
showgrid=False,
zeroline=True,
showline=True,
mirror=‘ticks’,
gridcolor=’#bdbdbd’,
gridwidth=2,
zerolinecolor=’#969696’,
zerolinewidth=4,
linecolor=’#636363’,
linewidth=3,
overlaying=‘y’,
side=‘right’
)
)

fig = FigureWidget(data,layout)

display(fig)

image

You can see from the plot that the dates do not show on the x-axis even though I am trying to only replace them at the tickvals for the 3 dates in the dates_list. The notes do show up in both the trace and the x-axis as I am specifiying though.

With this code where I comment out the tickvals and ticktext, the dates appear on the x-axis, but the notes do not appear.

traceA = Scatter(
x = new_df[‘Date’],
y = new_df[‘Daily_Viewability’],
name = ‘Viewability’,
yaxis = ‘y’,
mode=‘markers+lines’,
text=new_df[‘Notes’]
)
traceB = Scatter(
x = new_df[‘Date’],
y = new_df[‘TP_CTR’],
name = ‘CTR’,
yaxis = ‘y2’
)

data = [traceA, traceB]

layout = Layout(
title=‘TITLE’,
xaxis=dict(
range = [start_date, end_date],
showgrid=True,
zeroline=True,
showline=True,
mirror=‘ticks’,
gridcolor=’#bdbdbd’,
gridwidth=1,
zerolinecolor=’#969696’,
zerolinewidth=4,
linecolor=’#636363’,
linewidth=3,
title=‘Date’,

ticktext=notes_list,

tickvals=dates_list

),
yaxis=dict(
    #range=[60,100],
    showgrid=False,
    zeroline=True,
    showline=True,
    mirror='ticks',
    gridcolor='#bdbdbd',
    gridwidth=2,
    zerolinecolor='#969696',
    zerolinewidth=4,
    linecolor='#636363',
    linewidth=3,
    title="Viewability"
),
yaxis2=dict(
    title='CTR',
    showgrid=False,
    zeroline=True,
    showline=True,
    mirror='ticks',
    gridcolor='#bdbdbd',
    gridwidth=2,
    zerolinecolor='#969696',
    zerolinewidth=4,
    linecolor='#636363',
    linewidth=3,
    overlaying='y',
    side='right'
)

)

fig = FigureWidget(data,layout)

display(fig)

image

What am I missing here in terms of using tickvals and ticktext effectively to produce dates on the x-axis from the data set and then replace the dates with those from the notes_listf for the specified dates in the dates_list?


#2

Hi @benwood,

The tickvals property specifies an array of the exact locations, in data coordinates, where the ticks should be placed. That’s why you’re only getting three ticks when you specify three dates in dates_list.

I don’t think there’s a built-in way to get the effect you’re looking for. One work-around would be to specify the dates for all of the ticks you want (where you want a date or note displayed), and then include strings in ticktext for both the date labels and your notes.

-Jon


#3

Thanks @jmmease for your response. I think I am going to just create a list of tickvals for all of the dates and then just replace the missing notes with the dates themselves to show dates on the x-axis when there are no notes.

I appreciate your insight!