Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python散点图。标记的大小和样式_Python_Plot_Matplotlib_Scatter - Fatal编程技术网

Python散点图。标记的大小和样式

Python散点图。标记的大小和样式,python,plot,matplotlib,scatter,Python,Plot,Matplotlib,Scatter,我有一组数据要显示为散点图。我希望将每个点绘制为大小为dx的正方形 x = [0.5,0.1,0.3] y = [0.2,0.7,0.8] z = [10.,15.,12.] dx = [0.05,0.2,0.1] scatter(x,y,c=z,s=dx,marker='s') 问题是散射函数读取的大小s位于点^2。我想要的是用面积dx^2的平方表示每个点,其中这个面积是以“实”单位表示的,

我有一组数据要显示为散点图。我希望将每个点绘制为大小为
dx
的正方形

          x = [0.5,0.1,0.3]
          y = [0.2,0.7,0.8]
          z = [10.,15.,12.]
          dx = [0.05,0.2,0.1]

          scatter(x,y,c=z,s=dx,marker='s')
问题是散射函数读取的大小
s
位于点^2。我想要的是用面积dx^2的平方表示每个点,其中这个面积是以“实”单位表示的,即绘图单位。我希望你能明白这一点


我还有一个问题。散布函数用黑色边框绘制标记,我如何删除此选项而完全没有边框

如果您希望标记根据地物大小调整大小,可以使用面片:

from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle

x = [0.5, 0.1, 0.3]
y = [0.2 ,0.7, 0.8]
z = [10, 15, 12]
dx = [0.05, 0.2, 0.1]

cmap = plt.cm.hot
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')

for x, y, c, h in zip(x, y, z, dx):
    ax.add_artist(Rectangle(xy=(x, y),
                  color=cmap(c**2),        # I did c**2 to get nice colors from your numbers
                  width=h, height=h))      # Gives a square of area h*h

plt.show()

请注意:

  • 正方形的中心不在
    (x,y)
    。x、 y实际上是 广场在左下方。我这样做是为了简化我的代码。你 应该使用
    (x+dx/2,y+dx/2)
  • 颜色是从热颜色贴图中获取的。我用z**2表示颜色。 你也应该根据自己的需要来调整


  • 最后是你的第二个问题。您可以使用关键字参数
    edgecolor
    edgecolors
    获取散点标记的边框。它们分别是matplotlib颜色参数或rgba元组序列。如果将参数设置为“无”,则不会绘制边框。

    将用户数据坐标系转换为显示坐标系

    并使用edgecolors='none'绘制没有轮廓的面

    import numpy as np
    
    fig = figure()
    ax = fig.add_subplot(111)
    dx_in_points = np.diff(ax.transData.transform(zip([0]*len(dx), dx))) 
    scatter(x,y,c=z,s=dx_in_points**2,marker='s', edgecolors='none')
    

    我认为我们可以通过收集补丁做得更好。 根据文件:

    此(PatchCollection)使将颜色贴图分配给异构对象变得更容易 补丁的集合

    这也可以提高绘图速度,因为PatchCollection将 绘制速度比绘制大量面片快

    假设要以数据单位绘制具有给定半径的圆的散布:

    def circles(x, y, s, c='b', vmin=None, vmax=None, **kwargs):
        """
        Make a scatter of circles plot of x vs y, where x and y are sequence 
        like objects of the same lengths. The size of circles are in data scale.
    
        Parameters
        ----------
        x,y : scalar or array_like, shape (n, )
            Input data
        s : scalar or array_like, shape (n, ) 
            Radius of circle in data unit.
        c : color or sequence of color, optional, default : 'b'
            `c` can be a single color format string, or a sequence of color
            specifications of length `N`, or a sequence of `N` numbers to be
            mapped to colors using the `cmap` and `norm` specified via kwargs.
            Note that `c` should not be a single numeric RGB or RGBA sequence 
            because that is indistinguishable from an array of values
            to be colormapped. (If you insist, use `color` instead.)  
            `c` can be a 2-D array in which the rows are RGB or RGBA, however. 
        vmin, vmax : scalar, optional, default: None
            `vmin` and `vmax` are used in conjunction with `norm` to normalize
            luminance data.  If either are `None`, the min and max of the
            color array is used.
        kwargs : `~matplotlib.collections.Collection` properties
            Eg. alpha, edgecolor(ec), facecolor(fc), linewidth(lw), linestyle(ls), 
            norm, cmap, transform, etc.
    
        Returns
        -------
        paths : `~matplotlib.collections.PathCollection`
    
        Examples
        --------
        a = np.arange(11)
        circles(a, a, a*0.2, c=a, alpha=0.5, edgecolor='none')
        plt.colorbar()
    
        License
        --------
        This code is under [The BSD 3-Clause License]
        (http://opensource.org/licenses/BSD-3-Clause)
        """
        import numpy as np
        import matplotlib.pyplot as plt
        from matplotlib.patches import Circle
        from matplotlib.collections import PatchCollection
    
        if np.isscalar(c):
            kwargs.setdefault('color', c)
            c = None
        if 'fc' in kwargs: kwargs.setdefault('facecolor', kwargs.pop('fc'))
        if 'ec' in kwargs: kwargs.setdefault('edgecolor', kwargs.pop('ec'))
        if 'ls' in kwargs: kwargs.setdefault('linestyle', kwargs.pop('ls'))
        if 'lw' in kwargs: kwargs.setdefault('linewidth', kwargs.pop('lw'))
    
        patches = [Circle((x_, y_), s_) for x_, y_, s_ in np.broadcast(x, y, s)]
        collection = PatchCollection(patches, **kwargs)
        if c is not None:
            collection.set_array(np.asarray(c))
            collection.set_clim(vmin, vmax)
    
        ax = plt.gca()
        ax.add_collection(collection)
        ax.autoscale_view()
        if c is not None:
            plt.sci(collection)
        return collection
    
    scatter
    函数的所有参数和关键字(除了
    marker
    )都将以类似的方式工作。 我写了一篇文章,包括圆形椭圆正方形/矩形。如果需要其他形状的集合,可以自己修改

    如果要打印颜色条,只需运行
    colorbar()
    或将返回的集合对象传递给
    colorbar
    函数

    例如:

    from pylab import *
    figure(figsize=(6,4))
    ax = subplot(aspect='equal')
    
    #plot a set of circle
    a = arange(11)
    out = circles(a, a, a*0.2, c=a, alpha=0.5, ec='none')
    colorbar()
    
    #plot one circle (the lower-right one)
    circles(1, 0, 0.4, 'r', ls='--', lw=5, fc='none', transform=ax.transAxes)
    
    xlim(0,10)
    ylim(0,10)
    
    输出:


    这不会按照OP的要求以绘图单位绘制正方形,而是固定大小的正方形,不会调整大小(例如,通过手动更改图框大小。这可能是一个愚蠢的问题。但是,如果dx不是数组,但每个点(x、y、z)都相同,如何更改上面的代码。此外,我真的需要使用add_子绘图吗?您如何找到
    edgecolors
    参数?我想在开源项目中使用您的函数,但不能这样做,因为默认情况下所有的SO代码都在下面。您能显式说明您代码的许可证吗,最好是BSD之类的吗?@neo很高兴知道这一点。我不熟悉许可证,我认为它应该与保持一致,因为我刚刚基于
    scatter
    函数编写了这段代码。所以它应该是PSF之类的?您的代码片段不是matplotlib的派生作品,因此您可以在任何许可证下对代码进行许可。我只会使用,它在Python世界中非常常见。@neo这很好。我将使用BSD 3-条款