raph
raph

Reputation: 319

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

I have a Pandas DataFrame containing data as such:

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

I want to be able to plot two different types of users (Merchants, Non-Merchants) on a Scatterplot comparing them for [Num of Chats Received, y axis].

In the data above,

Merchants are those marked '1' in the Merchants column and

Non-Merchants are those marked '1' in the Merchants column,

it is binary, there is no [1,1].

I'm new to Bokeh and Data Viz in general, assuming Bokeh is my preferred method, how should I go about doing this?

Upvotes: 3

Views: 1398

Answers (1)

mitoRibo
mitoRibo

Reputation: 4548

Here is one way to do it with bokeh

CODE:

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

Upvotes: 2

Related Questions