【问题标题】:arrange X axis in descending order using plotly使用 plotly 按降序排列 X 轴
【发布时间】:2016-04-12 20:52:54
【问题描述】:

我正在尝试使用 plotly 创建一个交互式绘图,但无法对 X 轴进行排序。这是我正在使用的代码:

import plotly.plotly as py
import cufflinks as cf
import pandas as pd
import plotly.tools as tls
tls.set_credentials_file(username='ladeeda', api_key='ladeeda')

cf.set_config_file(offline=False, world_readable=True, theme='pearl')

StudentModalityRetention[StudentModalityRetention['schoolyearsemester'] == 'Sem3']\
   .iplot(kind='bubble', x='branch', y='retention', size='active_users', text='active_users',
             xTitle='', yTitle='Retention',
             filename='cufflinks/Sem3ModalityRetention')

这是生成的情节:

我想按降序排列 X 轴或 Y 轴。换句话说,我希望首先出现 Y 值最高的气泡,依此类推..

任何帮助将不胜感激。

【问题讨论】:

    标签: python matplotlib plot plotly


    【解决方案1】:

    实现目标的一种简单有效的方法是根据您的要求对 pandas 数据框进行降序排序,然后使用iplot 绘制图形,这将为您提供所需的结果。这是一个简短的例子:

    yourdataframe.sort_values(by='Y',\   #column name or index values according to which the dataframe is to be sorted
                         axis=0,         #for column sorting
                         ascending=False,  #in your case, for descending
                         inplace = True)\  #if you want to perform the operation inplace or return a new dataframe
                         .iplot(kind='bubble', x='branch', y='retention',size='active_users', text='active_users',
                         xTitle='', yTitle='Retention',
                         filename='cufflinks/Sem3ModalityRetention')
    

    希望对你有帮助:))

    【讨论】:

    • 非常感谢您的回答。我在iplot 调用之前添加了.sort_values(axis='retention',ascending=False,inplace = True),我收到以下错误:`TypeError:sort_values() 需要至少2 个参数(4 个给定)`。我什至没有4个论点。你会知道为什么会这样吗?
    • @Patthebug 非常抱歉,因为我忘了提及 sort_values 函数工作所必需的 'by' 参数。对于列排序,轴始终为 0。您可以验证结果。如有任何不便,敬请见谅。
    • 原来,只需要sort_values(by='retention',ascending=False)。不需要道歉,你帮了大忙。非常感谢:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    相关资源
    最近更新 更多