Plotly:如何自定义图例顺序? [英] Plotly: How to customize legend order?

查看:93
本文介绍了Plotly:如何自定义图例顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中绘制了 scatter_geo 图.

import plotly.express as px将熊猫导入为 pd行=[['501-600','65','122.58333','45.36667'],['直到 500','54','12.5'​​,'27.5'],['更多 1001','51','-115.53333','38.08'],['601-1000','54','120​​.54167','21.98'],]colmns=['bins','data','longitude','latitude']df=pd.DataFrame(数据=行,列=列)fig=px.scatter_geo(df,lon='longitude', lat='latitude',color='bins',不透明度=0.5,投影=自然地球")图.show()

如果我只有一条数据,是否有可能在图例标签中自定义顺序?

Coz 现在图例中的标签如下所示:

501-600直到 500更多 1001601-1000

我需要让它们看起来像那样:

直到 500501-600601-1000更多 1001

解决方案

如果你看看

如果您想指定任意顺序,可以通过在输入数据中定义顺序来实现.指定顺序可能有点乏味,所以我经常转置数据框,对现在出现在列中的类别进行排序,然后将其转回原来的形式:

order = ['till 500', '501-600', '601-1000', 'more 1001']df_ordered = df.T[order].T

按照这个顺序,结果会和上图一样.这是更新后的代码片段:

完整代码:

import plotly.express as px将熊猫导入为 pd行=[['501-600','65','122.58333','45.36667'],['直到 500','54','12.5'​​,'27.5'],['更多 1001','51','-115.53333','38.08'],['601-1000','54','120​​.54167','21.98'],]colmns=['bins','data','longitude','latitude']df=pd.DataFrame(数据=行,列=列)fig=px.scatter_geo(df,lon='longitude', lat='latitude',color='bins',不透明度=0.5,投影=自然地球")图.show()订单 = ['直到 500','501-600','601-1000','更多 1001']df = df.set_index('bins')df_ordered = df.T[order].T.reset_index()df_orderedfig2=px.scatter_geo(df_ordered,lon='longitude', lat='latitude',color='bins',不透明度=0.5,投影=自然地球")图2.show()

I made scatter_geo plot in Python within plotly.

import plotly.express as px
import pandas as pd

rows=[['501-600','65','122.58333','45.36667'],
      ['till 500','54','12.5','27.5'],
      ['more 1001','51','-115.53333','38.08'],
      ['601-1000','54','120.54167','21.98'],
      ]

colmns=['bins','data','longitude','latitude']
df=pd.DataFrame(data=rows, columns=colmns)

fig=px.scatter_geo(df,lon='longitude', lat='latitude',color='bins',
                      opacity=0.5,
                      projection="natural earth")
fig.show()

Are there any possibility to make customise order in legend labels if I have only one trace of data?

Coz now labels in legend looks like this:

501-600
till 500
more 1001
601-1000

Dut I need to make them looks like that:

till 500
501-600
601-1000
more 1001

解决方案

If you take a look at traceorder you'll see that items are displayed top-to-bottom in the same order as the input data for the "normal" option. So you can just change the order of your input data to get what you want:

And if you'd like to specify an arbitrary order, you can do so by defining the order in the input data. Specifying row order can ba a bit tedious, so I often transpose the dataframe, order the categories that now appear in the column, and transpose it right back to its original form:

order  = ['till 500', '501-600', '601-1000', 'more 1001']
df_ordered = df.T[order].T

With this order, the result will be the same as the figure above. Here's an updated code snippet:

Complete code:

import plotly.express as px
import pandas as pd

rows=[['501-600','65','122.58333','45.36667'],
      ['till 500','54','12.5','27.5'],
      ['more 1001','51','-115.53333','38.08'],
      ['601-1000','54','120.54167','21.98'],
      ]

colmns=['bins','data','longitude','latitude']
df=pd.DataFrame(data=rows, columns=colmns)

fig=px.scatter_geo(df,lon='longitude', lat='latitude',color='bins',
                      opacity=0.5,
                      projection="natural earth")
fig.show()


order  = ['till 500', '501-600', '601-1000', 'more 1001']
df = df.set_index('bins')
df_ordered = df.T[order].T.reset_index()
df_ordered

fig2=px.scatter_geo(df_ordered,lon='longitude', lat='latitude',color='bins',
                      opacity=0.5,
                      projection="natural earth")
fig2.show()

这篇关于Plotly:如何自定义图例顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆