【问题标题】:How to plot blurred points in Matplotlib如何在 Matplotlib 中绘制模糊点
【发布时间】:2014-09-21 06:12:59
【问题描述】:

正如问题所说,我正在寻找一种使用 Matplotlib 绘制模糊点的方法。 我不想绘制一组点,然后应用过滤器来模糊整个图像。而不是它,我想绘制一组点,每个点都有一个相关的模糊程度。

提前谢谢你。

【问题讨论】:

  • 抱歉,使用内置 matplotlib 函数是不可能的。
  • @Banana - 实际上,它带有 *Agg 后端(除了 OSX 之外的所有后端都是默认设置),但它会相当慢。看看:matplotlib.org/examples/pylab_examples/demo_agg_filter.html
  • 实际上有几种不同的方法可以解决这个问题(尽管 Banana 是正确的,你必须要有一点创意)。一个关键的考虑因素是您是否希望模糊的大小在数据坐标或显示坐标中保持固定。换句话说,当你放大时,模糊在显示中应该变大还是应该保持不变?
  • 嗨,@JoeKington。首先,感谢您的帮助。其次,不,我什至没有使用交互模式,只是将图像保存在另一个界面中,而不是 Matplolib 提供的界面。然后,模糊将保持固定。 (;

标签: python matplotlib blur noise


【解决方案1】:

当你做不到的时候,假装它。

import matplotlib.pyplot as plt
import numpy as np

# some random data
x = np.random.random(100)
y = np.random.random(100)
z = np.random.random(100)

# z reflects the amount of defocus at each dot
# if z=0, the point is small (1 pt)
# if z=1, the point is large (50 pt)
# each dot is composed of different layers
fig = plt.figure()
ax = fig.add_subplot(111)
for i in np.arange(.1,1.01,.1):
    ax.scatter(x, y, s=(50*i*(z*.9+.1))**2, color=(0,0,0,.5/i/10))

这给出了:

这绝不是完美的,但这些方面的东西可能足以满足您的需求。需要考虑的事项:

  • 点大小现在是绝对单位,它不能缩放(需要更多的数学来缩放)
  • 如果您希望每个点的墨水量相等,则必须降低较大斑点的 alpha 值
  • 您想让模糊直径反映值(如此处)还是模糊区域?
  • 真正的“模糊”通常是高斯的,这不是;可以这样做,但是尺寸和 alpha 缩放会变长一些
  • 当模糊点相互重叠时,您希望看到什么?
  • 在使用 alpha 值和颜色值进行数学运算时,请记住显示器的 gamma 函数

所以,这只是一个丑陋的假货。有时它们看起来足够好,有时却不够。

【讨论】:

    【解决方案2】:

    这是另一种解决方法。您可以使用BboxImage 在每个位置显示图像而不是标记。这样,您可以以任何您想要的方式模糊或操纵图像。本教程有更多关于BboxImages 的信息。

    import matplotlib.pyplot as plt
    from scipy import ndimage
    from matplotlib.image import BboxImage
    from matplotlib.transforms import Bbox, TransformedBbox
    import numpy as np
    
    # Create and save an image with just a marker in it
    fig1 = plt.figure()
    ax1 = fig1.add_subplot(111)
    ax1.plot(0.5,0.5,'*',ms=200)
    ax1.set_ylim(0,1)
    ax1.set_xlim(0,1)
    plt.axis('off')
    fig1.savefig('marker.png')
    
    # Read in the same marker image
    marker = plt.imread('marker.png')
    
    # New figure and data
    fig2 = plt.figure()
    ax2 = fig2.add_subplot(111)
    x = 8*np.random.rand(10) + 1
    y = 8*np.random.rand(10) + 1
    sigma = np.arange(10,60,5)
    
    # Blur the marker and image plot the blurred image at each data point. 
    for xi, yi, sigmai in zip(x,y,sigma):
        markerBlur = ndimage.gaussian_filter(marker,sigmai) # Blur the marker image
    
        # Create an BboxImage for the blurred marker and add it to the plot. 
        bb = Bbox.from_bounds(xi,yi,1,1)  
        bb2 = TransformedBbox(bb,ax2.transData)
        bbox_image = BboxImage(bb2,
                               norm = None,
                               origin=None,
                               clip_on=False)
    
        bbox_image.set_data(markerBlur)
        ax2.add_artist(bbox_image)
    
    ax2.set_xlim(0,10)
    ax2.set_ylim(0,10)
    plt.show()
    

    【讨论】:

    • 很好的解决方案!不知道那个!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 2017-12-24
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    相关资源
    最近更新 更多