2017-02-14 61 views
3

我有一个熊猫数据帧包含数据,例如:在比较他们[聊天的民接到散点图如何使用Bokeh和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 

我希望能够绘制两种不同类型的用户(招商,无招) y轴]。

在上面的数据,

招是那些在商家列中的商人列标记为“1”和

非招是那些标记为“1”,

是二进制,没有[1,1]。

我是新来的散景和数据Viz一般,假设散景是我首选的方法,我应该如何去做这件事?

回答

1

这里是背景虚化做到这一点

代码的方法:

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

+0

嗨@rbierman,我给它一个尝试,但我的情节是空白):任何想法,为什么? https://gist.github.com/rphly/dda99854a77e2fcc1b9c94fe2ef38ea1 我应该提到我的数值范围从单个数字到800〜。 – raph

+0

解决!去检查dtypes并意识到我的1&0是字符串类型。谢谢! – raph

+0

哦,真好!如果您对此答案感到满意,请接受它,以便问题关闭 – mitoRibo

相关问题