如何在Plotly Px.violin中排序值

发布于 2025-02-11 11:58:16 字数 454 浏览 0 评论 0原文

伙计们!

我正在使用px.violin绘制不同状态的一些分布我想要它在左第一个位置然后下降,因此第二个应该是科罗拉多州,第三可能是路易斯安那州等。

< img src =“ https://i.sstatic.net/mn5zc.png” alt =“在此处输入图像说明”>

我一直在尝试在natsort 包装,这样做

category_orders = {'state': natsorted(true_percentile_df['price per acre_x'].unique())}

,但似乎没有用。有什么想法吗?)

guys!

i am plotting some distributions across different states using px.violin and i want to order the stated based on distribution: right now you can see that florida with the biggest variance and higher values is in the middle, while i want it on the first left position and then descending, so second should be colorado, third probably louisiana etc.

enter image description here

i've been trying to do that with the help of natsort package, doing

category_orders = {'state': natsorted(true_percentile_df['price per acre_x'].unique())}

but that does not seem to be working. any ideas?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑着哭最痛 2025-02-18 11:58:16

您可以使用Groupby并按VAR进行排序,就像我在这里完成的或定义自定义订单一样。

import pandas as pd

df = pd.DataFrame([["jojo", 3], ["marcel", 4], ["jojo", 20], ["jojo", 0.1], ["marcel", 9], ["pierre", 0.9], ["pierre", 0.8], ["pierre", 0.8]], columns=["Name", "Value"])

px.violin(df,
         x="Name",
         y="Value",
         points = "all",
         category_orders={"Name": df.groupby("Name").var().sort_values('Value',ascending=False).index.to_list()})

希望它会有所帮助。

You could use groupBy and sort by var like I've done here or define a custom order.

import pandas as pd

df = pd.DataFrame([["jojo", 3], ["marcel", 4], ["jojo", 20], ["jojo", 0.1], ["marcel", 9], ["pierre", 0.9], ["pierre", 0.8], ["pierre", 0.8]], columns=["Name", "Value"])

px.violin(df,
         x="Name",
         y="Value",
         points = "all",
         category_orders={"Name": df.groupby("Name").var().sort_values('Value',ascending=False).index.to_list()})

my results

Hope it'll help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文