繁体   English   中英

Matplotlib:如何对分散的值/数据进行分类 plot?

[英]Matplotlib: how to classify values/data in a scatter plot?

我正在尝试创建一个散点 plot ,在图表上,您可以区分两件事:

  1. 按颜色。 例如,如果值为负,则颜色为红色,如果值为正,则颜色为蓝色。

  2. 按标记大小。 例如,如果值在 -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()

带有大小和色调的seaborn散点图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM