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

时间:2019-06-22 09:14:54

标签: python pandas sorting plotly data-visualization

我已按图顺序创建了一个基本的条形图,我想按降序对其进行排序。 My current output looks like this

我找不到用pallyly语法指定此内容的简便方法,因此我尝试使用Pandas修改数据框。这也没有用。

我的代码如下:

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)

我想反转条形图,以使具有最大值的列位于顶部。感谢您的协助。

1 个答案:

答案 0 :(得分:1)

添加此内容以更新您的身材:

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