简体   繁体   中英

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. 在此处输入图像描述

I couldn't find an easy way to specify this in the plotly syntax, so I tried modifying the dataframe with Pandas. 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:

在此处输入图像描述

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