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

我有一组数据要显示为散点图 . 我希望将每个点绘制为大小为 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]


问题是分散函数读取的大小 s 在点^ 2中 . 我'd like is having each point represented by a square of area dx^2, where this area is in '真正的'单位,情节单位 . 我希望你能明白这一点 .

我还有另一个问题 . 散点函数用黑色边框绘制标记,如何删除此选项并且根本没有边框?


2 years ago

user data 坐标系转换为 display 坐标系 .

并使用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')

2 years ago


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 =
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

enter image description here


  • 方块不以 (x,y) 为中心 . x,y实际上是左下方的坐标 . 我这样简化我的代码 . 你应该使用 (x + dx/2, y + dx/2) .

  • 颜色来自热色图 . 我用z ** 2来给出颜色 . 你也应该根据自己的需要进行调整

最后是你的第二个问题 . 您可以使用关键字参数 edgecoloredgecolors 来获取散点图的边框 . 它们分别是matplotlib颜色参数或rgba元组序列 . 如果将参数设置为'None',则不绘制边框 .

2 years ago

我想我们可以通过一系列补丁来做得更好 . 根据文件:

此(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.

    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.

    paths : `~matplotlib.collections.PathCollection`

    a = np.arange(11)
    circles(a, a, a*0.2, c=a, alpha=0.5, edgecolor='none')

    This code is under [The BSD 3-Clause License]
    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_clim(vmin, vmax)

    ax = plt.gca()
    if c is not None:
    return collection

scatter 函数的所有参数和关键字(除了 marker )都可以以类似的方式工作 . 我写了gist,包括 circlesellipsessquares / rectangles . 如果你想要一个其他形状的集合,你可以自己修改它 .

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


from pylab import *
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')

#plot one circle (the lower-right one)
circles(1, 0, 0.4, 'r', ls='--', lw=5, fc='none', transform=ax.transAxes)



Example Figure

2 years ago

为了使Python 3兼容,我添加了以下代码片段

except NameError:
    basestring = str

How to check if variable is string with python 2 and 3 compatibility

这是必要的,因为_3302266_在Python 3中不可用 . 在Python 2中, basestring 的目的是包括 strunicode . 在Python 3中, strunicode 之间没有区别,它只是 str .