简体   繁体   English

plotly热图中的自定义排序轴

[英]Custom sorting axis in plotly heatmap

I would like to sort the xaxis of my heatmap in such a way that is in the same order as my yaxis.我想以与我的 yaxis 相同的顺序对我的热图的 xaxis 进行排序。 This means that the order would appear in such a way that the youngest age range appears first and each axis that appears thereafter is an incremental increase这意味着顺序会以这样一种方式出现,即最年轻的年龄范围首先出现,之后出现的每个轴都是增量增加

Here is a sample of my data:这是我的数据示例:

{'age_range': {0: '15-20',
  1: '15-20',
  2: '20-25',
  3: '20-25',
  4: '20-25',
  5: '20-25',
  6: '20-25',
  7: '20-25',
  8: '20-25',
  9: '20-25'},
 'opp_age_range': {0: '25-30',
  1: '25-30',
  2: '20-25',
  3: '20-25',
  4: '20-25',
  5: '20-25',
  6: '25-30',
  7: '25-30',
  8: '25-30',
  9: '25-30'},
 'division': {0: 'cruiser',
  1: 'super feather',
  2: 'feather',
  3: 'light',
  4: 'super bantam',
  5: 'welter',
  6: 'cruiser',
  7: 'feather',
  8: 'heavy',
  9: 'super bantam'},
 'sex': {0: 'male',
  1: 'male',
  2: 'male',
  3: 'male',
  4: 'male',
  5: 'male',
  6: 'male',
  7: 'male',
  8: 'male',
  9: 'male'},
 'wins': {0: 6, 1: 15, 2: 9, 3: 30, 4: 7, 5: 25, 6: 14, 7: 28, 8: 45, 9: 21},
 'loss': {0: 7, 1: 11, 2: 35, 3: 28, 4: 21, 5: 46, 6: 18, 7: 34, 8: 50, 9: 32},
 'draw': {0: 2, 1: 2, 2: 2, 3: 1, 4: 1, 5: 2, 6: 1, 7: 7, 8: 2, 9: 3},
 'other': {0: 1, 1: 1, 2: 1, 3: 2, 4: 2, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1},
 'total_fights': {0: 16,
  1: 29,
  2: 47,
  3: 61,
  4: 31,
  5: 74,
  6: 34,
  7: 70,
  8: 98,
  9: 57},
 'win_rate': {0: 37.5,
  1: 51.724137931034484,
  2: 19.148936170212767,
  3: 49.18032786885246,
  4: 22.58064516129032,
  5: 33.78378378378378,
  6: 41.17647058823529,
  7: 40.0,
  8: 45.91836734693878,
  9: 36.84210526315789}}

This is what my current outlook looks like:这就是我目前的展望:

图片

This is the code I have written thus far where fights contains some of the sample data I added in the first example:这是我迄今为止编写的代码,其中 Fights 包含我在第一个示例中添加的一些示例数据:

 order = ['15-20', '20-25', '25-30', '30-35', '35-40', '40-45', '45-50']
    return {
        'data': [
            go.Heatmap(
                z=fights['win_rate'],
                y=fights['age_range'],
                x=fights['opp_age_range'],
                showscale=True)
        ],
        'layout': go.Layout(
            title='Wins rate by boxer age range',
            xaxis={'title':'Opposition age range'},
            yaxis={'title': 'Boxer age range'},
            hovermode='closest',
            paper_bgcolor='black',
        )
    }

I have also tried using tickvals to specify the order in which things should appear in my plots but this didn't change the order of my xaxis我还尝试使用 tickvals 来指定事物应出现在我的图中的顺序,但这并没有改变我的 xaxis 的顺序

'layout': go.Layout(
    title='Wins rate by boxer age range',
    xaxis={'title':'Opposition age range','tickvals': order},
    yaxis={'title': 'Boxer age range'},
    hovermode='closest',
    paper_bgcolor='black',
)

Your sample code was not very easily reproducible, so I'll show you how you can revere the order of the x axis using fig['layout']['xaxis']['autorange'] = "reversed" on the following heatmap exampe from plotly : 您的示例代码不是很容易重现,因此我将向您展示如何在以下热图上使用fig['layout']['xaxis']['autorange'] = "reversed"fig['layout']['xaxis']['autorange'] = "reversed" x轴的顺序举例来说

Code for default x-axis order: 默认x轴顺序的代码:

import plotly.graph_objects as go

fig = go.Figure(data=go.Heatmap(
                    z=[[1, 20, 30],
                      [20, 1, 60],
                      [30, 60, 1]]))
fig.show()

Plot for default x-axis order: 默认x轴顺序图:

在此处输入图片说明

Code for customized x-axis order: 自定义x轴顺序的代码:

import plotly.graph_objects as go

fig = go.Figure(data=go.Heatmap(
                    z=[[1, 20, 30],
                      [20, 1, 60],
                      [30, 60, 1]]))

fig['layout']['xaxis']['autorange'] = "reversed"
fig.show()

Plot for customized x-axis order: 定制的x轴顺序图:

在此处输入图片说明


And of course you can do the same thing with the y-axis: 当然,您可以对y轴执行相同的操作:

Plot for customized x and y axis order: 定制的x和y轴顺序图:

在此处输入图片说明

Code for customized x and y axis order: 自定义x和y轴顺序的代码:

import plotly.graph_objects as go

fig = go.Figure(data=go.Heatmap(
                    z=[[1, 20, 30],
                      [20, 1, 60],
                      [30, 60, 1]]))

fig['layout']['xaxis']['autorange'] = "reversed"
fig['layout']['yaxis']['autorange'] = "reversed"
fig.show()

Resolved this issue by referring to the plotly figure reference . 通过参考绘图插图解决了此问题。

According to the documentation categoryarray can be used to set the order in which categories in an axis appear. 根据文档,categoryarray可用于设置轴中类别的显示顺序。

This is what I wrote: 这是我写的:

order = ['15-20', '20-25', '25-30', '30-35', '35-40']
    return {
        'data': [
            go.Heatmap(
                z=fights['win_rate'],
                y=fights['age_range'],
                x=fights['opp_age_range'],
                showscale=True)
        ],
        'layout': go.Layout(
            title='Wins rate by boxer age range',
            xaxis={'title':'Opposition age range','categoryarray': order},
            yaxis={'title': 'Boxer age range'},
            hovermode='closest',
            paper_bgcolor='black',
        )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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