簡體   English   中英

Python 散點圖中的顏色類別 plot

[英]Python color clases in a scatter plot

我想分散 plot 2 個變量並根據第三個變量為點着色,這是一個包含 2 個類的定性變量。

df.plot(kind='scatter', x='TotalIncome', y='LoanAmount', figsize=(10, 6))
plt.xlabel('total income')
plt.ylabel('loan amount')
plt.show()

相對於第三個變量為各個點着色是很簡單的。 您只需要將 map 這個定性變量轉換為可以表示顏色的東西。 例如,如果第三個變量在以下代碼中為“c”:

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()

有關可用顏色圖的列表,請參閱:

https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html

暫無
暫無

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

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