i want to scatter plot 2 variables and color the points according to a 3rd variable which is a qualitative variable contains 2 classes.
df.plot(kind='scatter', x='TotalIncome', y='LoanAmount', figsize=(10, 6))
plt.xlabel('total income')
plt.ylabel('loan amount')
plt.show()
It is straightforward to colour individual points with respect to a third variable. You just need to map this qualitative variable to something that can represent a colour. For example, if this third variable were 'c' in the following code:
import matplotlib.pyplot as plt
def map_col(col):
if col == 'y_odd':
mapped_col = 0
elif col == 'y_even':
mapped_col = 1
return mapped_col
x = [1, 4, 2, 7, 4, 9]
y = [4, 1, 3, 6, 8, 2]
c = ['y_even', 'y_odd', 'y_odd', 'y_even', 'y_even', 'y_even']
color = [map_col(col) for col in c]
plt.scatter(x, y, s=100, c=color, cmap="gnuplot")
plt.show()
For a list of available color maps see:
https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.