如何将标签添加到具有多个变量的散点图?
How to add a label to a scatter, with multiple variables?
我创建了一个散点图,其中的点随着我 table 内行星的某些大小而变化。
为此,我使用了这个函数:
#data:
x=composition
y=composition
z=planetary_radii
#some stuff to let the scatter organized:
left, width = 0.1, 0.7
bottom, height = 0.1, 0.7
rect_scatter = [left, bottom, width, height]
ax_scatter = plt.axes(rect_scatter)
#the function that separates the dots in different colors:
colors = []
for i in z:
if i > 8:
colors.append('r')
elif i<8 and i>4:
colors.append('b')
elif i<4 and i>2:
colors.append('g')
elif i<2:
colors.append('orange')
else:
colors.append('y')
# the scatter plot:
ax_scatter.scatter(x, y,c=colors, s=10)
然后,我希望这些点位于标签中,但名称不同于 'g'、'orange' 等。它们会像 'Radii>8'、'4
我怎样才能做到这一点?我是否必须创建另一个函数才能使用 db.scatter 中的标签参数?
这张图片显示了没有标签的散点图:
我得到了一个解决方案,它利用了最新版本的 matplotlib
,即 3.1.2。要安装它,请执行
pip install -U matplotlib
但请注意,它仅适用于 Python3,因为 Python2 仅支持 matplotlib
直到版本 2。
在此处查看完整代码:
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
x=composition
y=composition
z=planetary_radii
# some stuff to let the scatter organized:
left, width = 0.1, 0.7
bottom, height = 0.1, 0.7
rect_scatter = [left, bottom, width, height]
ax_scatter = plt.axes(rect_scatter)
# the function that separates the dots in different classes:
classes = np.zeros( len(x) ) # z > 8
classes[(z <= 8) & (z > 4)] = 1
classes[(z <= 4) & (z > 2)] = 2
classes[z <= 2] = 3
# create color map:
colors = ['r', 'b', 'g', 'orange', 'y']
cm = LinearSegmentedColormap.from_list('custom', colors, N=len(colors))
# the scatter plot:
scatter = ax_scatter.scatter(x, y, c=classes, s=10, cmap=cm)
lines, labels = scatter.legend_elements()
# legend with custom labels
labels = [r'Radii $> 8$', r' <$ Radii $\leq 8$',
r' <$ Radii $\leq 4$', r'Radii $\leq 2$']
legend = ax_scatter.legend(lines, labels,
loc="lower right", title="Classes")
ax_scatter.add_artist(legend)
plt.show()
根据z
的值定义了四个类。请注意,由于您排除了一些值(如 4 和 8),因此我使用较小的等号稍微更改了范围。之后,定义了自定义颜色映射,其中设置了对应 类 的颜色。结果提供给散点图,通过调用 legend_elements()
从散点图中生成 lines
和 labels
。您现在可以根据需要更改这些标签,并最终将它们提供给 ax_scatter.legend()
。这里也可以指定图例的标题。
另一种方法也适用于 Python2.7,基于 roadrunner66 的评论如下:
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
x = np.asarray(composition)
y = np.asarray(composition)
z = np.asarray(planetary_radii)
# some stuff to let the scatter organized:
left, width = 0.1, 0.7
bottom, height = 0.1, 0.7
rect_scatter = [left, bottom, width, height]
ax_scatter = plt.axes(rect_scatter)
# definition of filters, colors, and labels
filters = [z > 8, (z <= 8) & (z > 4), (z <= 4) & (z > 2), z <= 2]
colors = ['r', 'b', 'g', 'orange', 'y']
labels = ['Radii $> 8$', r' <$ Radii $\leq 8$',
r' <$ Radii $\leq 4$', r'Radii $\leq 2$']
# filter the data and plot:
for idx, f in enumerate(filters):
ax_scatter.scatter(x[f], y[f],
c=colors[idx], s=10, label=labels[idx])
ax_scatter.legend(title='Classes', loc="lower right")
plt.show()
首先确保您的数据存储在 np.array
中。 filters
、colors
和 labels
可以随意设置。之后,使用先前定义的过滤条件过滤数据并绘制图表。此处,应用指定的 colors
和 labels
。
我创建了一个散点图,其中的点随着我 table 内行星的某些大小而变化。 为此,我使用了这个函数:
#data:
x=composition
y=composition
z=planetary_radii
#some stuff to let the scatter organized:
left, width = 0.1, 0.7
bottom, height = 0.1, 0.7
rect_scatter = [left, bottom, width, height]
ax_scatter = plt.axes(rect_scatter)
#the function that separates the dots in different colors:
colors = []
for i in z:
if i > 8:
colors.append('r')
elif i<8 and i>4:
colors.append('b')
elif i<4 and i>2:
colors.append('g')
elif i<2:
colors.append('orange')
else:
colors.append('y')
# the scatter plot:
ax_scatter.scatter(x, y,c=colors, s=10)
然后,我希望这些点位于标签中,但名称不同于 'g'、'orange' 等。它们会像 'Radii>8'、'4
我怎样才能做到这一点?我是否必须创建另一个函数才能使用 db.scatter 中的标签参数?
这张图片显示了没有标签的散点图:
我得到了一个解决方案,它利用了最新版本的 matplotlib
,即 3.1.2。要安装它,请执行
pip install -U matplotlib
但请注意,它仅适用于 Python3,因为 Python2 仅支持 matplotlib
直到版本 2。
在此处查看完整代码:
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
x=composition
y=composition
z=planetary_radii
# some stuff to let the scatter organized:
left, width = 0.1, 0.7
bottom, height = 0.1, 0.7
rect_scatter = [left, bottom, width, height]
ax_scatter = plt.axes(rect_scatter)
# the function that separates the dots in different classes:
classes = np.zeros( len(x) ) # z > 8
classes[(z <= 8) & (z > 4)] = 1
classes[(z <= 4) & (z > 2)] = 2
classes[z <= 2] = 3
# create color map:
colors = ['r', 'b', 'g', 'orange', 'y']
cm = LinearSegmentedColormap.from_list('custom', colors, N=len(colors))
# the scatter plot:
scatter = ax_scatter.scatter(x, y, c=classes, s=10, cmap=cm)
lines, labels = scatter.legend_elements()
# legend with custom labels
labels = [r'Radii $> 8$', r' <$ Radii $\leq 8$',
r' <$ Radii $\leq 4$', r'Radii $\leq 2$']
legend = ax_scatter.legend(lines, labels,
loc="lower right", title="Classes")
ax_scatter.add_artist(legend)
plt.show()
根据z
的值定义了四个类。请注意,由于您排除了一些值(如 4 和 8),因此我使用较小的等号稍微更改了范围。之后,定义了自定义颜色映射,其中设置了对应 类 的颜色。结果提供给散点图,通过调用 legend_elements()
从散点图中生成 lines
和 labels
。您现在可以根据需要更改这些标签,并最终将它们提供给 ax_scatter.legend()
。这里也可以指定图例的标题。
另一种方法也适用于 Python2.7,基于 roadrunner66 的评论如下:
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
x = np.asarray(composition)
y = np.asarray(composition)
z = np.asarray(planetary_radii)
# some stuff to let the scatter organized:
left, width = 0.1, 0.7
bottom, height = 0.1, 0.7
rect_scatter = [left, bottom, width, height]
ax_scatter = plt.axes(rect_scatter)
# definition of filters, colors, and labels
filters = [z > 8, (z <= 8) & (z > 4), (z <= 4) & (z > 2), z <= 2]
colors = ['r', 'b', 'g', 'orange', 'y']
labels = ['Radii $> 8$', r' <$ Radii $\leq 8$',
r' <$ Radii $\leq 4$', r'Radii $\leq 2$']
# filter the data and plot:
for idx, f in enumerate(filters):
ax_scatter.scatter(x[f], y[f],
c=colors[idx], s=10, label=labels[idx])
ax_scatter.legend(title='Classes', loc="lower right")
plt.show()
首先确保您的数据存储在 np.array
中。 filters
、colors
和 labels
可以随意设置。之后,使用先前定义的过滤条件过滤数据并绘制图表。此处,应用指定的 colors
和 labels
。