2016-04-12 157 views
0

我正在嘗試使用plotly創建交互式圖並且在訂購X軸時遇到問題。下面是我使用的代碼:使用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

實現您的目標的一種簡單有效的方法是按照您的要求以降序對熊貓數據框進行排序,然後使用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') 

希望幫助你:))

+0

非常感謝您的回答。我在'iplot'調用之前添加了'.sort_values(axis ='retention',ascending = False,inplace = True)',並且我得到以下錯誤:' TypeError:sort_values()至少需要2個參數)'。我甚至沒有4個參數。你會碰巧知道爲什麼會發生這種情況嗎? – Patthebug

+0

@Patthebug非常抱歉,因爲我忘記提及sort_values函數工作所必需的'by'參數。對於列分類,軸始終爲0。您可以驗證結果。如有不便之處,敬請原諒。 –

+0

原來,只需要'sort_values(by ='retention',ascending = False')。沒有道歉需要,你是巨大的幫助。非常感謝 :)。 – Patthebug