JZ.
JZ.

Reputation: 81

How to draw four different types of points in the same scatter plot using two columns' info?

I have a pandas dataframe. The first two columns are x,y axis values, the third column is 'label1', the fourth column is 'label2'. if 'label1'==0, plot the point with markerfacecolor='red'; else if 'label1'==0, plot the point with markerfacecolor='blue'; if 'label2'==0, plot the point with marker='.'; else if 'label1'==0, plot the point with marker='x'; How can I plot those four types of points using the dataframe in the same scatter plot? Thanks very much!

Upvotes: 0

Views: 530

Answers (2)

Jianxun Li
Jianxun Li

Reputation: 24742

You can do the scatter plot for 4 subgroups on the same axes. Here is the code:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# x, y
xy = np.random.randn(100, 2)
# label1, label2
labels = np.random.choice([True, False], size=(100, 2)).astype(int)
# construct data
data = np.concatenate([xy, labels], axis=1)
df = pd.DataFrame(data, columns=['x', 'y', 'label1', 'label2'])
# group into 4 sub-groups according to labels
mask1 = (df.label1 == 1) & (df.label2 == 0)
mask2 = (df.label1 == 0) & (df.label2 == 0)
mask3 = (df.label1 == 1) & (df.label2 == 1)
mask4 = (df.label1 == 0) & (df.label2 == 1)
# do scatter plots for 4 sub-groups individually on the same axes, add label info
fig, ax = plt.subplots(figsize=(12, 8))
ax.scatter(df.x[mask1], df.y[mask1], marker='o', c='r', label='label1 == 1, label2 == 0')
ax.scatter(df.x[mask2], df.y[mask2], marker='x', c='b', label='label1 == 0, label2 == 0')
ax.scatter(df.x[mask3], df.y[mask3], marker='.', c='g', label='label1 == 1, label2 == 1' )
ax.scatter(df.x[mask4], df.y[mask4], marker='<', c='k', label='label1 == 0, label2 == 1')
# show the legend
ax.legend(loc='best')

Upvotes: 1

sadaf2605
sadaf2605

Reputation: 7540

One way of plotting with 4 color would be:

import numpy as np
import matplotlib.pyplot as plt


N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.randint(4,size=N)
area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radiuses

plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()

Upvotes: 0

Related Questions