如何在 matplotlib 中的颜色条上可视化字符串列表
How to visualize a list of strings on a colorbar in matplotlib
我有一个像
这样的数据集
x = 3,4,6,77,3
y = 8,5,2,5,5
labels = "null","exit","power","smile","null"
然后我用
from matplotlib import pyplot as plt
plt.scatter(x,y)
colorbar = plt.colorbar(labels)
plt.show()
可以制作散点图,但无法制作以标签显示颜色的颜色条。
如何获得这个?
我不确定,一般来说,对 scatter
图这样做是否是个好主意(您对不同的数据点有相同的描述,也许只是在这里使用一些图例?),但我猜猜你想到的具体解决方案,可能是以下内容:
from matplotlib import pyplot as plt
# Data
x = [3, 4, 6, 77, 3]
y = [8, 5, 2, 5, 5]
labels = ('null', 'exit', 'power', 'smile', 'null')
# Customize colormap and scatter plot
cm = plt.cm.get_cmap('hsv')
sc = plt.scatter(x, y, c=range(5), cmap=cm)
cbar = plt.colorbar(sc, ticks=range(5))
cbar.ax.set_yticklabels(labels)
plt.show()
这将导致这样的输出:
代码结合了this Matplotlib demo and this SO answer.
希望对您有所帮助!
编辑:结合评论,我只能想到某种标签颜色字典,从颜色生成自定义颜色图,并在显式绘制之前获取正确的颜色索引来自标签。
这是更新后的代码(我添加了一些额外的颜色和数据点来检查可伸缩性):
from matplotlib import pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np
# Color information; create custom colormap
label_color_dict = {'null': '#FF0000',
'exit': '#00FF00',
'power': '#0000FF',
'smile': '#FF00FF',
'addon': '#AAAAAA',
'addon2': '#444444'}
all_labels = list(label_color_dict.keys())
all_colors = list(label_color_dict.values())
n_colors = len(all_colors)
cm = LinearSegmentedColormap.from_list('custom_colormap', all_colors, N=n_colors)
# Data
x = [3, 4, 6, 77, 3, 10, 40]
y = [8, 5, 2, 5, 5, 4, 7]
labels = ('null', 'exit', 'power', 'smile', 'null', 'addon', 'addon2')
# Get indices from color list for given labels
color_idx = [all_colors.index(label_color_dict[label]) for label in labels]
# Customize colorbar and plot
sc = plt.scatter(x, y, c=color_idx, cmap=cm)
c_ticks = np.arange(n_colors) * (n_colors / (n_colors + 1)) + (2 / n_colors)
cbar = plt.colorbar(sc, ticks=c_ticks)
cbar.ax.set_yticklabels(all_labels)
plt.show()
并且,新输出:
找到每个颜色段的正确中间点(仍然)不好,但我会把这个优化留给你。
我有一个像
这样的数据集x = 3,4,6,77,3
y = 8,5,2,5,5
labels = "null","exit","power","smile","null"
然后我用
from matplotlib import pyplot as plt
plt.scatter(x,y)
colorbar = plt.colorbar(labels)
plt.show()
可以制作散点图,但无法制作以标签显示颜色的颜色条。
如何获得这个?
我不确定,一般来说,对 scatter
图这样做是否是个好主意(您对不同的数据点有相同的描述,也许只是在这里使用一些图例?),但我猜猜你想到的具体解决方案,可能是以下内容:
from matplotlib import pyplot as plt
# Data
x = [3, 4, 6, 77, 3]
y = [8, 5, 2, 5, 5]
labels = ('null', 'exit', 'power', 'smile', 'null')
# Customize colormap and scatter plot
cm = plt.cm.get_cmap('hsv')
sc = plt.scatter(x, y, c=range(5), cmap=cm)
cbar = plt.colorbar(sc, ticks=range(5))
cbar.ax.set_yticklabels(labels)
plt.show()
这将导致这样的输出:
代码结合了this Matplotlib demo and this SO answer.
希望对您有所帮助!
编辑:结合评论,我只能想到某种标签颜色字典,从颜色生成自定义颜色图,并在显式绘制之前获取正确的颜色索引来自标签。
这是更新后的代码(我添加了一些额外的颜色和数据点来检查可伸缩性):
from matplotlib import pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np
# Color information; create custom colormap
label_color_dict = {'null': '#FF0000',
'exit': '#00FF00',
'power': '#0000FF',
'smile': '#FF00FF',
'addon': '#AAAAAA',
'addon2': '#444444'}
all_labels = list(label_color_dict.keys())
all_colors = list(label_color_dict.values())
n_colors = len(all_colors)
cm = LinearSegmentedColormap.from_list('custom_colormap', all_colors, N=n_colors)
# Data
x = [3, 4, 6, 77, 3, 10, 40]
y = [8, 5, 2, 5, 5, 4, 7]
labels = ('null', 'exit', 'power', 'smile', 'null', 'addon', 'addon2')
# Get indices from color list for given labels
color_idx = [all_colors.index(label_color_dict[label]) for label in labels]
# Customize colorbar and plot
sc = plt.scatter(x, y, c=color_idx, cmap=cm)
c_ticks = np.arange(n_colors) * (n_colors / (n_colors + 1)) + (2 / n_colors)
cbar = plt.colorbar(sc, ticks=c_ticks)
cbar.ax.set_yticklabels(all_labels)
plt.show()
并且,新输出:
找到每个颜色段的正确中间点(仍然)不好,但我会把这个优化留给你。