我应该如何使用Bokeh和Pandas绘制分类数据的散点图?

2024-06-01 02:30:48 发布

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

我有一个包含数据的Pandas数据框:

Merchant | Non-Merchant | Num of Chats Received | Num of Chats Made
1        | 0            | 80                    | 50
0        | 1            | 60                    | 30
1        | 0            | 70                    | 40
0        | 1            | 50                    | 20

我希望能够在一个散点图上绘制两种不同类型的用户(商人,非商人),比较他们的[numofchats Received,y轴]。在

在上面的数据中

商户是指在商户栏中标记为“1”的商户

非商户是指在商户栏中标记为“1”的商户

它是二进制的,没有[1,1]。在

一般来说,我对Bokeh和Data-Viz还不熟悉,假设Bokeh是我的首选方法,我该怎么做呢?在


Tags: of数据标记类型pandasbokeh绘制merchant
1条回答
网友
1楼 · 发布于 2024-06-01 02:30:48

这里有一个方法来对付博克

代码:

from bokeh.plotting import figure, output_file, show
import pandas as pd

data = {'Merchant':[1,0,1,0],
        'Non-Merchant':[0,1,0,1],
        'Num of Chats Received':[80,60,70,50],
        'Num of Chats Made':[50,30,40,20]}

df = pd.DataFrame(data)

merchant_chats_made = list(df[df['Merchant'] == 1]['Num of Chats Made'])
merchant_chats_received = list(df[df['Merchant'] == 1]['Num of Chats Received'])
non_merchant_chats_made = list(df[df['Non-Merchant'] == 1]['Num of Chats Made'])
non_merchant_chats_received = list(df[df['Non-Merchant'] == 1]['Num of Chats Received'])

output_file('merchant.html')

p = figure(plot_width=400, plot_height=400)
p.circle(merchant_chats_made,
         merchant_chats_received,
         size=10,color='red',alpha=0.5,
         legend='Merchant')

p.circle(non_merchant_chats_made,
         non_merchant_chats_received,
         size=10,color='blue',alpha=0.5,
         legend='Non-Merchant')

p.xaxis.axis_label = 'Chats Made'
p.yaxis.axis_label = 'Chats Received'
p.legend.location = 'top_left'

show(p)

enter image description here

相关问题 更多 >