具有不同标记和颜色的matplotlib散点图 [英] matplotlib scatter plot with different markers and colors

查看:76
本文介绍了具有不同标记和颜色的matplotlib散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据2个外部矢量的值绘制具有不同标记和不同颜色的图.

I would like to make a plot with different markers and different colors according to the values of 2 external vectors.

这是我尝试过的:

>>> s = [u'+', u'+', u'o']
>>> col = ['r','r','g']
>>> x = np.array([1,2,3])
>>> y = np.array([4,5,6])
>>> pl.scatter(x,y,marker=s,c=col)
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/matplotlib/markers.py", line 233, in set_marker
    Path(marker)
  File "/usr/lib/python3/dist-packages/matplotlib/path.py", line 133, in __init__
    vertices = np.asarray(vertices, np.float_)
  File "/usr/lib/python3/dist-packages/numpy/core/numeric.py", line 460, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: '+'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 3087, in scatter
    linewidths=linewidths, verts=verts, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axes.py", line 6298, in scatter
    marker_obj = mmarkers.MarkerStyle(marker)
  File "/usr/lib/python3/dist-packages/matplotlib/markers.py", line 162, in __init__
    self.set_marker(marker)
  File "/usr/lib/python3/dist-packages/matplotlib/markers.py", line 236, in set_marker
    raise ValueError('Unrecognized marker style {}'.format(marker))
ValueError: Unrecognized marker style ['+', '+', 'o']
>>> 

提出的解决方案适用于模拟示例,但不适用于我的实际问题. (我不知道为什么).

The proposed solution work on the simulated example but it does not work on my real problem. (I do not know why).

这是我的完整代码:

import matplotlib.pyplot as pl
import numpy as np
locY = np.linspace(0, 1, 50) #in the real data locX and locY are coordinate of the points
locX = np.linspace(0,1,50)
p = 50

C = {0: [0, 13, 14, 19, 22, 30], 16: [1, 16, 17], 20: [15, 18, 20, 24], 37: [4, 8, 9, 33, 37, 40, 47], 41: [5, 7, 28, 32, 34, 36, 39, 41, 42, 44, 46], 26: [6, 26, 27, 29, 31, 35], 45: [10, 11, 12, 38, 43, 45, 48, 49], 21: [2, 3, 21, 23, 25]}
        index = C.keys()

        def plot_cluster(C, index):
            color_map = ['b', 'g', 'r', 'c', 'm', 'y',  'k', 'w']
            colors = []
            m_type = []


            for i in range(p):
                for pos, k in enumerate(index):
                    if i in C[k]:
                        #colors.append(pos)
                        colors.append(color_map[pos])
                        if i == k:
                            m_type.append(u'+')
                        else:
                            m_type.append(u'o')


            fig = pl.scatter(locX, locY, c=colors, s=30, cmap='spectral')
            fig = pl.locator_params(nbins=3)
            fig = pl.xlim(-0.2, 1.2)
            fig = pl.ylim(-0.2, 1.2)
            fig = pl.locator_params(nbins=5, axis='y')

            return fig

    pl.show(plot_cluster(C, index))

如果我用建议的答案代替绘图部分,那么在绘图中我只会得到2分(我不知道为什么)

If I replace the plotting part with the suggested answer I obtain only 2 points in the plot (I do not know why)

我该如何解决?

推荐答案

这有效:

s = [u'+', u'+', u'o']
col = ['r','r','g']
x = np.array([1,2,3])
y = np.array([4,5,6])

for _s, c, _x, _y in zip(s, col, x, y):
    plt.scatter(_x, _y, marker=_s, c=c)

plt.xlim(0, 4)
plt.ylim(0, 8)

plt.show()

呈现如下:

更新

似乎您可以使用多种颜色,并且只需调用一下散点函数即可:示例.在 API 上确认了多色功能,但未读取您可以为标记kwarg指定一个可迭代的对象.如果删除marker=s

It seems you can have a variety of colors and have a single call to the scatter function: example. The multiple color feature is confirmed on the API but it doesn't read that you can specify an iterable for the marker kwarg. Your code works if you remove marker=s

这篇关于具有不同标记和颜色的matplotlib散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆