从各个点绘制散点图,如何从颜色图中为点着色?
Plotting a scatter plot from individual points, how to color points from a colormap?
我正在尝试使用 matplotlib 生成散点图,其中点的标记和颜色都不同。
import matplotlib.pyplot as plt
import numpy as np
markers = np.array(["1","2","3","4","+",">"])
# Dummy Data
x = np.array([0, 2, 3, 4, 0, 1, 5, 6, 7, 8])
y = np.array([0, 1, 0, 2, 3, 4, 5, 6, 7, 8])
c = np.array([0, 0.4, 0, 0.2, 1, 0.9, 1, 1, 1])
m = np.array([0, 0, 0, 1, 1, 1, 2, 3, 3])
# Figure
fig, ax = plt.subplots(figsize=(8,7))
# Scatter
ax.scatter(x=x, y=y, c=c, marker=markers[m], cmap = 'summer') # Not possible
但是,matplotlib 目前不支持提供标记数组。
所以我发现自己在点上循环,但颜色没有改变。
# Works nice for markers but all points are same color.
for i in range(x.shape[0]):
ax.scatter(x=x[i], y=y[i], c=c[i], marker=markers[m[i]], cmap = 'summer')
我不熟悉使用 ColorMap 实例,也找不到适合我想要实现的目标的示例。
可选地,最好有一个颜色图。
像在第二个块中一样对每个点使用 ax.scatter
,但结合新创建的 newcmp
,包括参数 vmin
和 vmax
将颜色数组 c
映射到颜色图 cmap
.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.colors import ListedColormap
markers = np.array(["1","2","3","4","+",">"])
# Dummy Data
x = np.array([0, 2, 3, 4, 0, 1, 5, 6, 7, 8])
y = np.array([0, 1, 0, 2, 3, 4, 5, 6, 7, 8])
c = np.array([0, 0.4, 0, 0.2, 1, 0.8, 0.9, 1, 1, 1])
m = np.array([0, 0, 0, 1, 1, 2, 3, 4, 5, 3])
# Figure
fig, ax = plt.subplots(figsize=(8,7))
hsv_modified = cm.get_cmap('hsv', 500)
newcmp = ListedColormap(hsv_modified(np.linspace(0.0, 0.3, 500)))
for xi,yi,mi,ci in zip(x,y,m,c):
ax.scatter(xi,yi,c=ci, marker=markers[mi], s=200, cmap=newcmp, vmin=min(c), vmax=max(c))
plt.show()
我正在尝试使用 matplotlib 生成散点图,其中点的标记和颜色都不同。
import matplotlib.pyplot as plt
import numpy as np
markers = np.array(["1","2","3","4","+",">"])
# Dummy Data
x = np.array([0, 2, 3, 4, 0, 1, 5, 6, 7, 8])
y = np.array([0, 1, 0, 2, 3, 4, 5, 6, 7, 8])
c = np.array([0, 0.4, 0, 0.2, 1, 0.9, 1, 1, 1])
m = np.array([0, 0, 0, 1, 1, 1, 2, 3, 3])
# Figure
fig, ax = plt.subplots(figsize=(8,7))
# Scatter
ax.scatter(x=x, y=y, c=c, marker=markers[m], cmap = 'summer') # Not possible
但是,matplotlib 目前不支持提供标记数组。
所以我发现自己在点上循环,但颜色没有改变。
# Works nice for markers but all points are same color.
for i in range(x.shape[0]):
ax.scatter(x=x[i], y=y[i], c=c[i], marker=markers[m[i]], cmap = 'summer')
我不熟悉使用 ColorMap 实例,也找不到适合我想要实现的目标的示例。
可选地,最好有一个颜色图。
像在第二个块中一样对每个点使用 ax.scatter
,但结合新创建的 newcmp
,包括参数 vmin
和 vmax
将颜色数组 c
映射到颜色图 cmap
.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.colors import ListedColormap
markers = np.array(["1","2","3","4","+",">"])
# Dummy Data
x = np.array([0, 2, 3, 4, 0, 1, 5, 6, 7, 8])
y = np.array([0, 1, 0, 2, 3, 4, 5, 6, 7, 8])
c = np.array([0, 0.4, 0, 0.2, 1, 0.8, 0.9, 1, 1, 1])
m = np.array([0, 0, 0, 1, 1, 2, 3, 4, 5, 3])
# Figure
fig, ax = plt.subplots(figsize=(8,7))
hsv_modified = cm.get_cmap('hsv', 500)
newcmp = ListedColormap(hsv_modified(np.linspace(0.0, 0.3, 500)))
for xi,yi,mi,ci in zip(x,y,m,c):
ax.scatter(xi,yi,c=ci, marker=markers[mi], s=200, cmap=newcmp, vmin=min(c), vmax=max(c))
plt.show()