python - 根据标签更改颜色和大小(matplotlib,python 3)

标签 python python-3.x matplotlib scatter-plot

我正在尝试使用 matplotlib 显示散点图,到目前为止显示数据、颜色和大小在硬编码时效果很好,但我需要根据标签集分配大小和颜色,例如,DataSet 看起来像:

data = [[1,1],[1,1],[1,1]]
label =['label1','label3','label3']

散点图应如下所示

enter image description here

关于颜色:

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()

关于python - 根据标签更改颜色和大小(matplotlib,python 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37642954/

相关文章:

python - 绘制周期轨迹

python - 是否必须将 redis-py 对象设置为全局变量才能在函数中使用?

python - 我可以在 python 中为多处理创建一个共享的多数组或列表对象列表吗?

python-3.x - AWS S3 根据条件路径检查文件是否存在

python - 奇怪的错误: ZeroDivisionError: float division by zero

python - 使用 Matplotlib 在一张条形图中绘制两个字典

python - Matplotlib:如何绘制一个填充有颜色图的小矩形作为图例

Python静默打印PDF到特定打印机

python - PyQt - 基于值更新的闪烁背景颜色

python - 删除 :memory: 中的数据库文件