根据标签更改颜色和大小(matplotlib, python 3)
Change color and size according to label (matplolib, python 3)
我正在尝试使用 matplotlib 显示散点图,到目前为止显示的数据、颜色和大小在硬编码时很好,但我需要根据标签集分配大小和颜色,例如,DataSet 看起来像:
data = [[1,1],[1,1],[1,1]]
label =['label1','label3','label3']
散点图应如下所示
关于颜色:
so label1 is green
so label2 is red
so label3 is blue
关于尺码:每款尺码相差很小,您可以分辨。
感谢任何帮助,我声明要在 python
中编写代码
谢谢
这是您可以使用的片段。如果您需要更多自定义,此 example from the matplotlib 文档可能会有所帮助。
import matplotlib.pyplot as plt
data = [[1,2],[2,1],[1,1]]
labels = ['label1','label2','label3']
colorMap = {'label1': 'g',
'label2': 'r',
'label3': 'b'}
sizeMap = {'label1': 10,
'label2': 20,
'label3': 30}
colors = [ colorMap[label] for label in labels]
sizes = [ sizeMap[label] for label in labels]
x,y = zip(*data)
plt.scatter(x,y, c=colors, s=sizes)
plt.show()
我正在尝试使用 matplotlib 显示散点图,到目前为止显示的数据、颜色和大小在硬编码时很好,但我需要根据标签集分配大小和颜色,例如,DataSet 看起来像:
data = [[1,1],[1,1],[1,1]]
label =['label1','label3','label3']
散点图应如下所示
关于颜色:
so label1 is green
so label2 is red
so label3 is blue
关于尺码:每款尺码相差很小,您可以分辨。
感谢任何帮助,我声明要在 python
中编写代码谢谢
这是您可以使用的片段。如果您需要更多自定义,此 example from the matplotlib 文档可能会有所帮助。
import matplotlib.pyplot as plt
data = [[1,2],[2,1],[1,1]]
labels = ['label1','label2','label3']
colorMap = {'label1': 'g',
'label2': 'r',
'label3': 'b'}
sizeMap = {'label1': 10,
'label2': 20,
'label3': 30}
colors = [ colorMap[label] for label in labels]
sizes = [ sizeMap[label] for label in labels]
x,y = zip(*data)
plt.scatter(x,y, c=colors, s=sizes)
plt.show()