在外部定义打印标签plt.绘图()命令Matplotlib

2024-10-04 05:33:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个散点图,根据数据中的条件使用两个不同的符号。 在遍历数据行的for循环中,如果满足条件,则用圆绘制点,如果不满足条件,则用正方形绘制点:

for i in thick.index:
    if thick['Interest'][i] == 1:
        plt.scatter(thick['NiThickness'][i], thick['GdThickness'][i], marker = 'o', color = 'b')
    else:
        plt.scatter(thick['NiThickness'][i], thick['GdThickness'][i], marker = 's', color = 'r')

其中“Interest”是一个填充了1和0(零?)的列。你知道吗

我希望在图例中有一个圆形标签,一个正方形标签,但是如果我在plt.scatter(...)命令中声明label = 'circle',那么图例中的行数与数据文件中的行数相同。你知道吗

有没有一个简单的小把戏我错过了?你知道吗

谢谢。你知道吗


Tags: 数据for绘制plt标签条件markercolor
2条回答

如果thick是数据帧:

idx = thick['Interest'] == 1
ax = plt.subplot(111)
ax.scatter(thick['NiThickness'][idx], thick['GdThickness'][idx],
           marker='o', color='b', label='circle')
ax.scatter(thick['NiThickness'][~idx], thick['GdThickness'][~idx],
           marker='s', color='r', label='square')

这是我在这种情况下使用的模式:

label_o = 'Circle'
label_s = 'Square'
for i in thick.index:
    if thick['Interest'][i] == 1:
        plt.scatter(thick['NiThickness'][i], thick['GdThickness'][i], marker=o', color='b', label=label_o)
        label_o = None
    else:
        plt.scatter(thick['NiThickness'][i], thick['GdThickness'][i], marker='s', color='r', label=label_s)
        label_s = None

这也很好地解决了只有一个类别存在的情况。你知道吗

相关问题 更多 >