iterating markers in plots

前端 未结 2 1265
挽巷
挽巷 2020-12-02 00:36

I\'m trying to denote the predictions with a color and the correct labels as markers for the iris data set. Here is what I have so far:

from sklearn.mixture          


        
2条回答
  •  忘掉有多难
    2020-12-02 01:27

    Using several markers in a single scatter is currently not a feature matplotlib supports. There is however a feature request for this at https://github.com/matplotlib/matplotlib/issues/11155

    It is of course possible to draw several scatters, one for each marker type. A different option is the one I proposed in the above thread, which is to set the markers after creating the scatter:

    import numpy as np
    import matplotlib.pyplot as plt
    
    def mscatter(x,y,ax=None, m=None, **kw):
        import matplotlib.markers as mmarkers
        if not ax: ax=plt.gca()
        sc = ax.scatter(x,y,**kw)
        if (m is not None) and (len(m)==len(x)):
            paths = []
            for marker in m:
                if isinstance(marker, mmarkers.MarkerStyle):
                    marker_obj = marker
                else:
                    marker_obj = mmarkers.MarkerStyle(marker)
                path = marker_obj.get_path().transformed(
                            marker_obj.get_transform())
                paths.append(path)
            sc.set_paths(paths)
        return sc
    
    
    N = 40
    x, y, c = np.random.rand(3, N)
    s = np.random.randint(10, 220, size=N)
    m = np.repeat(["o", "s", "D", "*"], N/4)
    
    fig, ax = plt.subplots()
    
    scatter = mscatter(x, y, c=c, s=s, m=m, ax=ax)
    
    plt.show()
    

    If you only have numbers, instead of marker symbols you would first need to map numbers to symbols and supply the list of symbols to the function.

提交回复
热议问题