[英]Matplotlib: how to classify values/data in a scatter plot?
我正在嘗試創建一個散點 plot ,在圖表上,您可以區分兩件事:
按顏色。 例如,如果值為負,則顏色為紅色,如果值為正,則顏色為藍色。
按標記大小。 例如,如果值在 -0.20 和 0 之間,大小為 100,如果值在 0 和 0.1 之間,大小為 200,如果值在 0.5 和 1 之間,大小為 300,依此類推……
這是我正在處理的一些數據(以防萬一):
0 0.15
1 0.04
2 0.02
3 0.01
4 -0.03
5 -0.07
6 -0.25
7 -0.27
8 -0.30
我嘗試了以下方法:
fig = plt.figure(figsize=(15, 8))
ax = fig.add_subplot(1, 1, 1)
res = np.genfromtxt(os.path.join(folder, 'residuals.csv'), delimiter=',', names=True)
for name in res.dtype.names[1:]:
plt.scatter(res.x, res.y, s=200, c=res.residual, cmap='jet')
這很好用,但它只按顏色對我的數據進行排序。 大小是一樣的,我不知道哪個是負值/正值,所以這就是為什么我要尋找前面提到的這兩個條件。
非常感謝任何幫助!
Seaborn 的 散點圖允許根據變量着色和大小。 這是您的數據類型的外觀。
import matplotlib.pyplot as plt
from matplotlib.colors import TwoSlopeNorm
import numpy as np
import seaborn as sns
res = np.array([0.15, 0.04, 0.02, 0.01, -0.03, -0.07, -0.25, -0.27, -0.30])
x = np.arange(len(res))
y = np.ones(len(res))
hue_norm = TwoSlopeNorm(vcenter=0) # to make sure the center color goes to zero
ax = sns.scatterplot(x=x, y=y, hue=res, hue_norm=hue_norm, size=res, sizes=(100, 300), palette='Spectral')
for xi, resi in zip(x, res):
ax.text(xi, 1.1, f'{resi:.2f}', ha='center', va='bottom')
ax.set_ylim(0.75, 2)
ax.set_yticks([])
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.