使用plotly按降序排列X轴

2024-10-01 15:32:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用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

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

任何帮助都将不胜感激。在


Tags: 代码import绘图排序astlsplotlyusers
1条回答
网友
1楼 · 发布于 2024-10-01 15:32:26

实现目标的一个简单而有效的方法是根据您的需求按降序对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')

希望对您有帮助:))

相关问题 更多 >

    热门问题