Search code examples
pythonbqplot

bqplot - Tick placement outside of plot


I'm trying to place the x-axis tick marks out side of the plot, not beginning on the x-axis as below. Do you have any tips on how the achieve this?

My code for the plotting bits:

x_data = df_ts.columns.values.tolist()
y_end = df_ts.loc[end_date, :].values.flatten()
y_start = df_ts.loc[start_date, :].values.flatten()

x_sc = OrdinalScale()
y_sc = LinearScale()
ax_x = Axis(label='RIC', scale=x_sc, grid_lines='solid', tick_rotate=90)
ax_y = Axis(label='%', scale=y_sc, orientation='vertical',tick_format='0.2f')

line = Lines(x=x_data, y=[y_end, y_start], scales={'x': x_sc, 'y': y_sc}, 
             display_legend=True, labels=[end_date, start_date], stroke_width=1)
bar = Bars(x=x_data, y=df_diff.tolist(), scales={'x': x_sc, 'y': y_sc}, padding=0.5)

fig = Figure(marks=[line, bar], axes=[ax_x, ax_y], title='Swap rates and differences', legend_location='top-left')
display(fig)

Results in: enter image description here


Solution

  • I found a way to do it by changing two things for the variable ax_x.

    1. Reduce the font size by adding tick_style={'font-size': 7}.
    2. Add offset={'scale':x_sc, 'value':10}.

    The new code looks like:

    x_data = df_ts.columns.values.tolist()
    y_end = df_ts.loc[end_date, :].values.flatten()
    y_start = df_ts.loc[start_date, :].values.flatten()
    
    x_sc = OrdinalScale()
    y_sc = LinearScale()
    ax_x = Axis(label='RIC', scale=x_sc, grid_lines='solid', tick_rotate=90, tick_style={'font-size': 7}, label_offset='40',
               offset={'scale':x_sc, 'value':10})
    ax_y = Axis(label='%', scale=y_sc, orientation='vertical',tick_format='0.2f', label_offset='-40')
    
    line = Lines(x=x_data, y=[y_end, y_start], scales={'x': x_sc, 'y': y_sc}, 
                 display_legend=True, labels=[end_date, start_date], stroke_width=1)
    bar = Bars(x=x_data, y=df_diff.tolist(), scales={'x': x_sc, 'y': y_sc}, padding=0.5)
    
    fig = Figure(marks=[line, bar], axes=[ax_x, ax_y], title='Swap rates and differences', legend_location='top-left')
    display(fig)
    

    And it displays as: Results