python - 如何使用seaborn中的彩色气泡图设置数值变量的色调范围? [英] How to set the hue range for a numeric variable using a colored bubble plot in seaborn, python?

查看:142
本文介绍了python - 如何使用seaborn中的彩色气泡图设置数值变量的色调范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 seaborn 创建 3-D 点 (x,y,z) 的彩色气泡图,每个坐标都是 [0,255] 范围内的整数.我希望坐标轴代表 x 和 y,散射气泡的色调颜色和大小代表 z 坐标.

I'm trying to use seaborn to create a colored bubbleplot of 3-D points (x,y,z), each coordinate being an integer in range [0,255]. I want the axes to represent x and y, and the hue color and size of the scatter bubbles to represent the z-coordinate.

代码:

import seaborn
seaborn.set()
import pandas
import matplotlib.pyplot

x               = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200]
y               = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200]
z               = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200]

df = pandas.DataFrame(list(zip(x, y, z)), columns =['x', 'y', 'z']) 

ax = seaborn.scatterplot(x="x", y="y",
                 hue="z",
                 data=df)

matplotlib.pyplot.xlim(0,255)
matplotlib.pyplot.ylim(0,255)
matplotlib.pyplot.show()

几乎得到了我想要的:

然而,这使得色调范围基于 z 中的数据.我想根据最小和最大 z 值的范围(如 0,255)设置范围,然后让实际点的颜色相应地映射到该范围上(因此,如果一个点的 z 值为 50,那么应该映射到由 [0,255] 范围内的值 50 表示的颜色).

This however makes the hue range be based on the data in z. I instead want to set the range according to the range of the min and max z values (as 0,255), and then let the color of the actual points map onto that range accordingly (so if a point has z-value 50, then that should be mapped onto the color represented by the value 50 in the range [0,255]).

我总结的问题:

  • 如何使用 seaborn 在散点图中手动设置数值变量的色调颜色范围?

我已经在许多教程和论坛上彻底查看了网上,但没有找到答案.我不确定我是否使用了正确的术语.我希望我的信息能得到传达.

I've looked thoroughly online on many tutorials and forums, but have not found an answer. I'm not sure I've used the right terminology. I hope my message got across.

推荐答案

按照@JohanC 的建议使用 Hue_norm 是解决方案.我首先尝试通过删除 [hue=] 参数并仅使用 [hue_norm=] 参数来执行此操作,该参数根本不产生任何颜色(这是有道理的).

Following @JohanC's suggestion of using hue_norm was the solution. I first tried doing so by removing the [hue=] parameter and only using the [hue_norm=] parameter, which didn't produce any colors at all (which makes sense).

自然应该同时使用 [hue=] 和 [hue_norm=] 参数.

Naturally one should use both the [hue=] and the [hue_norm=] parameters.

import seaborn
seaborn.set()
import pandas
import matplotlib.pyplot

x               = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200]
y               = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200]
z               = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 255]

df = pandas.DataFrame(list(zip(x, y, z, my_sizes)), columns =['x', 'y', 'z']) 

ax = seaborn.scatterplot(x="x", y="y",
                 hue="z", 
                 hue_norm=(0,255),        # <------- the solution
                 data=df)

matplotlib.pyplot.xlim(0,255)
matplotlib.pyplot.ylim(0,255)
matplotlib.pyplot.show()

这篇关于python - 如何使用seaborn中的彩色气泡图设置数值变量的色调范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆