enumerate(iterable,star=t)

本文介绍了Python内置的enumerate函数,它用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在for循环当中。示例代码展示了如何使用enumerate从0开始迭代数据列表并打印下标和对应的元素。

iterable可以是一个序列,可迭代对象。

enumerate(iterable,start=i)从start开始,不断调用 _next_() 返回可迭代对象的下一个值,返回值是一个枚举对象。

example:

'''python'''
data = ['one','two','three','four','five','six']
for i, d in enumerate(data,0):
    print(i,d)

'''
0 one
1 two
2 three
3 four
4 five
5 six
'''

生成气泡图时出错: argument of type 'Timestamp' is not iterable def plot_bubble_chart(df, q): try: # 转换相对时间为datetime df['time'] = df['time'].apply(relative_time_to_datetime) # 将Timestamp转换为Unix时间戳(数值) df['timestamp'] = df['time'].astype('int64') // 1e9 # 将数字标签转换为中文标签 df['emotion'] = df['label'].map(label_mapping_reverse) # 为情感生成 y 轴位置 emotion_y_mapping = {emotion: idx for idx, emotion in enumerate(label_mapping_reverse.values())} df['emotion_y'] = df['emotion'].map(emotion_y_mapping) # 绘制气泡图 plt.figure(figsize=(15, 8)) colors = { "喜欢": 'blue', "厌恶": 'red', "愤怒": 'orange', "快乐": 'green', "恐惧": 'purple', "悲伤": 'gray', "惊讶": 'brown' } for emotion, group in df.groupby('emotion'): x = group['timestamp'].values y = group['emotion_y'].values # 修改这里 s = group['star'].values plt.scatter(x, y, s=s, c=colors[emotion], alpha=0.5, label=emotion) plt.xlabel('Time', fontproperties=font_prop) plt.ylabel('Emotion', fontproperties=font_prop) plt.title(f'Bubble Chart of Emotion and Likes for {q}', fontproperties=font_prop) # 自定义x轴标签,将时间戳转换回可读的日期时间格式 plt.xticks(rotation=45, fontproperties=font_prop) ax = plt.gca() x_ticks = ax.get_xticks() x_labels = [pd.to_datetime(t, unit='s').strftime('%Y-%m-%d %H:%M') for t in x_ticks] ax.set_xticklabels(x_labels) plt.legend(prop=font_prop) plt.tight_layout() # 优化布局 plt.savefig(os.path.join(IMAGES_DIR, f'{q}_bubble_chart.png')) plt.close('all') except Exception as e: print(f"生成气泡图时出错: {str(e)}") plt.close('all')
05-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值