簡體   English   中英

如何使用Bokeh和Pandas繪制帶有分類數據的散點圖?

[英]How should I plot a scatterplot with categorical data using Bokeh and Pandas?

我有一個包含數據的Pandas DataFrame:

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

我希望能夠在散點圖上繪制兩種不同類型的用戶(商家,非商家),比較[收到的聊天數,y軸]。

在上面的數據中,

商家是商家專欄中標有“1”的商家

非商家是Merchants列中標記為'1'的商家,

它是二進制的,沒有[1,1]。

我一般都是Bokeh和Data Viz的新手,假設Bokeh是我的首選方法,我應該怎么做呢?

這是用散景做的一種方法

碼:

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)

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM