简体   繁体   English

Plotly 条形图不升/降

[英]Plotly bar chart not ascending/descending

I have a bar chart in plotly that I have produced, however, it is not in any type of order.我在 plotly 中有一个我制作的条形图,但是,它没有任何类型的顺序。 How would I sort to ascending or descending?我将如何排序为升序或降序?

在此处输入图像描述

What I am doing:我在做什么:

fig = px.bar(data, x='Old_SKU', y='u_power')
fig = data.sort_values('u_power', ascending=True)
fig.show()

I'm not sure what your desired output is, or what your data looks like.我不确定您想要的 output 是什么,或者您的数据是什么样的。 In any case fig in plotly terms is normaly a plotly figure object.在任何情况下, fig术语中的图通常是 plotly 图 object。 When you're running fig = data.sort_values('u_power', ascending=True) you're not building a figure, but sorting a dataframe.当您运行fig = data.sort_values('u_power', ascending=True)时,您不是在构建图形,而是对 Z6A8064B5DF4794555500553C47C55057DZ 进行排序。 So far I can only imagine that you'd like to sort a dataset that looks like this:到目前为止,我只能想象您想要对如下所示的数据集进行排序:

在此处输入图像描述

... into this: ...进入这个:

在此处输入图像描述

Or maybe you're expecting a continuous increase or decrease?或者,也许您期望持续增加或减少? In that case you will have to share a dataset .在这种情况下,您将不得不共享一个数据集 Nevertheless, with a few tweaks depending on your dataset, the following snippet should not be far from a working solution:尽管如此,根据您的数据集进行一些调整,以下代码段应该离可行的解决方案不远:

import plotly.express as px
import numpy as np
import pandas as pd
var = np.random.randint(low=2, high=6, size=20).tolist()
data = pd.DataFrame({'u_power':var,
                   'Old_SKU':np.arange(0, len(var))})

# fig = px.bar(data, x='Old_SKU', y='u_power', barmode='stack')
fig = px.bar(data.sort_values('u_power'), x='Old_SKU', y='u_power', barmode='stack')
fig.show()

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

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