简体   繁体   English

如何按降序对 Plotly 条形图进行排序

[英]How to sort Plotly bar chart in descending order

I have created a basic bar chart in plotly that I would like to sort by descending order.我在 plotly 中创建了一个基本条形图,我想按降序排序。 在此处输入图像描述

I couldn't find an easy way to specify this in the plotly syntax, so I tried modifying the dataframe with Pandas.我找不到在 plotly 语法中指定它的简单方法,所以我尝试使用 Pandas 修改 dataframe。 This also hasn't worked.这也没有奏效。

My code is below:我的代码如下:

import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd


df = pd.read_csv('C:/Users/Documents/Python/CKANMay.csv')
sd = df.nlargest(3,'Views')
fd = sd.sort_values(by='Views', ascending = False)


my_data = [go.Bar( x = fd.Views, y = fd.Publisher, orientation = 'h')]
my_layout = ({"title": "Most popular publishers",
                       "yaxis": {"title":"Publisher"},
                       "xaxis": {"title":"Views"},
                       "showlegend": False})

fig = go.Figure(data = my_data, layout = my_layout)

py.iplot(fig)

I would like to invert the bar chart, so that the column with the greatest value is on the top.我想反转条形图,使具有最大值的列位于顶部。 Appreciative for any assistance.感谢任何帮助。

添加此内容以更新您的数字:

fig.update_layout(barmode='stack', xaxis={'categoryorder':'total descending'})

Update your layout with this:用这个更新你的布局:

fig.update_layout(yaxis=dict(autorange="reversed"))

source here来源这里

Example:例子:

在此处输入图像描述

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

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