使用plotly以降序排列X轴

时间:2016-04-12 20:52:54

标签: python matplotlib plot plotly

我尝试使用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')

并且这里是生成的情节:

enter image description here

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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')

希望能帮到你:))

相关问题