简体   繁体   中英

Plotly: How to offset the grid on a heatmap?

As you can see from the screenshot, the gridlines start directly in the middle of the boxes.

  • For a line chart, this is good, because the grid starts at the description on the axis labels
  • With a heatmap I would rather like the axis to go around the boxes
  • eg the y-axis line for 2020-01 (marked red) should be moved to the right (marked green), so that it lookes like a ruled paper.

Is there any way I can do that with plotly?

屏幕截图:Plotly 热图中的网格线

Here is the setup for the heatmap:

colorscale= [[0, 'yellow'], [0.7, 'red'], [1, 'darkred']]


trace_activity = go.Heatmap(
                   z=activity_orgname['activity'],
                   x=activity_orgname['month'],
                   y=activity_orgname['org_name'].astype(str),
                   hoverongaps = False,colorscale=colorscale)

layout = go.Layout(title='Activity "' + ac_name + '" per Month:',
                  xaxis={'type':'category', "gridcolor": "rgba(157, 166, 0, 1)"}, 
                  yaxis={'type':'category', "gridcolor": "rgba(157, 166, 0, 1)"}, 
                  plot_bgcolor="white")

fig = go.Figure(data=[trace_activity], 
                layout=layout, 
                )

iplot(fig)

This is what you're looking for:

fig.update_xaxes(tickson='boundaries')

在此处输入图像描述

The other option is:

fig.update_xaxes(tickson='labels')

在此处输入图像描述

Complete code:

import plotly.graph_objects as go

fig = go.Figure(data=go.Heatmap(
                   z=[[1, None, None, 50, 1], [20, None, None, 80, 30], [30, 60, None, None, 20]],
                   x=['01.01.2020', '01.02.2020', '01.03.2020', '01.04.2020', '01.05.2020'],
                   y=['Morning', 'Afternoon', 'Evening'],
                   hoverongaps = False))


fig.update_xaxes(gridcolor='steelblue',
    ticks="outside",
    tickson="boundaries",
    ticklen=20
)


fig.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM