如何根据第三个变量对分散标记进行着色

发布于 2024-12-17 06:00:47 字数 185 浏览 2 评论 0原文

我想制作一个散点图(使用 matplotlib),其中点根据第三个变量进行着色。我已经非常接近这个:

plt.scatter(w, M, c=p, marker='s')

其中 w 和 M 是数据点,p 是我想要相对于其进行着色的变量。
不过我想用灰度而不是彩色来做。有人可以帮忙吗?

I want to make a scatterplot (using matplotlib) where the points are shaded according to a third variable. I've got very close with this:

plt.scatter(w, M, c=p, marker='s')

where w and M are the data points and p is the variable I want to shade with respect to.
However I want to do it in greyscale rather than colour. Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

茶花眉 2024-12-24 06:00:47

无需手动设置颜色。相反,指定灰度颜色图...

import numpy as np
import matplotlib.pyplot as plt

# Generate data...
x = np.random.random(10)
y = np.random.random(10)

# Plot...
plt.scatter(x, y, c=y, s=500) # s is a size of marker 
plt.gray()

plt.show()

在此处输入图像描述

或者,如果您更喜欢 更广泛的颜色图,您还可以指定 cmap kwarg 分散。要使用其中任何一个的反向版本,只需指定其中任何一个的“_r”版本即可。例如,用gray_r代替gray。有几种不同的预先制作的灰度颜色图(例如graygist_yargbinary等)。

import matplotlib.pyplot as plt
import numpy as np

# Generate data...
x = np.random.random(10)
y = np.random.random(10)

plt.scatter(x, y, c=y, s=500, cmap='gray')
plt.show()

There's no need to manually set the colors. Instead, specify a grayscale colormap...

import numpy as np
import matplotlib.pyplot as plt

# Generate data...
x = np.random.random(10)
y = np.random.random(10)

# Plot...
plt.scatter(x, y, c=y, s=500) # s is a size of marker 
plt.gray()

plt.show()

enter image description here

Or, if you'd prefer a wider range of colormaps, you can also specify the cmap kwarg to scatter. To use the reversed version of any of these, just specify the "_r" version of any of them. E.g. gray_r instead of gray. There are several different grayscale colormaps pre-made (e.g. gray, gist_yarg, binary, etc).

import matplotlib.pyplot as plt
import numpy as np

# Generate data...
x = np.random.random(10)
y = np.random.random(10)

plt.scatter(x, y, c=y, s=500, cmap='gray')
plt.show()
筱果果 2024-12-24 06:00:47

在 matplotlib 中,灰色可以作为 0-1 之间的数值字符串给出。
例如 c = '0.1'

然后您可以将第三个变量转换为该范围内的值,并使用它为您的点着色。
在以下示例中,我使用点的 y 位置作为确定颜色的值:

from matplotlib import pyplot as plt

x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [125, 32, 54, 253, 67, 87, 233, 56, 67]

color = [str(item/255.) for item in y]

plt.scatter(x, y, s=500, c=color)

plt.show()

在此处输入图像描述

In matplotlib grey colors can be given as a string of a numerical value between 0-1.
For example c = '0.1'

Then you can convert your third variable in a value inside this range and to use it to color your points.
In the following example I used the y position of the point as the value that determines the color:

from matplotlib import pyplot as plt

x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [125, 32, 54, 253, 67, 87, 233, 56, 67]

color = [str(item/255.) for item in y]

plt.scatter(x, y, s=500, c=color)

plt.show()

enter image description here

も让我眼熟你 2024-12-24 06:00:47

有时您可能需要根据 x 值情况精确绘制颜色。例如,您可能有一个包含 3 种类型的变量和一些数据点的数据框。您想要执行以下操作,

  • 绘制与红色中的物理变量“A”相对应的点。
  • 与蓝色物理变量“B”相对应的绘图点。
  • 与绿色物理变量“C”相对应的绘图点。

在这种情况下,您可能必须编写短函数以将 x 值作为列表映射到相应的颜色名称,然后将该列表传递给 plt.scatter 命令。

x=['A','B','B','C','A','B']
y=[15,30,25,18,22,13]

# Function to map the colors as a list from the input list of x variables
def pltcolor(lst):
    cols=[]
    for l in lst:
        if l=='A':
            cols.append('red')
        elif l=='B':
            cols.append('blue')
        else:
            cols.append('green')
    return cols
# Create the colors list using the function above
cols=pltcolor(x)

plt.scatter(x=x,y=y,s=500,c=cols) #Pass on the list created by the function here
plt.grid(True)
plt.show()

着色散点图作为 x 变量的函数

Sometimes you may need to plot color precisely based on the x-value case. For example, you may have a dataframe with 3 types of variables and some data points. And you want to do following,

  • Plot points corresponding to Physical variable 'A' in RED.
  • Plot points corresponding to Physical variable 'B' in BLUE.
  • Plot points corresponding to Physical variable 'C' in GREEN.

In this case, you may have to write to short function to map the x-values to corresponding color names as a list and then pass on that list to the plt.scatter command.

x=['A','B','B','C','A','B']
y=[15,30,25,18,22,13]

# Function to map the colors as a list from the input list of x variables
def pltcolor(lst):
    cols=[]
    for l in lst:
        if l=='A':
            cols.append('red')
        elif l=='B':
            cols.append('blue')
        else:
            cols.append('green')
    return cols
# Create the colors list using the function above
cols=pltcolor(x)

plt.scatter(x=x,y=y,s=500,c=cols) #Pass on the list created by the function here
plt.grid(True)
plt.show()

Coloring scatter plot as a function of x variable

绮筵 2024-12-24 06:00:47

一个非常简单的解决方案也是这样:

fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,8)) 

p = ax.scatter(x, y, c=y, cmap='cmo.deep')
fig.colorbar(p,ax=ax,orientation='vertical',label='labelname')

A pretty straightforward solution is also this one:

fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8,8)) 

p = ax.scatter(x, y, c=y, cmap='cmo.deep')
fig.colorbar(p,ax=ax,orientation='vertical',label='labelname')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文